时间线滑块使用HTML CSS Javascript

时间:2016-11-16 10:23:10

标签: javascript html css

我想制作一个时间轴滑块,但我很困惑,因为我是新手。

要求:

  1. 必须有15个项目。

  2. 两个滑块按钮(左和右)。

  3. 一次显示五周。

  4. 我该怎么做?

    我能够制作列表,但我不知道如何隐藏除我想要显示的元素之外的元素。如何为列表中的每个元素赋予宽度。

3 个答案:

答案 0 :(得分:1)

如果您有一个图像列表,假设您有15个图像的列表,并且每个图像都在具有类abcdclass的div中,那么您可以像给出的代码那样考虑这个。这将在3秒后滑动。

<script>
                                $(function () {

                                    var slides = $(".abcdclass");
                                    var totalslides = slides.length - 1;
                                    var totalnumberofslides = slides.length - 1;

                                    setInterval(function () {

                                        var jsonCSS = {
                                            width: "0",
                                            opacity: "0",
                                            bottom: "0px",
                                        };

                                        $(".abcdclass").eq(totalslides).animate(jsonCSS, 2000);



                                        if (totalslides <= 0) {
                                            $(".abcdclass").animate({
                                                width: "100%",
                                                opacity: "1",
                                                height: "100%"
                                            }, 500);
                                            totalslides = totalnumberofslides;
                                        } else {
                                            totalslides--;
                                        }

                                    }, 3000);


                                });
                            </script>

答案 1 :(得分:1)

试试这个,我已经完成并使用它了。它对我有用

https://codyhouse.co/demo/horizontal-timeline/index.html

答案 2 :(得分:1)

通过使用此代码段获取简单的滑块,我得到了您想要的内容:Very Simple Slider

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

	});    
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: 500px;
  height: 300px;
  background: #ccc;
  text-align: center;
  line-height: 300px;
}

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

#slider ul ul li {
line-height: 3em;
    border-bottom: 1px dashed;
}

#slider ul ul {
    line-height: 1em;
    list-style: none;
    margin: 0;
    padding: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slider">
  <a href="#" class="control_next">&gt;</a>
  <a href="#" class="control_prev">&lt;</a>
  <ul>
    <li>
	 <ul id="slide1">
	  <li>Week 1</li>
	  <li>Week 2</li>
	  <li>Week 3</li>
	  <li>Week 4</li>
	  <li>Week 5</li>
	 </ul>
    </li>
    <li style="background: #aaa;">
	 <ul id="slide2">
	  <li>Week 6</li>
	  <li>Week 7</li>
	  <li>Week 8</li>
	  <li>Week 9</li>
	  <li>Week 10</li>
	 </ul>
    </li>
    <li>
	 <ul id="slide3">
	  <li>Week 11</li>
	  <li>Week 12</li>
	  <li>Week 13</li>
	  <li>Week 14</li>
	  <li>Week 15</li>
	 </ul>
    </li>
  </ul>  
</div>

<div class="slider_option">
  <input type="checkbox" id="checkbox">
  <label for="checkbox">Autoplay Slider</label>
</div>

enter image description here