2014-10-03 37 views
0

我正在使用下面的jquery代碼來創建一個慢速滾動動畫。 http://jsfiddle.net/fhj45f21/如何在mouseover上暫停.scrollTop()動畫? (包括jsfiddle)

不幸的是,我在暫停鼠標懸停動畫時遇到了困難。有人可以看看,給我一個提示嗎?

function Scroller(y){ 

    this.times = 0; 
    this.moveInter = 0; 
    this.backInter = 0; 

    this.moveBack = function() { 

     var self = this; 
     clearInterval(this.moveInter); 
     this.backInter = setInterval(function() { 
      self.times -= 5; 
      y.scrollTop(self.times); 
      if (self.times == 0) { 
       return self.startMove(); 
      } 
     }, 1); 
    } 

    this.move = function() { 

     var self = this; 
     this.moveInter = setInterval(function() { 
      self.times++; 
      y.scrollTop(self.times); 
      if (self.times == 1200) { 
       return self.moveBack(); 
      } 
     }, 50); 
    } 

    this.startMove = function() { 
     this.times = 0; 
     var self = this; 
     if (this.backInter != null) { 
      clearInterval(this.backInter); 
     } 
     window.setTimeout(function() { 
      self.move(); 
     }, 1000); 
    } 
} 
jQuery('.textBox').each(function() { 

    y = jQuery(this); 
    var scroller = new Scroller(y); 
    scroller.startMove(); 

}); 

非常感謝!

+0

Ÿ不會停止懸停在滾動,並重新開始? http://api.jquery.com/stop/ – 2014-10-03 10:12:43

回答

2

在這裏你去:http://jsfiddle.net/nxk4vseq/ 與功能添加y.hover處理程序mousein及移出

var scrl=this; 
y.hover(function(){ 
    clearInterval(scrl.moveInter); 
},function(){ 
    scrl.move();  
}); 
+1

非常感謝,這是完美的!很簡單 :) – Gena 2014-10-03 10:39:34