<!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> <title>scroll Demo</title> <style> body { margin:0px; padding:0px; height:900px; } #tip { position:absolute; right:0px; width:200px; height:200px; border:1px solid red; } </style> <script src="../js/jquery-1.7.2.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function(){ var tipObj = $("#tip"); var tipHeight = tipObj.height(); //可视化高度 var clientHeight = $(window).height(); var height = clientHeight-tipHeight; //window scroll事件 $(window).scroll(function(){ //alert("window scroll事件!!!"); var scrollTop = $(this).scrollTop(); var currentHeight = (height + scrollTop)/2; tipObj.css("top",currentHeight+"px"); }).trigger("scroll"); }); </script> </head> <body> <div id="tip"> </div> </body> </html>
w3c scroll事件:
http://www.w3school.com.cn/tiy/t.asp?f=jquery_event_scroll
时间: 2024-11-06 01:23:42