javascript - Translate an image slider coded in jQuery to pure JS -


i found tutorial image slider made jquery. wanting recreate same exact functionality in pure js, facing problems translating functionality of animate function of jquery pure js.

here code it. (the problem facing opacity shifting)

html: (from tutorial)

<!doctype html> <html>   <head>     <title>jquery slider demo</title>     <script src="jquery-2.1.3.min.js"  type="text/javascript" ></script>     <script type="text/javascript" src="script.js"></script>     <link rel="stylesheet" type="text/css" href="style.css">   </head>   <body>     <div class="slides">       <img src="img/1.jpg" />       <img src="img/2.jpg" />       <img src="img/3.jpg" />       <img src="img/4.jpg" />     </div>   </body> </html> 

js (from tutorial):

$(function(){   // initalizing first image class 'top'   $('.slides img:first').addclass('top');   //function alter image index changing class   var change = function (){     var curr = $('.slides img.top');     var next = curr.next();       // if next image not available      // set class-top first image     // vanish current image     // show previous next image     if(!next.length){        next = $('.slides img:first');       next.addclass('top');        curr.animate({opacity: 0.0},1000, function() {         curr.removeclass('top');         curr.css({opacity: 1.0});       });      }else{        // pick next image       // set opacity 0       // den use animation make appear       // above top image       next.css({opacity: 0.0})         .addclass('top')         .animate({opacity: 1.0}, 1000, function() {         curr.removeclass('top');       });     }   }    // repeat function execution every 3 secs   setinterval(change, 3000 );   }); 

what have far:

(function(){    var list = document.getelementsbytagname('img');    (var i=0; i<list.length; i++ ) {     list[i].addeventlistener('transitionend', function(){       this.style.opacity = "1";       this.classlist.remove('top');     })   };    list[0].classlist.add('top');    var change = function () {     var curr = document.queryselector('img.top');     var next = curr.nextelementsibling;      if(next == undefined) {       next = list[0]       next.classlist.add('top');       curr.style.opacity = "0";     }      else {       curr.style.opacity = "0";       console.log('i working')       next.classlist.add('top');     }   }    setinterval(change,3000);  })(); 

as mentionned in comment below question, best way have nice fading effect, without jquery, css.

function slider(){      var list = document.getelementsbytagname('img');      list[0].classlist.add('top');      var change = function () {      console.log('i working');          var curr = document.queryselector('img.top');      var next = curr.nextelementsibling;        if(next == undefined) {        next = list[0]      }            curr.classlist.remove('top');      next.classlist.add('top');    }      setinterval(change,3000);  }
.slides img {      /* demo slider run slider... */    position:absolute;    top:0;    left:0;    width:100%;        /* css fading */    -webkit-transition: 1s linear;    opacity: 0;  }  .slides img.top {    opacity: 1;  }
<body onload="slider();">    <div class="slides">      <img src="https://lorempixel.com/400/200/sports/image 1" />      <img src="https://lorempixel.com/400/200/sports/image 2" />      <img src="https://lorempixel.com/400/200/sports/image 3" />      <img src="https://lorempixel.com/400/200/sports/image 4" />    </div>  </body>

codepen


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -