2014-05-07 67 views
0

我有一個在第一次運行時運行良好的左側滾動動畫。 如果點擊該按鈕,則滾動將動畫到最右邊的區域,然後返回到原始位置。 但是當再次點擊該按鈕時,內容滾動到最右側,滾動回到原始位置。 我不明白是什麼問題。請幫忙。提前致謝。ScrollLeft動畫在第二次嘗試時失敗

小提琴:http://jsfiddle.net/tXt3T/

HTML:

<div class="main"> 
    <div class="content"> 
     <p> 
      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 
     </p> 
     <p> 
      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 
     </p> 
    </div> 
</div> 
<button id="button">Scroll</button> 

CSS:

* { margin: 0 auto; padding: 0;} .clearfix {clear:both;} 
    .wrapper { width: 100%;} 
    .main { width: 100%; height: 300px; position: relative; overflow: scroll;} 
    .content { width: 200%; position: absolute; left: -500;} 

JS:

var scrollLeftInt, scrollRightInt, scrollTarget,  
     scrollToLeft = function(t) {     
      if($(".main").scrollLeft() < scrollTarget) { 
       $(".main").scrollLeft($(".main").scrollLeft() + (scrollTarget*0.01));     
      } 
      else { 
       clearInterval(scrollLeftInt); 
       $(".main").scrollLeft(scrollTarget); 
       scrollRightInt = setInterval(function() {scrollToRight(t);}, t); 
      } 
     }, 
     scrollToRight = function() { 
      if ($(".main").scrollLeft() > 0) { 
       $(".main").scrollLeft($(".main").scrollLeft() - (scrollTarget*0.01)); 
      } 
      else {     
       clearInterval(scrollRightInt); 
       $("#button").removeAttr('disabled'); 
       $(".main").scrollLeft(0); 
       scrollLeftInt = ""; scrollRightInt =""; 
       console.log(scrollRightInt); 
      } 
     }; 

    $(function() { 
     console.log("Script File is working"); 
     $("#button").click(function() { 
      var t = 10; 
      $(this).attr('disabled', true); 
      $(".main").scrollLeft(scrollTarget); 
      scrollTarget = $(".content").width() - $(".content").parent().width() ;      
      scrollLeftInt = setInterval(function() {scrollToLeft(t);}, t);  
     });   
    }); 

回答

1

只是刪除這一行,一旦你不初始化和它的undefined

$("#button").click(function() { 
     var t = 10; 
     $(this).attr('disabled', true); 
     // $(".main").scrollLeft(scrollTarget); 
     scrollTarget = $(".content").width() - $(".content").parent().width() ;      
     scrollLeftInt = setInterval(function() {scrollToLeft(t);}, t);  
    }); 

http://jsfiddle.net/tXt3T/2/

相關問題