元素随着滚动条滚动而滚动,但是到浏览器窗口顶部的距离是固定的功能代码

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>jquery浮动层</title>
<script src="jquery-1.8.3.js"></script><!-- 注意修改引用路径 -->
<style type="text/css">
#Float {height: 200px; 100px;position: absolute;top: 80px;right: 20px;}
</style>
</head>
<script language="javascript">
  $(document).ready(function(){
   $(window).scroll(function (){
// 让浮动层距离窗口顶部,始终保持80px
   var offsetTop = $(window).scrollTop() + 80 +"px";
   $("#Float").animate({top : offsetTop },{ duration:500 , queue:false });
   });
  });
</script>
<body>
<div style="height:2000px;"></div>
<div id="Float"></div>
</body>
</html>
原文地址:https://www.cnblogs.com/fdxxiaobai/p/6848966.html