2011-12-09 108 views
2

當DIV圖層的滾動條被手動滾動或向下滾動時,是否可以編寫JavaScript來執行操作?向上滾動或向下滾動的JavaScript事件

如果是這樣,請給我一個提示,以實現一個簡單的警告框,說你滾動和你向下滾動。

+0

這會幫助你:http://www.adomas.org/javascript -mouse-wheel/ –

回答

7

你可以簡單的使用onscroll事件的Java腳本。

OnScroll事件參考http://www.w3schools.com/jquery/event_scroll.asp

這裏是例子,

<head> 
    <script type="text/javascript"> 
     function OnScrollDiv (div) { 
      var info = document.getElementById ("info"); 
      info.innerHTML = "Horizontal: " + div.scrollLeft 
          + "px<br/>Vertical: " + div.scrollTop + "px"; 
     } 
    </script> 
</head> 
<body> 
    <div style="width:200px;height:200px; overflow:auto;" onscroll="OnScrollDiv (this)"> 
     Please scroll this field! 
     <div style="height:300px; width:2000px; background-color:#a08080;"></div> 
     Please scroll this field! 
     <div style="height:300px; width:2000px; background-color:#a08080;"></div> 
     Please scroll this field! 
    </div> 
    <br /><br /> 
    Current scroll amounts: 
    <div id="info"></div> 
</body> 

工作複製到這裏:http://jsbin.com/iledat/2/edit

-3

嘗試jquery scroll event

$('div').scroll(function(){alert('scrolled!')}) 
+0

這不會告訴你,如果你已經上漲或下跌。 – jabclab

+6

'-1',jQuery在此問題中未被標記。 –

相關問題