<!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>The JQ element is less than 0 suspended from the top window and greater than 0 to restore the original state</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; //scroll monitor $(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>
Effect picture: