2014-02-27 231 views
0

我想使用鼠標滾輪將圖像從一側移動到另一側。圖像默認設置爲絕對位置,左= 100px。它不允許scroll使用parseInt移動圖像,但是如果我將它移出,它立即移動到left = 0px。我希望能夠將它移動幾個像素點擊每個輪子。使用鼠標滾輪移動圖像

window.onload = function() { 
     if (document.body.addEventListener) { 
       document.body.addEventListener("mousewheel", MouseWheelHandler, false); 
       document.body.addEventListener("DOMMouseScroll", MouseWheelHandler, false); 
     } 
     else document.body.attachEvent("onmousewheel", MouseWheelHandler); 

     function MouseWheelHandler(e){ 
      // cross-browser wheel delta 
     var e = window.event || e; // old IE support 
     var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail))); 

     img1.style.left = Math.max(0, Math.min(1100, parseInt(img1.style.left) + (delta))) + "px"; 

     return false; 
     } 
    }; 
+0

其1100px不是100px的 – user3081171

回答