简体   繁体   English

Jquery scrollTo在Firefox上不起作用

[英]Jquery scrollTo doesn't work on Firefox

I'm using scrollTo-1.4.3.1. 我正在使用scrollTo-1.4.3.1。 plugin to selected menu item, and this works perfectly on Chrome but on Firefox is the total opposite. 插件到选定的菜单项,这在Chrome上完美运行,但在Firefox上完全相反。 Can anyone tell me why is this happening in Firefox and how can I fix this. 任何人都可以告诉我为什么在Firefox中发生这种情况,我该如何解决这个问题。

JS: 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>

And my HTML 还有我的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>

maybe try this script: http://jsfiddle.net/mekwall/up4nu/ 也许试试这个脚本: 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");
   }                   
   });

Try this: 尝试这个:

change html,body as window

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM