窗口滚动怎么不出效果,真是看不出来,那里面错了!对比那几次也没找到问题

来源:4-5 编程练习

云彩无色3804005

2016-10-27 23:21

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

        #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="";
    items.each(function(){
    var m=$(this);
    var scrollTop=m.offset().top;
    if(top>scrollTop-200){
    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="http://img.mukewang.com/53660fce0001111903990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/53660fce0001111903990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/53660fce0001111903990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/53660fce0001111903990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/53660fce0001111903990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/53660fce0001111903990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/53660fce0001111903990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/53660fce0001111903990422.jpg" alt=""/></a></li>
<li><a href="#"><img src="http://img.mukewang.com/53660fce0001111903990422.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>

写回答 关注

7回答

  • 12只怕有心人
    2016-10-28 21:49:46
    已采纳

    回复 云彩无色3804005:额 我对的是你一开始的版本。。。

    以下是你的最新版本:

    $(document).ready(function(){

        $(window).scroll(function(){

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

           var menu=$("#menu");

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

           

           var currentID="";

           items.each(function(){

           var m=$(this);//1.变量命名不能用保留的关键字,用m就是为了少打几个字啊没必要用$(this)

           var itemTop=m.offset().top;

            if(top>itemTop-200){

          //2.把current改成currentID

            currentID="#"+m.attr("id");

           } else{

            return false;

          }

          });

         // 3.currentLink是新变量,前面要加上var

         var currentLink=menu.find(".current");

         if(currentID && currentLink.attr("href")!=currentID){

         //4.currentLink写错了

         currentLink.removeClass("current");

         //5.是从menu中开始寻找,不是currentID

         menu.find("[href="+currentID+"]").addClass("current");

         }

          });

    });

            


    12只怕有心... 回复云彩无色38...

    不谢;)

    2016-10-28 22:14:42

    共 3 条回复 >

  • 云彩无色3804005
    2016-10-28 22:13:18

               $(document).ready(function(){
      $(window).scroll(function(){
        var top=$(document).scrollTop();
        var menu=$("#menu");
        var items=$("#content").find(".item");
        var currentID="";
        items.each(function(){
        var m=$(this);
        var scrollTop=m.offset().top;
        if(top>scrollTop-200){
        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");  
       } //这块是个重点容易出错,里面必须是双引号
      });
    })
    //老板本问题解决,2处问题,一个是CLASS名称,一个是引号,折腾一天了

  • 云彩无色3804005
    2016-10-28 21:57:28

    $(document).ready(function(){
        $(window).scroll(function(){
           var top=$(document).scrollTop();
           var menu=$("#menu");
           var items=$("#content").find(".item");    
           var currentID="";
           items.each(function(){
           var $this=$(this);
           var itemTop=$this.offset().top;
            if(top>itemTop-200){
            currentID="#"+$this.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");
         }
       });
    });



  • 云彩无色3804005
    2016-10-28 21:00:47

    $(document).ready(function(){
        $(window).scroll(function(){
           var top=$(document).scrollTop();
           var menu=$("#menu");
           var items=$("#content").find(".item");
           
           var currentID="";
           items.each(function(){
           var $this=$(this);
           var itemTop=$this.offset().top;
            if(top>itemTop-200){
            current="#"+$this.attr("id");
           }
           else{
            return false;
          }
          });
         currentLink=menu.find(".current");
         if(currentID&&currentLink.attr("href")!=currentID){
         currentLlink.removeClass("current");
         currentID.find("[href="+currentID+"]").addClass("current");
         }
          });
    });

  • 12只怕有心人
    2016-10-28 15:31:10

    menu.find("[href='+currentID+']").addClass("current"); 引号对象错啦,改成

    menu.find("[href="+currentID+"]").addClass("current");  改成单引号也行

    云彩无色38...

    还是不行啊

    2016-10-28 21:01:21

    共 1 条回复 >

  • 云彩无色3804005
    2016-10-27 23:38:20

    下面这个代码,找不到问题所在

  • 云彩无色3804005
    2016-10-27 23:37:46

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

            #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="";
        items.each(function(){
        var m=$(this);
        var scrollTop=m.offset().top;
        if(top>scrollTop-200){
        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="http://img.mukewang.com/53660fce0001111903990422.jpg" alt=""/></a></li>
    <li><a href="#"><img src="http://img.mukewang.com/53660fce0001111903990422.jpg" alt=""/></a></li>
    <li><a href="#"><img src="http://img.mukewang.com/53660fce0001111903990422.jpg" alt=""/></a></li>
    <li><a href="#"><img src="http://img.mukewang.com/53660fce0001111903990422.jpg" alt=""/></a></li>
    <li><a href="#"><img src="http://img.mukewang.com/53660fce0001111903990422.jpg" alt=""/></a></li>
    <li><a href="#"><img src="http://img.mukewang.com/53660fce0001111903990422.jpg" alt=""/></a></li>
    <li><a href="#"><img src="http://img.mukewang.com/53660fce0001111903990422.jpg" alt=""/></a></li>
    <li><a href="#"><img src="http://img.mukewang.com/53660fce0001111903990422.jpg" alt=""/></a></li>
    <li><a href="#"><img src="http://img.mukewang.com/53660fce0001111903990422.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>

网页定位导航特效

本课程讲解网页定位导航特效,仿天猫版地狗购物网,你值得拥有

71404 学习 · 486 问题

查看课程

相似问题