javascript jQuery scrollLeft 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3928848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:43:26  来源:igfitidea点击:

jQuery scrollLeft not working

javascriptjqueryscroll

提问by Zander

I just launched http://elliewauters.com& have a little problem that I'd like to fix, the site uses a lot of animated horizontal scrolling to go from one 'page' to the next, but on refresh most browsers remember the scroll position & go back to where I was before, I do not want this. Try going to the 'About' page & then refreshing. You'll see that the logo & menu are in the middle of the page as they where when you arrived at the site for the first time.

我刚刚启动了http://elliewauters.com并且有一个我想解决的小问题,该站点使用了大量动画水平滚动来从一个“页面”转到下一个“页面”,但是在刷新时大多数浏览器都会记住滚动位置并回到我以前的位置,我不想要这个。尝试转到“关于”页面,然后刷新。您会看到徽标和菜单位于页面中间,就像您第一次到达该站点时的位置一样。

I would like help with either of two things:

我需要以下两方面的帮助:

  1. To reset the scroll position back to 0,0 on page refresh OR
  2. To move the logo & menu to the top if the scrollLeft value is greater than 1.
  1. 在页面刷新时将滚动位置重置回 0,0 或
  2. 如果 scrollLeft 值大于 1,则将徽标和菜单移至顶部。

I have tried using $(window).scrollLeft(0);to no avail &

我试过使用$(window).scrollLeft(0);无济于事&

scrollLeft = $(window).scrollLeft();
console.log(scrollLeft)
if (scrollLeft>1) {
    $('#header').addClass('notLeft').css('top','0%');
} else {
    $('#header').addClass('left').css('top','25%');
}

does not work, does anyone please have an idea of how I could achieve what I want?

不起作用,有没有人知道我如何实现我想要的?

Thanks in advance

提前致谢

回答by Mottie

Well Firefox wasn't working because of the console.log(scrollLeft)so it should work after removing that. As for the problem you are having, I'm guessing it is only happening in IE? The solution is to not use $(window)but instead replace it with $(document). So use:

好吧,Firefox 无法正常工作,console.log(scrollLeft)因此在删除它后它应该可以工作。至于您遇到的问题,我猜它只发生在 IE 中?解决方案是不使用$(window),而是将其替换为$(document). 所以使用:

scrollLeft = $(document).scrollLeft();

回答by SellRex

I know this is an old thread, but .scrollLeft() in jQuery didn't work for me on Android, too, under jQuery 1.9.1 today (April, 2013). Try this:

我知道这是一个旧线程,但是 jQuery 中的 .scrollLeft() 在今天(2013 年 4 月)的 jQuery 1.9.1 下也不适用于 Android 上的我。试试这个:

window.scrollTo(x, y);

This worked for me on all recent platforms and OS's, including Android phones.

这对我最近的所有平台和操作系统都有效,包括 Android 手机。

回答by arturmoroz

$("#el").animate({ scrollLeft:110 }, "fast");