How to animate setInterval used to make an Image slider using javascript/Jquery? -
the images changing , want them have slide effect.
html
<div><img src="1.jpg" id="sliderimage"></div>
this script i'm using:
var myimage = document.getelementbyid("sliderimage"); var imagearray = ["1.jpg","2.jpg","3.jpg"]; var imageindex = 0; function changeimage() { myimage.setattribute("src",imagearray[imageindex]); imageindex++; if (imageindex >= imagearray.length){imageindex = 0;} } var intervalhandle = setinterval(changeimage,5000);
the simplest way achieve put use css transition effect , javascript set timer. helps organize elements inside list. can't see images in code below because of same origin policy. try locally , download images , should work.
here simple slider have built https://github.com/rotexhawk/javascript-practice/tree/master/slider-simple
document.ready = function(){ var slides = document.queryselectorall('#slider li'); var position = 0; slides[position].style.transition = '0s'; slides[position].style.left = '1200px'; var myinterval = setinterval(startslider,4000); function startslider(){ if (position == 3){ slides[position].style.transition = '0s'; slides[position].style.left = '1200px'; position = 0; slides[position].style.transition = '2s'; slides[position].style.left = 0; } else{ slides[position].style.transition = '0s'; slides[position].style.left = '1200px'; position ++; slides[position].style.transition = '2s'; slides[position].style.left = 0; } } }();
.container{ width: 1200px; box-shadow: 5px 5px 35px 0px rgba(0,0,0,0.4); -webkit-box-shadow: 5px 5px 35px 0px rgba(0,0,0,0.4); -moz-box-shadow: 5px 5px 35px 0px rgba(0,0,0,0.4); overflow: hidden; margin: 0 auto; padding: 5%; } #slider{ width: 1200px; height: 400px; position: relative; overflow: hidden; padding-left: 0; margin: 0 auto; } #slider li{ width: 100%; position: absolute; left: 1200px; top: 0; visibility: hidden; opacity:0; transition: 2s; } #slider li img{ width: 100%; } #slider-buttons ul{ text-align: center; } #slider-buttons ul li{ display: inline-block; width: 8px; height: 8px; border: 3px solid #afafaf; border-radius: 50%; margin: 0 3px; cursor: pointer; } #slider-buttons ul li.active{ background-color: #249cd8; } #slider-buttons ul li:hover{ background-color: #676767; } #effects{ margin-bottom: 30px; } #effects h4, #effects select{ display: inline-block; } #effects select{ min-width: 150px; }
<div class="container"> <h1 class="page-title underline-text">simple slider</h1> <div class="slider-container"> <ul id="slider"> <li><img src="https://raw.githubusercontent.com/rotexhawk/javascript-practice/master/slider-simple/images/slider1.jpg"></li> <li><img src="https://raw.githubusercontent.com/rotexhawk/javascript-practice/master/slider-simple/images/slider2.jpg"></li> <li><img src="https://raw.githubusercontent.com/rotexhawk/javascript-practice/master/slider-simple/images/slider3.jpg"></li> <li><img src="https://raw.githubusercontent.com/rotexhawk/javascript-practice/master/slider-simple/images/slider4.jpg"></li> </ul> <div id="slider-buttons"></div> </div>
Comments
Post a Comment