写了一个非常简单的 jQuery 插件实例 浮动层随窗口滚动滑动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.1.min.js"></script> <script type="text/javascript"> (function($){ $.fn.floatAutoScroll = function(){ this.each(function(){ var obj = $(this); var top = obj.css('top').replace('px', ''); $(window).scroll(function(){ var scrollTop = $(window).scrollTop(); obj.stop().animate({ top:(top*1 + scrollTop*1) }, 'normal'); }); }); } })(jQuery); $(function(){ $('.float-container').floatAutoScroll(); }); </script> <style type="text/css"> .main { height:1500px; } .float-container { position:absolute; right:0; width:50px; height:50px; background-color:#960; } .float1 { top:100px; } .float2 { top:200px; } .float3 { top:300px; } </style> </head> <body> <div class="main"></div> <div class="float-container float1">浮动框1</div> <div class="float-container float2">浮动框2</div> <div class="float-container float3">浮动框3</div> </body> </html>