Simple realization of js suspension navigation effect

  • 2021-07-16 01:27:19
  • OfStack

In this paper, we share the specific code of js suspension navigation for your reference. The specific contents are as follows


<head>
 <meta charset="UTF-8">
 <title> Suspension navigation </title>
 <style>
  * {
   margin: 0px;
   padding: 0px;
  }
  ul li{
   list-style: none;
  }
  body{
   height: 2000px;
  }
  #top{
   height: 300px;
   width: 100%;
   background-color: red;
  }
  #nav{
   height: 50px;
   line-height: 50px;
   width: 100%;
   background-color: pink; 
  }
  #nav ul{
   width: 1200px;
   margin: 0 auto;
  }
  #nav ul li{
   float: left;
   width: 164px;
   text-align: center;
  }
 </style>
</head>
<body>
 <div id="top">
   Top 
 </div>
 <div id="nav">
  <ul>
   <li> Home page </li>
   <li> Home page </li>
   <li> Home page </li>
   <li> Home page </li>
   <li> Home page </li>
   <li> Home page </li>
   <li> Home page </li>
  </ul>
 </div>
</body>
<script>
 var ad = document.getElementById("nav")
 window.onscroll = function(){
  var _top = document.body.scrollTop || document.documentElement.scrollTop;// Compatible 
  if(_top>=300){
   ad.style.position = "fixed";
   ad.style.top = 0 +"px";
  }else{
   ad.style.position = "absolute";
   ad.style.top = 300+"px";
  }
 }
</script>

Related articles: