爱程序网

如何获取滚动向下滚动的距离

来源: 阅读:

如何获取滚动向下滚动的距离:
在实际应用中,有时候需要获得滚动条顶部距离窗口顶部的距离,在运动框架中经常用到,下面简单介绍一下如何实现此功能。
代码实例如下:

 

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>IE6下实现position:fixed-蚂蚁部落</title>
<style type="text/css">
*{
  margin:0px;
  padding:0px;
}
body{height:1000px;}
#thediv{
  width:100px;
  height:100px;
  background-color:#CCC;
  position:fixed;
  _margin-top:20px;
  _position:absolute;
  _top:expression(eval(document.documentElement.scrollTop));
  left:20px;
  top:20px;
  text-align:center;
  line-height:100px;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){ 
  $(window).scroll(function(){ 
    $("#thediv").text($(window).scrollTop());
  })
}); 
</script>
</head>
<body>
<div id="thediv"></div>
</body>
</html>

 

代码当向下拖动滚动条的时候,能够在灰色的div中实时的显示向下拖动的距离,代码非常的简单,这里就不多介绍,可以参阅相关阅读。
相关阅读:
1.scroll事件可以参阅jQuery的scroll事件一章节。
2.scrollTop()函数可以参阅jQuery的scrollTop()方法一章节。

原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=8930

更多内容可以参阅:http://www.softwhy.com/jquery/

 

关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助