jq实现滚动定位

Posted 松哥1204

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 {
        width: 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 {
        width: 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="width: 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>

 

以上是关于jq实现滚动定位的主要内容,如果未能解决你的问题,请参考以下文章

jq定位 scroll 定位

使用js或jq控制一个div,当滚动到页面顶部的时候固定在顶部,离开可继续滚动吗?

jq怎么获取绝对定位与相对定位的top值?

-仿京东商城楼梯式导航定位菜单

如何用jquery实现页面滚动到指定位置后触发事件的效果

jq实现页面中锚点滚动跳转