为了账号安全,请及时绑定邮箱和手机立即绑定

代码照着老师讲的看了几遍也没找到什么问题,为什么没有出现滚动定楼层呢?求解大白一个,勿喷!

<!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;}


        


        #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 src="http://libs.baidu.com/jquery/1.10.2/jquery.js"></script>

<script>

       

            //滚动条发生滚动时,要获取相应的值。

           

          $(document).ready(function(){

       //  滚动条发生滚动

         $(window).scroll(function(){

             //在这里,我们做一些事情

            var top=$(document).scrollTop();

             var menu=$("#menu");

             var items=$("#current").find(".item");

             var currentId="";//当前所在的楼层(item)id

             items.each(function(){

                var m=$(this);

                 var itemTop=m.offset().top;

                 if(top>itemTop-200){

                  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")

             }


            // console.log(top)

         });

     });

    </script>   



         

</script>

</head>

<body>

<div id="menu">

<ul>

<!--运用锚点,实现导航定位。-->



<li><a href="#" class="current">1F 男装</a></li>

<li><a href="#">2F 女装</a></li>

<li><a href="#">3F 美妆</a></li>

<li><a href="#">4F 数码</a></li>

<li><a href="#">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://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

</ul>

</div>

<div id="item2" class="item">

<h2>2F 女装</h2>

<ul>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

</ul>

</div>

<div id="item3" class="item">

<h2>3F 美妆</h2>

<ul>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

</ul>

</div>

<div id="item4" class="item">

<h2>4F 数码</h2>

<ul>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

</ul>

</div>

<div id="item5" class="item">

<h2>5F 母婴</h2>

<ul>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

</ul>

</div>

</div>

</body>

</html>


正在回答

5 回答

  1. var items=$("#current").find(".item");  #current改成#content
    2.锚点链接没加
    <li><a href="#" class="current">1F 男装</a></li>。。。。
    全部改成相应的href="#item1" href="#item2" href="#item3" href="#item4"  href="#item5"
    3.   </script> 多个去掉个

    上面代码测试过,问题解决,你试下,我也是今晚问题才解决

0 回复 有任何疑惑可以回复我~

<!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;}


        


        #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 src="http://libs.baidu.com/jquery/1.10.2/jquery.js"></script>

<script>

       

            //滚动条发生滚动时,要获取相应的值。

           

          $(document).ready(function(){

       //  滚动条发生滚动

         $(window).scroll(function(){

             //在这里,我们做一些事情

            var top=$(document).scrollTop();

             var menu=$("#menu");

             var items=$("#content").find(".item");

             var currentId="";//当前所在的楼层(item)id

             items.each(function(){

                var m=$(this);

                 var itemTop=m.offset().top;

                 if(top>itemTop-200){

                  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")

             }


            // console.log(top)

         });

     });

    </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://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//53660fce0001111903990422.jpg" alt=""/></a></li>

</ul>

</div>

<div id="item2" class="item">

<h2>2F 女装</h2>

<ul>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//5366104c0001e5c403990422.jpg" alt=""/></a></li>

</ul>

</div>

<div id="item3" class="item">

<h2>3F 美妆</h2>

<ul>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610820001cf5b03990422.jpg" alt=""/></a></li>

</ul>

</div>

<div id="item4" class="item">

<h2>4F 数码</h2>

<ul>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610ac00012df703990422.jpg" alt=""/></a></li>

</ul>

</div>

<div id="item5" class="item">

<h2>5F 母婴</h2>

<ul>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

<li><a href="#"><img src="http://img1.sycdn.imooc.com//536610cd0001f34603990422.jpg" alt=""/></a></li>

</ul>

</div>

</div>

</body>

</html>

0 回复 有任何疑惑可以回复我~

1.首先要先改下锚点定位导航

2.var items=$("#current").find(".item"); 这一步是要在id为content的div里查找.item, 

  把current改成content就行啦

3. menu.find("[href="+ currentId + "]").addClass("current") 这一步也要用分号结尾的

4.你多用了一个</script>啦

0 回复 有任何疑惑可以回复我~

我也遇到了,真是找不出问题啊

0 回复 有任何疑惑可以回复我~

改过了,可是还是没效果

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

代码照着老师讲的看了几遍也没找到什么问题,为什么没有出现滚动定楼层呢?求解大白一个,勿喷!

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信