gpt4 book ai didi

javascript - jQuery 动画在 Firefox 的 IE 中不起作用?

转载 作者:行者123 更新时间:2023-11-30 10:16:01 24 4
gpt4 key购买 nike

我有一个功能,我想水平滚动我的网页,我有以下在 Chrome 中运行良好的功能,只有当我在 Firefox 中测试它时才会遇到问题和 Internet 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 语句更好的方法,这里是一个例子。

给 anchor 添加数据属性

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

你可以删除所有的 if/else 疯狂并用两行做同样的事情

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

关于javascript - jQuery 动画在 Firefox 的 IE 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23634766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com