美文网首页
网页定位导航案例(jq方法)

网页定位导航案例(jq方法)

作者: 悟空你又瘦了 | 来源:发表于2017-05-31 15:17 被阅读0次

jq方法

<!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;
            left: 50%;
            top: 100px;
            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 不支持 fixed*/
        /*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 src="js/jquery-1.7.1.min.js"></script>
<script>
            $(function(){
                $(window).scroll(function(){
                    // 获取滚动条滚动距离
                    var top = $(document).scrollTop();
                    console.log(top)
                    var menu = $("#menu")
                    // 当前所在楼层的id
                    var currentId = "";
                    // 获取每一个item距离顶部的top距离,和滚动条的top比较,
                    // 如果top大了就获取id,直到最终小了停止
                    var items = $("#content").find(".item") ;         items.each(function(){
                        var m = $(this);
                        var itemTop = m.offset().top;
                        // 通过循环的判断,会锁定最终的,把前面的覆盖掉,-200为了体验更好
                        if (top > itemTop - 200){
                            currentId = "#"+m.attr("id");

                        }
                        
                    })

                //给相应楼层的a 设置 current,取消其他链接的current
                var currentLink = menu.find(".current")
                // 排他
                  if (currentId && currentLink.attr("href") !=currentId) {
                    currentLink.removeClass("current");
                    menu.find ("[href="+ currentId + "]").addClass("current")
                  }
                })
            })


js方法



相关文章

网友评论

      本文标题:网页定位导航案例(jq方法)

      本文链接:https://www.haomeiwen.com/subject/nkcjfxtx.html