是否可以使用UL / LI和无图像进行滑动?

时间:2014-04-19 13:30:00

标签: javascript jquery html css

所以在谷歌寻找滑块教程,我发现了负载,但所有的图像。我想知道是否可以使用ul元素做一个没有图像的滑块。

例如,UL的宽度为300px,我有3个LI元素,每个元素宽度为300px,背景颜色不同。

是否有可能只使用滑块?

很难找到...

我是一个有滑块的新手......!洛尔

谢谢!

4 个答案:

答案 0 :(得分:4)

<强> LIVE DEMO

根据您的要求滑动并添加自动播放

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

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

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();
    });

});

答案 1 :(得分:1)

是的,这绝对可以使用任意数量的滑块插件,但我个人最喜欢的是:

http://owlgraphic.com/owlcarousel

从网站上的描述:

  

您不需要任何特殊标记。您只需要在容器元素中包装div(owl适用于任何类型元素)。 “owl-carousel”类必须使用来自owl.carousel.css文件的正确样式。

一旦你加载了适当的资产,包括两个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>

但您不限于任何特定元素。它也是一个支持触控的滑块,因此它在移动设备上运行良好。

编辑:这是一个我从一个带有<ul><li>组合和不同颜色的bkgds的示例中分叉的编码器。

http://codepen.io/anon/pen/mcgKn

答案 2 :(得分:0)

您可以查看swipejs。它需要div而不是li,但跨浏览器效果很好。

答案 3 :(得分:0)

是的,我一直这样做使用FlexSlide。它也很敏感,我强烈推荐这个:

<强> http://www.woothemes.com/flexslider/

它也快速且易于使用。你可以在里面使用任何东西。