javascript - Jquery scrollTo 在 Firefox 上不起作用

标签 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>

最佳答案

也许试试这个脚本: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");
   }                   
   });

关于javascript - Jquery scrollTo 在 Firefox 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20543847/

相关文章:

javascript - 删除子项时 jQuery touchmove 停止工作

javascript - JS中的文本函数第一个参数(_)

javascript - 如何从扩展中检测页面加载错误?

java - 使用 Java Selenium,如何让 Firefox 在新窗口或选项卡中打开下载的文本文件?

javascript - 如何搜索href并打开部分url

javascript - 构建了一个滚动 Controller ,需要反转它

javascript - 需要关闭我的应用程序打开的所有新窗口

javascript - $routeProvider 未知

Jquery数据表: Dynamically manipulate the aoColumns attributes

javascript - 如何在Node.js中声明和使用map/dictionary?