Harman Harman - 5 days ago 6
Javascript Question

Timeline Slider using HTML CSS Javascript

I want to make a timeline slider but I am very confused as I am a newbie.

Requirements:


  1. It must have 15 items.

  2. Two slider buttons(Left and right).

  3. Display five weeks at one time.



How do I do this?

I am able to make the list but I don't know how to hide elements except the ones I want to display. How do I give width to each element of the list.

Answer

I got what you want by using this code snippet for a simple slider: 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

Comments