javascriptcssdom-eventsmobile-webkit

Continuous JavaScript Scroller


Continuous JavaScript Scroller

I have below script which is working on next and previous click. There are about 1 to 3 div for which I have added this JavaScript to flow from left to right and vice versa upon the next and previous button action.

I want to make it continuous scrolling. After third div scroller should again navigate to first div. It should not go in a reverse direction. Flow should be continuous again and again.

Please advise on the solution to achieve this.

<div id="mainContainer"> 
<!--mainContainer Div Start-->
<div class="arrowRight"><img src="images/arrow_left.png" width="36" height="39"></div> 
<div class="arrowLeft"><img src="images/arrow_right_disable.png" width="36" height="39">   </div>
  <div class="slider">
    <div class="slides"> <img src="images/1.jpg" width="250" height="250"></div>
    <div class="slides"> <img src="images/2.jpg" width="250" height="250"></div>
    <div class="slides"><img src="images/3.jpg" width="250" height="250"> </div>
  </div>
  <!--mainContainer Div End--> 
</div>

js

window.onload=init;

function init(){
    
var arrowLeft=document.querySelector('.arrowLeft');     
var arrowRight=document.querySelector('.arrowRight');       
arrowRight.addEventListener('click',slideRight,false);
arrowLeft.addEventListener('click',slideLeft,false);

  var flag=1;
  function slideRight(){
    if(flag<3){ 
    flag++; 
    arrowLeft_div.innerHTML="<img src='images/arrow_right.png' width='36' height='39'>";    
    var slider=document.querySelector('.slider');
    slider.style.webkitTransform+='translate(-1024px, 0px)';
   }
    if(flag==3){
    arrowRight_div.innerHTML="<img src='images/arrow_left_disable.png' width='36'    
    height='39'>";  
   }
}


  function slideLeft(){
    if(flag>1){
    flag--; 
    arrowRight_div.innerHTML="<img src='images/arrow_left.png' width='36' height='39'>";    
    var slider=document.querySelector('.slider');
    slider.style.webkitTransform+='translate(1024px, 0px)';
   }
   if(flag==1){
   arrowLeft_div.innerHTML="<img src='images/arrow_right_disable.png' width='36'  
   height='39'>";   
    
  }
 }

}

Solution

  • Something like this?

    var flag=1;
    function slideRight(){
        if(flag<3){ 
        flag++; 
        arrowLeft_div.innerHTML="<img src='images/arrow_right.png' width='36' height='39'>";    
        var slider=document.querySelector('.slider');
        slider.style.webkitTransform+='translate(-1024px, 0px)';
    }
        if(flag==3){
        arrowLeft_div.innerHTML="<img src='images/arrow_right.png' width='36' height='39'>";    
        var slider=document.querySelector('.slider');
        slider.style.webkitTransform+='translate(2048px, 0px)'; 
        flag = 1;
    }