JavaScript定位导航滚动2

Posted 前端HL

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript定位导航滚动2相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title>网页定位导航效果</title>
  <style type="text/css">
    *{
      margin: 0;
      padding: 0;
    }
    body{
      font-size: 12px;
      line-height: 1.7;
    }
    li{
      list-style: none;
    }
    #content{
      width: 800px;
      margin: 0 auto;
      padding: 20px;
    }
    #content h1{
      color: #0088bb;
    }
    #content .item{
      padding: 20px;
      margin-bottom: 20px;
      border: 1px dotted #0088bb;
    }
    #content .item h2{
      font-size: 12px;
      font-weight: bold;
      border-bottom: 2px solid #0088bb;
      margin-bottom: 10px;
    }
    #content .item li{
      display: inline;
      margin-left:10px ;
    }
    #content .item li a img{
      width: 230px;
      height: 230px;
      border: none;
    }
    #menu{
      position: fixed;
      top: 100px;
      left: 50%;
      margin-left: 400px;
      width: 80px;
    }
    #menu ul li a{
      display: block;
      margin: 5px 0;
      font-size: 14px;
      font-weight: bold;
      color: #333;
      width: 80px;
      height: 50px;
      line-height: 50px;
      text-align: center;
      text-decoration: none;
    }
    #menu ul li a:hover{
      color: #fff;
      background: #0088bb;
    }
    #menu ul li .current{
      color: #fff;
      background: #0088bb;
    }
  </style>
  <script type="text/javascript">
    window.onload = function(){
      window.onscroll=function(){
        var top = document.documentElement.scrollTop || document.body.scrollTop;
        var menus = document.getElementById("menu").getElementsByTagName("a");
        var items=document.getElementById("content").getElementsByClassName("item");
 
        var currentId="";
        for(var i=0;i<items.length;i++){
          var _item=items[i];
          var _itemTop = _item.offsetTop;
          if(top>_itemTop - 200){
            currentId=_item.id;
          }else{
            break;
          }
        }
        if(currentId!=""){
          //给正确的menu下的a元素class赋值
          for(var j=0;j<menus.length;j++){
            var _menu=menus[j];
            var _href=_menu.href.split("#");//因为只通过href获取的链接为一长串链接,需要通过#分成数组
            if(_href[_href.length-1]!=currentId){
              _menu.className = "";
            }else{
              _menu.className = " current";
            }
          }
        }
      }
    }
  </script>
</head>
<body>
<div id="menu">
  <ul>
    <li><a href="#item1" class="current">1F 男装</a></li>
    <li><a href="#item2">2F 女装</a></li>
    <li><a href="#item3">3F 美妆</a></li>
    <li><a href="#item4">4F 数码</a></li>
    <li><a href="#item5">5F 母婴</a></li>
  </ul>
</div>
<div id="content">
  <div id="item1" class="item">
    <h2>1F 男装</h2>
    <ul>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
    </ul>
  </div>
  <div id="item2" class="item">
    <h2>2F 女装</h2>
    <ul>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
    </ul>
  </div>
  <div id="item3" class="item">
    <h2>3F 美妆</h2>
    <ul>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><a href="#"><img src="http://img20.360buyimg.com/babel/s340x420_jfs/t15313/103/1889006795/27939/ac735b0f/5a619794N40987404.jpg!q90!cc_340x420" alt=""/></a></li>
      <li><以上是关于JavaScript定位导航滚动2的主要内容,如果未能解决你的问题,请参考以下文章

Javascript页面滚动时导航智能定位

Android Jetpack 导航禁用滚动位置

jQuery用jQuery实现定位滚动导航效果

如何在滚动列表视图上显示/隐藏底部导航视图?

滚动经过片段时,Angular 10会获得路由器活动片段吗?

没有滚动链接定位的当前导航位置