网页定位导航

时间:2021-06-22 09:09:09

使用jQuery实现

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>地狗购物网-网页定位导航效果</title>
    <style>
        * {
            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: 16px;
            font-weight: bold;
            border-bottom: 2px solid #0088bb;
            margin-bottom: 10px;
        }

        #content .item li {
            display: inline;
            margin-right: 10px;
        }

        #content .item li a img {
            width: 230px;
            height: 230px;
            border: none;
        }

        #menu {
            position: fixed;
            top: 100px;
            left: 50%;
            margin-left: 400px;
            width: 50px;
        }

        #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-decoration: none;
            text-align: center;
        }

        #menu ul li a:hover,
        #menu ul li a.current {
            color: #fff;
            background: #0088bb;
        }

        * html, * html body {
            background-image: url(about:blank);
            background-attachment: fixed;
        }

        * html #menu {
            position: absolute;
            bottom: auto;
            top: expression(100+((e=document.documentElement.scrollTop)?e:document.body.scrollTop)+'px');
        }
    </style>
    <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $(window).scroll(function () {
                var items = $("#content").find(".item");
                var menu = $("#menu");
                var top = $(document).scrollTop();
                var currentId = ""; //滚动条现在所在位置的item id
                items.each(function () {
                    var m = $(this);
                    //注意:m.offset().top代表每一个item的顶部位置
                    if (top > m.offset().top - 300) {
                        currentId = "#" + m.attr("id");
                    } else {
                        return false;
                    }
                });

                var currentLink = menu.find(".current");
                if (currentId && currentLink.attr("href") != currentId) {
                    currentLink.removeClass("current");
                    menu.find("[href=" + currentId + "]").addClass("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">
    <h1>地狗购物网</h1>

    <div id="item1" class="item">
        <h2>1F 男装</h2>

        <ul>
            <li><a href="#"><img src="1F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="1F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="1F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="1F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="1F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="1F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="1F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="1F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="1F.jpg" alt=""/></a></li>
        </ul>
    </div>
    <div id="item2" class="item">
        <h2>2F 女装</h2>
        <ul>
            <li><a href="#"><img src="2F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="2F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="2F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="2F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="2F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="2F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="2F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="2F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="2F.jpg" alt=""/></a></li>
        </ul>
    </div>
    <div id="item3" class="item">
        <h2>3F 美妆</h2>

        <ul>
            <li><a href="#"><img src="3F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="3F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="3F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="3F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="3F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="3F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="3F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="3F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="3F.jpg" alt=""/></a></li>
        </ul>
    </div>
    <div id="item4" class="item">
        <h2>4F 数码</h2>

        <ul>
            <li><a href="#"><img src="4F.png" alt=""/></a></li>
            <li><a href="#"><img src="4F.png" alt=""/></a></li>
            <li><a href="#"><img src="4F.png" alt=""/></a></li>
            <li><a href="#"><img src="4F.png" alt=""/></a></li>
            <li><a href="#"><img src="4F.png" alt=""/></a></li>
            <li><a href="#"><img src="4F.png" alt=""/></a></li>
            <li><a href="#"><img src="4F.png" alt=""/></a></li>
            <li><a href="#"><img src="4F.png" alt=""/></a></li>
            <li><a href="#"><img src="4F.png" alt=""/></a></li>
        </ul>
    </div>
    <div id="item5" class="item">
        <h2>5F 母婴</h2>

        <ul>
            <li><a href="#"><img src="5F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="5F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="5F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="5F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="5F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="5F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="5F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="5F.jpg" alt=""/></a></li>
            <li><a href="#"><img src="5F.jpg" alt=""/></a></li>
        </ul>
    </div>
</div>

</body>
</html>

使用JavaScript实现

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>地狗购物网——网页定位导航效果</title>
<style>
        * {
            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: 16px;
            font-weight: bold;
            border-bottom: 2px solid #0088bb;
            margin-bottom: 10px;
        }

        #content .item li {
            display: inline;
            margin-right: 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-decoration: none;
            text-align: center;
        }

        #menu ul li a:hover,
        #menu ul li a.current {
            color: #fff;
            background: #0088bb;
        }

        /*ie6 hack*/
        * html, * html body {
            background-image: url(about:blank);
            background-attachment: fixed;
        }

        * html #menu {
            /*position: fixed;*/
            position: absolute;
            top: expression(((e=document.documentElement.scrollTop)?e:document.body.scrollTop)+100+'px');
        }

</style>
<script>
    
 //定义getByClassName函数,让函数实现根据class name获取对象并返回
    function getByClassName(obj, cls){
        var elements = obj.getElementsByTagName("*");
        var result = [];
        for(var i = 0; i < elements.length; i++){
            if(elements[i].className == cls){
                result.push(elements[i]);
            }
        }
        return result;
    }
    
	function hasClass( obj, cls ){ 
	    return obj.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
	}
	
	function removeClass( obj, cls ){ 
	    if( hasClass( obj, cls )){ 
		    //remove
			var reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
			obj.className = obj.className.replace(reg, "");
		}
	}
   
    //定义getByClassName函数,让函数实现给对象增加class 
    function addClass(obj, cls){
        if(!hasClass(obj, cls)){
            obj.className += " " + cls;
        }
    }
	
	window.onload = function(){ 
		
	    window.onscroll = function(){ 		
		    var top = document.documentElement.scrollTop || document.body.scrollTop;
			
			var menus = document.getElementById("menu").getElementsByTagName("a");
			
			var items = getByClassName(document.getElementById("content"), "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;
				}			
			}
		
			 				
      //请补充此处代码,给正确的menu下的a元素class赋值current
           if(currentId){
               for(var j = 0; j < menus.length; j++){
                   var _menu = menus[j];
                   var _href = _menu.href.split("#");
                   if(_href[_href.length - 1] != currentId){
                       removeClass(_menu, "current");
                   } else {
                       addClass(_menu, "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">
<h1>地狗购物网</h1>

<div id="item1" class="item">
<h2>1F 男装</h2>
<ul>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
</ul>
</div>
<div id="item2" class="item">
<h2>2F 女装</h2>
<ul>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/5366104c0001e5c403990422.jpg" alt=""/></a></li>
</ul>
</div>
<div id="item3" class="item">
<h2>3F 美妆</h2>
<ul>
<li><a href="#"><img src="http://img.mukewang.com/536610820001cf5b03990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610820001cf5b03990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610820001cf5b03990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610820001cf5b03990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610820001cf5b03990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610820001cf5b03990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610820001cf5b03990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610820001cf5b03990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610820001cf5b03990422.jpg" alt=""/></a></li>
</ul>
</div>
<div id="item4" class="item">
<h2>4F 数码</h2>
<ul>
<li><a href="#"><img src="http://img.mukewang.com/536610ac00012df703990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610ac00012df703990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610ac00012df703990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610ac00012df703990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610ac00012df703990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610ac00012df703990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610ac00012df703990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610ac00012df703990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610ac00012df703990422.jpg" alt=""/></a></li>
</ul>
</div>
<div id="item5" class="item">
<h2>5F 母婴</h2>
<ul>
<li><a href="#"><img src="http://img.mukewang.com/536610cd0001f34603990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610cd0001f34603990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610cd0001f34603990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610cd0001f34603990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610cd0001f34603990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610cd0001f34603990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610cd0001f34603990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610cd0001f34603990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/536610cd0001f34603990422.jpg" alt=""/></a></li>
</ul>
</div>
</div>
</body>
</html>


知识点:

快捷键:ul>li*5>a 按tab键
按住ALT键,就可以竖向选择
jQuery中通过id筛选永远比用class筛选的效率要高的多所以通过$("#").find()更有效率
$(window).scroll()滚动条滚动的时候
$(document).scrollTop()获取滚动条距离页面顶端的距离
items是所有item的集合
each遍历所有item
m.offset().top是获取所有item距离顶端的距离
然后当前滚动条依次与item的距离比较
console.Log(元素)在控制台看该元素的值
each遍历item所以this的值也在变化

currentId获取当前的ID,return false跳出循环


效果图

网页定位导航