javascript 是否可以制作带有 UL/LI 且无图像的滑块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23170668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to do a SLIDER with UL/LI and NO IMAGES?
提问by MarkBurnsRed
So was looking on google for slider tutorials, I have found loads, but all for images. I wanted to know if it was possible to do a slider with ul elements, with no images.
所以在谷歌上寻找滑块教程,我发现了负载,但都是针对图像的。我想知道是否可以用 ul 元素做一个没有图像的滑块。
UL has width of 300px, for example, and I have 3 LI elements, each one 300px width and with a diferent background color.
例如,UL 的宽度为 300 像素,我有 3 个 LI 元素,每个元素的宽度为 300 像素,并且具有不同的背景颜色。
Is it possible to do a slider with just that?
是否可以用它来做一个滑块?
It's hard to find...
很难找到...
I'm a newbie with sliders...! lol
我是滑块的新手......!哈哈
Thanks!
谢谢!
回答by 4dgaurav
Slider as per your requirements and added autoplay
根据您的要求滑动并添加自动播放
HTML
HTML
<h1>Slider</h1>
<div id="slider">
<a href="#" class="control_next">>></a>
<a href="#" class="control_prev"><</a>
<ul>
<li>SLIDE 1</li>
<li style="background: #aaa;">SLIDE 2</li>
<li>SLIDE 3</li>
<li style="background: #aaa;">SLIDE 4</li>
</ul>
</div>
<div class="slider_option">
<input type="checkbox" id="checkbox">
<label for="checkbox">Autoplay Slider</label>
</div>
CSS
CSS
html {
border-top: 5px solid #fff;
background: #58DDAF;
color: #2a2a2a;
}
html, body {
margin: 0;
padding: 0;
font-family: 'Open Sans';
}
h1 {
color: #fff;
text-align: center;
font-weight: 300;
}
#slider {
position: relative;
overflow: hidden;
margin: 20px auto 0 auto;
border-radius: 4px;
}
#slider ul {
position: relative;
margin: 0;
padding: 0;
height: 200px;
list-style: none;
}
#slider ul li {
position: relative;
display: block;
float: left;
margin: 0;
padding: 0;
width: 300px;
height: 200px;
background: #ccc;
text-align: center;
line-height: 200px;
}
a.control_prev, a.control_next {
position: absolute;
top: 40%;
z-index: 999;
display: block;
padding: 4% 3%;
width: auto;
height: auto;
background: #2a2a2a;
color: #fff;
text-decoration: none;
font-weight: 600;
font-size: 18px;
opacity: 0.8;
cursor: pointer;
}
a.control_prev:hover, a.control_next:hover {
opacity: 1;
-webkit-transition: all 0.2s ease;
}
a.control_prev {
border-radius: 0 2px 2px 0;
}
a.control_next {
right: 0;
border-radius: 2px 0 0 2px;
}
.slider_option {
position: relative;
margin: 10px auto;
width: 160px;
font-size: 18px;
}
js
js
jQuery(document).ready(function ($) {
$('#checkbox').change(function(){
setInterval(function () {
moveRight();
}, 3000);
});
var slideCount = $('#slider ul li').length;
var slideWidth = $('#slider ul li').width();
var slideHeight = $('#slider ul li').height();
var sliderUlWidth = slideCount * slideWidth;
$('#slider').css({ width: slideWidth, height: slideHeight });
$('#slider ul').css({ width: sliderUlWidth, marginLeft: - slideWidth });
$('#slider ul li:last-child').prependTo('#slider ul');
function moveLeft() {
$('#slider ul').animate({
left: + slideWidth
}, 200, function () {
$('#slider ul li:last-child').prependTo('#slider ul');
$('#slider ul').css('left', '');
});
};
function moveRight() {
$('#slider ul').animate({
left: - slideWidth
}, 200, function () {
$('#slider ul li:first-child').appendTo('#slider ul');
$('#slider ul').css('left', '');
});
};
$('a.control_prev').click(function () {
moveLeft();
});
$('a.control_next').click(function () {
moveRight();
});
});
回答by bazeblackwood
Yes, this is definitely possible with any number of slider plugins, but my personal favorite is:
是的,这绝对可以使用任意数量的滑块插件,但我个人最喜欢的是:
http://owlgraphic.com/owlcarousel
http://owlgraphic.com/owlcarousel
From the description on the site:
从网站上的描述:
You don't need any special markup. All you need is to wrap your divs(owl works with any type element) inside the container element . Class "owl-carousel" is mandatory to apply proper styles that come from owl.carousel.css file.
您不需要任何特殊标记。您所需要的只是将您的 div(猫头鹰适用于任何类型元素)包装在容器元素中。类“owl-carousel”对于应用来自 owl.carousel.css 文件的正确样式是强制性的。
Once you load the proper assets, including two css files and the javascript plugin itself you can just create your markup as so:
一旦你加载了适当的资产,包括两个 css 文件和 javascript 插件本身,你就可以像这样创建你的标记:
<div id="owl-example" class="owl-carousel">
<div> Your Content </div>
<div> Your Content </div>
<div> Your Content </div>
<div> Your Content </div>
<div> Your Content </div>
<div> Your Content </div>
<div> Your Content </div>
...
</div>
But you're not limited to any specific element. It's a touch enabled slider too so it works great on mobile devices.
但是您不限于任何特定元素。它也是一个支持触摸的滑块,因此在移动设备上效果很好。
EDIT: Here's a codepen I forked from an example with a <ul><li>
combo and different colored bkgds.
编辑:这是我从一个带有<ul><li>
组合和不同颜色 bkgds的示例中分叉出来的代码笔。
回答by Thomas
回答by JCBiggar
Yes, I do it all the time Using FlexSlide. Its also responsive, I highly recommend this:
是的,我一直在使用 FlexSlide。它也响应迅速,我强烈推荐这个:
http://www.woothemes.com/flexslider/
http://www.woothemes.com/flexslider/
Its also quick and easy to use. And you can use anything inside of it.
它也快速且易于使用。你可以使用它里面的任何东西。