Jquery scrollTo在Firefox上不起作用

时间:2013-12-12 12:39:26

标签: javascript jquery firefox scrollto

我正在使用scrollTo-1.4.3.1。插件到选定的菜单项,这在Chrome上完美运行,但在Firefox上完全相反。任何人都可以告诉我为什么这会在Firefox中发生,我该如何解决这个问题。

JS:

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="../js/jquery.scrollTo-1.4.3.1-min.js"></script>
<script>
        $( document ).ready(function(e) {

            var lastId,
                topMenu = $(".main-nav"),
                topMenuHeight = topMenu.outerHeight()+15,
                // All list items
                menuItems = topMenu.find("a"),
                // Anchors corresponding to menu items
                scrollItems = menuItems.map(function(){
                  var item = $($(this).attr("href"));
                  if (item.length) { return item; }
                });         

            menuItems.click(function(e){
              var items = $('.main-nav li a');
              var lastItem = $('.main-nav li a:last');
              var index = items.index(lastItem);
              //alert(item);
              var href = $(this).attr("href"),
                  offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight+1;
            if(href=="#contact-section"){
              $('html, body').stop().animate({         
                  scrollTop: offsetTop+600
              }, "slow");
            }
            else
            $('html, body').stop().animate({ 
                  scrollTop: offsetTop
              }, "slow");
              e.preventDefault();
            });

        });
        </script>

我的HTML

<ul class="main-nav">
                    <li>
                        <a href="#portfolio" id="toTop">Client Portfolio</a>
                    </li>
                    <li>
                        <a href="#service-provided" id="toService">Service Provided</a>
                    </li>
                    <li>
                        <a href="#team-section" id="toTeam">Our Team</a>
                    </li>
                    <li>
                        <a href="#contact-section" id="toContact">Contact Us</a>
                    </li>
                </ul>

2 个答案:

答案 0 :(得分:2)

也许可以尝试这个脚本:http://jsfiddle.net/mekwall/up4nu/

  // Cache selectors
    var lastId,
    topMenu = $("#top-menu"),
    topMenuHeight = topMenu.outerHeight()+15,
    // All list items
    menuItems = topMenu.find("a"),
    // Anchors corresponding to menu items
    scrollItems = menuItems.map(function(){
      var item = $($(this).attr("href"));
      if (item.length) { return item; }
    });

    // Bind click handler to menu items
    // so we can get a fancy scroll animation
    menuItems.click(function(e){
    var href = $(this).attr("href"),
      offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight+1;
    $('html, body').stop().animate({ 
      scrollTop: offsetTop
    }, 300);
    e.preventDefault();
    });

    // Bind to scroll
    $(window).scroll(function(){
    // Get container scroll position
    var fromTop = $(this).scrollTop()+topMenuHeight;

    // Get id of current scroll item
    var cur = scrollItems.map(function(){
     if ($(this).offset().top < fromTop)
       return this;
    });
    // Get the id of the current element
    cur = cur[cur.length-1];
    var id = cur && cur.length ? cur[0].id : "";

    if (lastId !== id) {
       lastId = id;
       // Set/remove active class
       menuItems
         .parent().removeClass("active")
         .end().filter("[href=#"+id+"]").parent().addClass("active");
   }                   
   });

答案 1 :(得分:0)

试试这个:

change html,body as window

    $('window').stop().animate({         
                      scrollTop: offsetTop+600
                  }, "slow");