2012-06-12 32 views
1

我可以銷燬這個函數或mousemove事件嗎?我可以銷燬這個函數或mousemove事件嗎?

我在jQuery中有一些代碼讓我頭疼。我在網上發現了這個代碼,當我傳遞元素時,我服務於鼠標移動效果,但是想知道當我離開他時怎樣才能補充或刪除事件。

一個例子,我有如下:

$("#icons").mouseenter(function(e) { 
    $(this).find('div').slidemouse(); 
}); 

$("#icons").mouseleave(function(e) { 
    $(this).find('div').slidemouse('destroy'); 
}); 

代碼插件:

(function($) { 
    $.fn.slidemouse=function(options) { 
     var defaults= 
      { 
      height:'240px', 
      widthExpand:true, 
      mirror:false, 
      mirrorOpacity:.3 
     }; 
     var opts=$.extend(defaults,options); 
     var expands=1; 
     var galleryWidth=0; 
     var self=this; 
     self.css('overflow','hidden'); 
     self.children().css('height',opts.height); 
     self.children().children().each(function(index) { 
      galleryWidth=galleryWidth+$(this).outerWidth(true) 
     } 
     ); 
     if(opts.widthExpand) { 
      while(galleryWidth<self.width()) { 
       self.children().children(":nth-child("+expands+")").clone().appendTo(self.children()); 
       galleryWidth=galleryWidth+self.children().children(":nth-child("+expands+")").outerWidth(true); 
       expands++ 
      } 
     } 
     self.children().css("width",galleryWidth); 
     if(opts.mirror) { 
      self.clone().insertAfter(self).attr("id",""); 
      self.next().fadeTo(0,opts.mirrorOpacity) 
     } 
     if(opts.widthExpand||opts.mirror) { 
      $(window).bind("resize",resizeWindow) 
     } 
     function resizeWindow() { 
      if(opts.widthExpand) { 
       galleryWidth=0; 
       self.children().children().each(function(index) { 
        galleryWidth=galleryWidth+$(this).outerWidth(true) 
       }); 
       while(galleryWidth<self.width()) { 
        self.children().children(":nth-child("+expands+")").clone().appendTo(self.children()); 
        galleryWidth=galleryWidth+self.children().children(":nth-child("+expands+")").outerWidth(true); 
        expands++ 
       } 
       self.children().css("width",galleryWidth); 
       if(opts.mirror) { 
        self.next().remove(); 
        self.clone().insertAfter(self).attr("id",""); 
        self.next().fadeTo(0,opts.mirrorOpacity) 
       } 
      } 
     } 
     $(this).parent().mousemove(function(e) { 
      var x=e.pageX-this.offsetLeft; 
      var calc=(self.children().width()-self.width())/self.width(); 
      var left=x*calc; 
      var right=(self.width()-x)*calc; 
      self.stop().animate({scrollLeft: left}, 500); 
      if(opts.mirror) { 
       self.next().stop().animate({scrollLeft: right}, 500); 
      } 
     }) 
    } 
} 
)(jQuery); 
+2

我恐怕這裏的前兩段在英文中沒有太大意義。你會編輯你的問題來解釋一下這個問題,或者讓別人來幫助你改正它嗎?目前尚不清楚問題是什麼。 – halfer

+0

如果你原諒我的英語,因爲我講西班牙語 –

回答

1

這個插件有你在你的代碼所需要的特定的任務沒有方法。你可以只綁定/解除綁定事件以防止插件的例程被觸發。適當時使用$(this).unbind('event')。作爲替代方案,你可以嘗試通過添加破壞標誌,它修改插件的代碼:

var defaults= 
     { 
     height:'240px', 
     widthExpand:true, 
     mirror:false, 
     mirrorOpacity:.3, 
     destroy: false 
}; 

var opts=$.extend(defaults,options); 
var self=this; 

if(destroy) { 

self.stop(); 
self.children().stop(); 

}

現在你可以使用新的選項上的鼠標離開爲:

$(this).slidemouse({destroy: true}); 
+0

如果我工作的想法,稍作修改運行的代碼,謝謝你,請原諒,我的英語,因爲我講西班牙語 如果(opts.destroy){ \t opts.destroy (選擇採用); \t self.stop(); \t self.children()。stop(); } –

相關問題