javascript - 是否可以制作带有 UL/LI 且无图像的 slider ?

标签 javascript jquery html css

所以在谷歌上寻找 slider 教程,我发现了很多,但都是图像。我想知道是否可以用 ul 元素制作一个没有图像的 slider 。

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

是否可以用它来做一个 slider ?

很难找...

我是 slider 新手......!哈哈

谢谢!

最佳答案

<强> LIVE DEMO

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

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

});

关于javascript - 是否可以制作带有 UL/LI 且无图像的 slider ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23170668/

相关文章:

javascript - Protractor - 无法通过CSS获取嵌套元素

Javascript:如何在多个事件发生后执行代码?

javascript - 获取所有选定的 ="selected"值并放入数组中

html - div不随内容展开,不清除其他元素

javascript - 从 JSON 对象中提取值

javascript - 是否可以通过 URL 从 NPM 下载并安装 JS 文件(不是 NPM 包)?

javascript - 使用 jQuery 或 Javascript 保护或启用链接的 'href' 属性

jquery - 一次点击即可实现多种 jQuery 效果

html - Node 中的简单静态 HTML 服务器

javascript - ReactJS 将列动态附加到图像库中的行中