jquery 滑鼠滾輪事件 外掛程式 Mousewheel

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   使用   io   strong   

 jquery外掛程式預設是不支援滑鼠中輪滾輪事件的,現在我們可以用於添加跨瀏覽器的滑鼠滾輪支援可以使用jquery的Mousewheel外掛程式。

 使用mousewheel事件有以下兩種方式: 使用mousewheel和unmousewheel事件函數;使用經典的bind和unbind函數。
 JavaScript Code複製內容到剪貼簿
  1. $(‘div.mousewheel_example‘).mousewheel(fn);  
  2. $(‘div.mousewheel_example‘).bind(‘mousewheel‘, fn);  
mousewheel事件的處理函數有一點小小的變化,它除了第一個參數event外,還接收到第二個參數delta。通過參數delta可以擷取滑鼠滾輪的方向和速度。如果delta的值是負的,那麼滾輪就是向下滾動,正的就是向上。 以下是樣本的原始碼:JavaScript Code複製內容到剪貼簿
  1. jQuery(function($) {  
  2.     $(‘div.mousewheel_example‘)  
  3.         .bind(‘mousewheel‘, function(event, delta) {  
  4.             var dir = delta > 0 ? ‘Up‘ : ‘Down‘,  
  5.                 vel = Math.abs(delta);  
  6.             $(this).text(dir + ‘ at a velocity of ‘ + vel);  
  7.             return false;  
  8.         });  
  9. });  
jquery的滑鼠滾輪外掛程式 Mousewheel下載從GitHub下載其他版本。 使用要使用這個功能,只需對目標元素的’mousewheel’事件綁定事件處理函數即可。 Mousewheel外掛程式還提供了兩個事件函數:mousewheel和unmousewheel,可以對目標元素調用這兩個函數,並在參數中指定回呼函數。 事件的回呼函數第一個參數為event,第二個參數為delta,代表滑鼠滾輪的變化值。 以下是上面兩種使用方式的樣本:
 JavaScript Code複製內容到剪貼簿
  1. // 綁定方式  
  2. $(‘#my_elem‘).bind(‘mousewheel‘, function(event, delta) {  
  3.     console.log(delta);  
  4. });  
  5.   
  6. // 事件函數方式  
  7. $(‘#my_elem‘).mousewheel(function(event, delta) {  
  8.     console.log(delta);  
  9. });  

     

    在發生滾輪滾動事件時,如果要設定螢幕滾動高度,就應該使用animate運動設定 scrollTop 來做。直接設定 window 的scrollTop不能達到效果。總會會出多點來。
  10. $(window).on(‘mousewheel‘, function(event, delta) {          //var direction = delta > 0 ? ‘Up‘ : ‘Down‘;        var step = $(window).height();            //可視區高度        var cur_top = $(window).scrollTop();    //當前滾過的高度        var direction = delta > 0 ? -1 : 1;        var height = direction * step + cur_top;        var x_height = Math.floor(height/step)*step;    //滾過整數倍的可視區大小                $("html, body").stop().animate({ scrollTop: x_height }, 400);            return false;      });

     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.