js获取滚动条位置鼠标坐标

2010年02月19日  |  11:14分类:长沙网站开发  |  标签:  |  2,049 views

获取鼠标坐标: IE (event.clientX event.clientY)

获取滚动条位置:

     document.body.scrollTop (滚动条离页面最上方的距离)

     document.body.scrollLeft   (滚动条离页面最左方的距离)

当用js获取当前垂直或者水平方向滚动条位置的时候,使用”document.body.scrollTop”或者”document.body.scrollLeft”是无效的,得到的数值永远是0。不过,当写在“onscroll”事件里面的时候,上述方法可以获得当前滚动条的位置。

当网页最前面有以下内容时:

<! DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>

     document.documentElement.scrollTop (滚动条离页面最上方的距离)

     document.documentElement.scrollLeft   (滚动条离页面最左方的距离)

所以为了准确取得当前滚动条的位置,正确的使用方法是:

      document.documentElement.scrollTop:垂直方向
document.documentElement.scrollLeft:水平方向

喜欢本文,那就收藏到: Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网

2条评论 关于 “js获取滚动条位置鼠标坐标”

  1. 右边跟随滚动条滚动漂浮广告 - 长沙网站设计,长沙网站开发-小龙包 发表于: 二月 19th, 2010 11:15

    [...] 关于 上一篇:zen-cart修改后台目录名 下一篇:js获取滚动条位置鼠标坐标 [...]

  2. Yahoouj 发表于: 二月 23rd, 2010 12:14

    Really good work about this website was done. Keep trying more – thanks!


发表您的评论

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word