繁体   English   中英

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

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

我有一个要水平滚动网页的功能,下面的功能在Chrome中效果很好,只有当我在FirefoxInternet Explorer对其进行测试时,它会绊倒。 有人可以在我的语法中看到任何明显的错误吗?

/* 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);

    }



});

不同的浏览器将滚动条附加到不同的元素,您必须执行此操作

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

尝试找出比所有if / else语句更好的方法,这是一个示例。

将数据属性添加到锚点

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

您可以删除所有if / else疯狂,并使用两行代码进行相同操作

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

尝试:

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

一些浏览器使用“ html”,一些浏览器使用“ body”

暂无
暂无

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

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