简体   繁体   English

jQuery动画无法在Firefox的IE中工作?

[英]jQuery animate not working in IE of Firefox?

I have a function that I want to scroll my webpage horizontally, I have the following that works well in Chrome only it stumbles when I come to test it in Firefox and Internet Explorer . 我有一个要水平滚动网页的功能,下面的功能在Chrome中效果很好,只有当我在FirefoxInternet Explorer对其进行测试时,它会绊倒。 Can anybody see any notable errors in my syntax? 有人可以在我的语法中看到任何明显的错误吗?

/* Navigtion */
$('nav ol li a').click(function(e){
    e.preventDefault();
    $('nav').find('.active').removeClass('active'); 
    $(this).addClass('active'); 


    if( $(this).hasClass('sectionOne') ){

        scrollTo = $('.section-one').position().left;             
        $('body').animate({'scrollLeft': scrollTo}, 800);

    } else if( $(this).hasClass('sectionTwo') ){

        scrollTo = $('.section-two').position().left;             
        $('body').animate({'scrollLeft': scrollTo}, 800);

    } else if( $(this).hasClass('sectionThree') ){

        scrollTo = $('.section-three').position().left;             
        $('body').animate({'scrollLeft': scrollTo}, 800);

    } else if( $(this).hasClass('sectionFour') ){

        scrollTo = $('.section-four').position().left;             
        $('body').animate({'scrollLeft': scrollTo}, 800);

    } else if( $(this).hasClass('sectionFive') ){

        scrollTo = $('.section-five').position().left;             
        $('body').animate({'scrollLeft': scrollTo}, 800);

    } else if( $(this).hasClass('sectionSix') ){

        scrollTo = $('.section-six').position().left;             
        $('body').animate({'scrollLeft': scrollTo}, 800);

    } else if( $(this).hasClass('sectionSeven') ){

        scrollTo = $('.section-seven').position().left;             
        $('body').animate({'scrollLeft': scrollTo}, 800);

    }



});

Different browsers attach the scrollbar to different elements, you have to do this 不同的浏览器将滚动条附加到不同的元素,您必须执行此操作

$('html, body').animate({'scrollLeft': scrollTo}, 800);

Try to figure out a better way than all those if / else statements, here's an example. 尝试找出比所有if / else语句更好的方法,这是一个示例。

Add data attributes to the anchors 将数据属性添加到锚点

<nav>
   <ol>
      <li>
         <a data-section="section-one" ....

and you can remove all the if/else madness and do the same with two lines 您可以删除所有if / else疯狂,并使用两行代码进行相同操作

var scrollTo = $('.' + $(this).data('section')).position().left;             
$('html, body').animate({'scrollLeft': scrollTo}, 800);

try: 尝试:

$('body').animate({'scrollLeft': scrollTo}, 800);
$('html').animate({'scrollLeft': scrollTo}, 800);

Some browsers work with 'html' and some work with 'body' 一些浏览器使用“ html”,一些浏览器使用“ body”

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

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