2012-02-01 148 views
2

我有以下的html页面。当我滚动鼠标时,为什么警告不起作用?jQuery滚动绑定不起作用

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    <html lang="en"> 
    <head> 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 


     <script> 
     $(window).scroll(function(){ 
       alert("scrolling"); 
      }); 
     </script> 
    </head> 
    <body> 

    </body> 
    </html> 

编辑:它的工作原理当页面右侧的滚动条和滚动页面向下移动。但是我希望它在空白页面上工作,例如。

EDIT2:我已将document.body更改为窗口,但具有相同的问题。

EDIT3:那么,我来结果是我要赶鼠标滚轮事件

+1

你是否尝试绑定到身体以及HTML? $(“body,html”) – 2012-02-01 09:23:39

+0

我试过了,没有结果 – 2012-02-01 09:26:59

+0

你应该如何滚动空白页? % - ( – 2012-02-01 09:42:26

回答

4

它不是身体,而是窗户。
但是我会在文档准备好之后再把它放进去。
这里有一个工作代码:

$(document).ready(function() 
    { 
     console.log('ready'); 
     $(window).scroll(function() 
     { 
      console.log('scroll'); 
     }); 
    }); 

在回答您的编辑3
赶上鼠标滚轮事件中使用jQuery的mousewheel_plugin看到这个example
所以,你的代码将

<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript" src="jQuery_mousewheel_plugin.js"></script> 
<script> 

    var intOverallDelta = 0; 

    $(document).ready(function() 
    { 
     console.log('ready'); 
     $(document.body).mousewheel(function(objEvent, intDelta) 
     { 
      if (intDelta > 0) 
      { 
       intOverallDelta++; 
       console.log('up - (' + intOverallDelta + ')'); 
      } 
      else if (intDelta < 0) 
      { 
       intOverallDelta--; 
       console.log('down - (' + intOverallDelta + ')'); 
      } 
     }); 
    }); 

</script> 
+0

它在空白页面上不起作用。请参阅edit1 – 2012-02-01 09:39:32

+0

你应该如何滚动空白页面?% - ( – 2012-02-01 09:43:54

+0

我已回答你关于问题的意见 – 2012-02-01 09:46:27

2

试试它的窗口

$(window).scroll(function(){ 
    alert("scrolling"); 
}); 

阅读手册:http://api.jquery.com/scroll/

+0

...这里是一个例子:http://jsfiddle.net/Dutgp/1/ – Niklas 2012-02-01 09:33:15

+0

删除高度:1000px;样式和这个例子将不起作用 查看编辑! – 2012-02-01 09:35:05

1

你的选择是错误的,document.body不会匹配任何东西...

+0

查看编辑请 – 2012-02-01 09:38:07

+0

这不是真的:document.body存在于jquery中,只需尝试'$(document.body).click(function(){console.log('click');});'...事实是,按照这个问题的要求,你需要使用'window'而不是 – 2012-02-01 09:40:01