• jq实现滚动定位


    直接上代码

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <title>爱淘气购物网-JQuery网页定位导航特效</title>
        <style>
        /*All tag*/
    
        html,
        body,
        div,
        ul,
        li,
        img,
        h1,
        h2 {
            margin: 0;
            padding: 0;
        }
    
        ul {
            list-style: none;
        }
    
        a {
            text-decoration: none;
            color: #333;
        }
        /*menu*/
    
        #menu {
            position: fixed;
            top: 100px;
            left: 50%;
            margin-left: 400px;
        }
    
        #menu>li>a {
            display: block;
            padding: 10px 15px;
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            font-weight: bold;
            margin: 5px 0;
        }
    
        #menu>li>a:hover,
        #menu>li>a.current {
            color: #fff;
            background-color: #0088bb;
        }
        /*content*/
    
        #content {
             800px;
            margin: 30px auto;
        }
    
        h1 {
            color: #0088bb;
            font-size: 30px;
            margin-bottom: 10px;
        }
    
        .item {
            border: 1px dotted #0088bb;
            padding: 20px;
            margin-bottom: 20px;
        }
    
        .item>h2 {
            font-size: 16px;
            padding-bottom: 5px;
            border-bottom: 2px solid #0088bb;
            margin-bottom: 10px;
        }
    
        .item-list>li {
            display: inline-block;
            margin-right: 10px;
        }
    
        .item-list>li>a>img {
             230px;
            height: 230px;
            border: none;
        }
        /* 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>
    </head>
    
    <body>
        <ul id="menu">
            <li><a class="current" href="#item1">1F&nbsp;男装</a></li>
            <li><a href="#item2">2F&nbsp;女装</a></li>
            <li><a href="#item3">3F&nbsp;美妆</a></li>
            <li><a href="#item4">4F&nbsp;数码</a></li>
            <li><a href="#item5">5F&nbsp;母婴</a></li>
        </ul>
        <div id="content">
            <h1>爱淘气购物网</h1>
            <div id="item1" class="item">
                <h2>1F&nbsp;男装</h2>
                <ul class="item-list">
                    <li>
                        <a href="#"><img src="http://p1.qhimgs4.com/dmfd/160_90_/t01a8d3c891c7c3e202.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p1.qhimgs4.com/dmfd/160_90_/t01a8d3c891c7c3e202.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p1.qhimgs4.com/dmfd/160_90_/t01a8d3c891c7c3e202.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p1.qhimgs4.com/dmfd/160_90_/t01a8d3c891c7c3e202.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p1.qhimgs4.com/dmfd/160_90_/t01a8d3c891c7c3e202.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p1.qhimgs4.com/dmfd/160_90_/t01a8d3c891c7c3e202.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p1.qhimgs4.com/dmfd/160_90_/t01a8d3c891c7c3e202.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p1.qhimgs4.com/dmfd/160_90_/t01a8d3c891c7c3e202.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p1.qhimgs4.com/dmfd/160_90_/t01a8d3c891c7c3e202.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                </ul>
            </div>
            <div id="item2" class="item">
                <h2>2F&nbsp;女装</h2>
                <ul class="item-list">
                    <li>
                        <a href="#"><img src="http://p2.qhimgs4.com/dmfd/160_90_/t01c5e14034226e1d97.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p2.qhimgs4.com/dmfd/160_90_/t01c5e14034226e1d97.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p2.qhimgs4.com/dmfd/160_90_/t01c5e14034226e1d97.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p2.qhimgs4.com/dmfd/160_90_/t01c5e14034226e1d97.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p2.qhimgs4.com/dmfd/160_90_/t01c5e14034226e1d97.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p2.qhimgs4.com/dmfd/160_90_/t01c5e14034226e1d97.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p2.qhimgs4.com/dmfd/160_90_/t01c5e14034226e1d97.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p2.qhimgs4.com/dmfd/160_90_/t01c5e14034226e1d97.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p2.qhimgs4.com/dmfd/160_90_/t01c5e14034226e1d97.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                </ul>
            </div>
            <div id="item3" class="item">
                <h2>3F&nbsp;美妆</h2>
                <ul class="item-list">
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t017b1f5993c08af904.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t017b1f5993c08af904.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t017b1f5993c08af904.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t017b1f5993c08af904.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t017b1f5993c08af904.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t017b1f5993c08af904.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t017b1f5993c08af904.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t017b1f5993c08af904.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t017b1f5993c08af904.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                </ul>
            </div>
            <div id="item4" class="item">
                <h2>4F&nbsp;数码</h2>
                <ul class="item-list">
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t014922d8477716f299.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t014922d8477716f299.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t014922d8477716f299.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t014922d8477716f299.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t014922d8477716f299.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t014922d8477716f299.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t014922d8477716f299.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t014922d8477716f299.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p0.qhimgs4.com/dmfd/160_90_/t014922d8477716f299.jpg?size=640x400" alt="商品图片"></a>
                    </li>
                </ul>
            </div>
            <div id="item5" class="item">
                <h2>5F&nbsp;母婴</h2>
                <ul class="item-list">
                    <li>
                        <a href="#"><img src="http://p4.qhimg.com/dmfd/160_90_/t0100eb94d000a1b3c9.jpg" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p4.qhimg.com/dmfd/160_90_/t0100eb94d000a1b3c9.jpg" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p4.qhimg.com/dmfd/160_90_/t0100eb94d000a1b3c9.jpg" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p4.qhimg.com/dmfd/160_90_/t0100eb94d000a1b3c9.jpg" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p4.qhimg.com/dmfd/160_90_/t0100eb94d000a1b3c9.jpg" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p4.qhimg.com/dmfd/160_90_/t0100eb94d000a1b3c9.jpg" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p4.qhimg.com/dmfd/160_90_/t0100eb94d000a1b3c9.jpg" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p4.qhimg.com/dmfd/160_90_/t0100eb94d000a1b3c9.jpg" alt="商品图片"></a>
                    </li>
                    <li>
                        <a href="#"><img src="http://p4.qhimg.com/dmfd/160_90_/t0100eb94d000a1b3c9.jpg" alt="商品图片"></a>
                    </li>
                </ul>
            </div>
            <div style=" 100%;height: 1000px;"></div>
        </div>
        <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
        <script>
        $(function() {
            $(window).scroll(function() {
                //滚动条发生滚动时,要获取相应的值
                var top = $(document).scrollTop();
                var menu = $('#menu');
                var items = $('#content').find('.item');
                var currentId = '';
                //获取相应楼层的ID
                items.each(function() {
                    var m = $(this);
                    var itemTop = m.offset().top;
                    if (top > itemTop) {
                        currentId = '#' + m.attr('id');
                    } else {
                        return false;
                    }
                    //给相应楼层的 a 设置 current,取消其他链接的current
                    var currentLink = menu.find('.current');
                    if (currentId && currentLink.attr('href') != currentId) {
                        currentLink.removeClass('current');
                        menu.find('[href=' + currentId + ']').addClass('current');
                    }
                });
            });
        });
        </script>
       
    </body>
    
    </html>
  • 相关阅读:
    编写Excel文件的Golang库
    Go多组Raft库
    Go GraphQL初学者教程
    简洁架构的思想,基于go实现
    gometalinter代码质量检查分析工具(golang)
    用go实现一个redis-cli
    Sublime text —— 自定义主题Soda
    Sublime text —— 自定义Color theme
    Sublime Text增加Build system类型,打造一个全能IDE
    一个简单的增强型PHP curl函数
  • 原文地址:https://www.cnblogs.com/chenmiaosong/p/8413135.html
Copyright © 2020-2023  润新知