JQ 元素距离顶部窗口小于0悬浮大于0恢复原来的状态
程序员文章站
2022-03-31 20:33:12
...
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <title>JQ 元素距离顶部窗口小于0悬浮大于0恢复原来的状态</title> <link rel="stylesheet" href="css/base.css" /> <script type="text/javascript" src="js/jquery-1.10.1.min.js"></script> </head> <body> <div style="height: 200px; background: #06f;"></div> <div style="height: 20px; background: #f60; z-index: 999; width: 100%;" class="headcall"></div> <div style="height: 3000px; background: #ccc;"></div> <script type="text/javascript"> $(function(){ headcall(); }); function headcall(){ var dh = $(".headcall"); var dhtop = dh.offset().top; //滚动监听 $(window).scroll(function(){ if ($(window).scrollTop() >= dhtop){ dh.css({'position':'fixed','top':'0','left':'0'});} else{ dh.css({'position':'static','top':'0','left':'0'}); } }); } </script> </body> </html>
效果图:
上一篇: 不是所有冰棒都叫特仑苏