同样的老问题,.scrollTop(0) 在 Chrome 和 Chrome 中不起作用狩猎之旅

发布于 2025-01-05 22:29:46 字数 1431 浏览 1 评论 0原文

首先,让我指出,我已经用谷歌搜索过,甚至查看了这里的答案,例如 这个这个,但是,我还没有为我的案例找到可行的解决方案。

我设计了一个页面,其中有几个覆盖页面的固定元素,并由 html5/css3 组成,在主文档上创建一个干净的“掩码”,从而允许正文滚动条仍然滚动底层内容。

在 firefox 和 ie (bleh) 中,scrollTop(0) 工作完美。然而,正如问题所述,在我最喜欢的浏览器中并没有那么多。

我注意到的是在scrollTo事件之前和之后调用以下

$("body,html,document").each(function(){ console.log($(this).scrollTop()); });

内容结果并不令人满意,它告诉我scrolltop已经是0,因此甚至没有尝试scrollTop,或者至少这就是我的“认为”到目前为止。

在你问之前,我对这 3 个项目中的每一个都进行了控制台调用,因为文档滚动顶部应该包含在这些项目之一中(我会认为是 body,但就像在 ie 中一样,你也必须调用 html)

有想法的接受者吗?

仅供参考,这可能是一个 css 奇怪的问题(尽管它在 IE 中工作,而不是在 chrome 中工作,我真的无法理解),但我已经尝试过以下操作,得到负面结果:

$(window).scrollTop(0);
$(document).scrollTop(0);
$("html").scrollTop(0);
$("body").scrollTop(0);
window.scroll(0,0);
$("body,html,document").scrollTop(0);
$("body,html").scrollTop(0);

我想这扩展了我的问题,是这样吗? CSS问题? 我没有外部链接,而且代码太长(用 CI 视图部分制作),无法发布所有内容,但为了澄清我所做的事情:

  • 修复了页眉、页脚和侧边栏,使内容与文档滚动条滚动
  • 很少到目前为止,实现了 javascript 或 jquery,除了前面提到的元素的固定位置之外,几乎有 0 个自定义 css,
  • “内容”是使用 jQuery 的 .load 函数基于侧边栏中单击的列表项进行 ajax 的导航器

临时小提琴 不再上来了

First, let me point out, i've googled and even looked at answers on here like this and this, however, I'm still yet to find a working solution for my case.

I've designed a page that has several fixed elements covering the page and makes of html5/css3 to create a clean "mask" over the main document, thus allowing the body scroll bar to still scroll the underlying content.

In firefox and ie (bleh), scrollTop(0) is working perfect. However, as stated by the question, not so much in my fav browsers.

Something I've made note of is to call the following both before the scrollTo event and after

$("body,html,document").each(function(){ console.log($(this).scrollTop()); });

The results were not pleasing, it tells me that the scrolltop is already 0 and thus is not even attempting a scrollTop, or at least that's what I "think" thus far.

And before you ask, i made that console call on each of those 3 items as the document scrolltop should be covered within one of those items (i would think body, but like in ie you have to call html too)

Any takers on ideas?

FYI, it may be a css oddity (tho how it works in IE and not chrome i really cant understand) but I have already tried the following with negative results:

$(window).scrollTop(0);
$(document).scrollTop(0);
$("html").scrollTop(0);
$("body").scrollTop(0);
window.scroll(0,0);
$("body,html,document").scrollTop(0);
$("body,html").scrollTop(0);

Which I suppose extends my question, is this a css issue?
I dont have an outside link and the code is too long (made with CI view Partials) to post all of it, but to CLARIFY what i've done:

  • Fixed header, footer, and sidebar leaving content to scroll with documet scrollbar
  • very little javascript or jquery implemented thus far, almost 0 custom css outside of fixing position of presaid elements
  • the "content" is ajax'd in using jQuery's .load function based on list items clicked in sidebar navigator

temp Fiddle
no longer up

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

━╋う一瞬間旳綻放 2025-01-12 22:29:46

我也有同样的问题。如果我放入

$(window).scrollTop(0);

文档就绪处理程序,它不起作用;但如果我在 Chrome 开发者工具包的 javascript 控制台面板中测试它,它就有效了!唯一的区别是脚本的执行时间。所以我尝试了

window.setTimeout(function() {
    $(window).scrollTop(0); 
}, 0);

并且成功了。设置大于 0 的延迟不是必需的,尽管这也有效。这不是jQuery的错,因为它是一样的

window.scrollTo(0, 0);  window.scroll(0, 0);

所以原因可能是浏览器渲染页面并执行js后填充了window.pageYOffset属性。

I had the same problem. If I put

$(window).scrollTop(0);

in the document ready handler, it didn't work; but if I test it in the javascript console panel of Chrome developer toolkit, it worked! The only difference was the execution time of the script. So I tried

window.setTimeout(function() {
    $(window).scrollTop(0); 
}, 0);

and it worked. Setting a delay greater than 0 is not neccesary, although that also worked. It's not jQuery's fault, because it is the same with

window.scrollTo(0, 0);  window.scroll(0, 0);

So the reason might be the browser populates the window.pageYOffset property after it renders the page and executes the js.

无人问我粥可暖 2025-01-12 22:29:46

对于需要溢出设置的人,解决方法是使用这样的动画。

    $('#element_id').stop().animate({ scrollTop: 0 }, 500);

这似乎比 .scrollTop(0) 表现得更好。

For people that need the overflow settings, the workaround is to use an animation like this.

    $('#element_id').stop().animate({ scrollTop: 0 }, 500);

This seems to behave better than .scrollTop(0).

厌味 2025-01-12 22:29:46

问题出在 CSS 上。特别是我在下面列出的规则。

html, body {
    overflow-x: hidden;
    overflow-y: auto;
}

尽管这些规则显然与滚动条有关,但我不确定它们为什么会导致您所观察到的行为。但如果你删除它们,它应该可以解决你的问题。

请参阅:http://jsfiddle.net/jNWUm/23/

The problem is with CSS. In particular, the rules I've included below.

html, body {
    overflow-x: hidden;
    overflow-y: auto;
}

Though these rules are obviously related to scrollbars, I'm not sure why they are causing the behavior you are observing. But if you remove them, it should solve your problem.

See: http://jsfiddle.net/jNWUm/23/.

如痴如狂 2025-01-12 22:29:46

我刚刚在 IE8 和 Chrome 中测试了以下内容,两者都有效:

$(window).scrollTop(0)

I just tested the following in IE8 and Chrome and both work:

$(window).scrollTop(0)
等风来 2025-01-12 22:29:46

我在 chrome 中滚动时遇到了同样的问题。原因是 bodyhtml 中的 height:100% 样式。 查看此答案

I had a same problem with scrolling in chrome. Reason was height:100% style in body and html. See this answer

心如荒岛 2025-01-12 22:29:46

另请查看此答案:scrolltop bad on android - 它似乎效果最好当溢出设置为可见并且位置设置为相对时 - 但是 ymmv。您可能会发现这些很有用...

function repeatMeOnMouseDown() // for smooth scrolling
{
    var $newTop = $('#element_id').position().top + ($goingUp ? -20 : 20);
    $('#element_id').animate({top: $newTop}, 20, repeatMeOnMouseDown); 
}

// these should work
$('#element_id').animate({top: -200}, 200); // scroll down
$('#element_id').animate({top: 0}, 200); // scroll back up

// DON'T DO THIS - Offsets don't work with top like they do with scrollTop 
$('#element_id').animate({top: ("-= " + 200)}, 200);

// and when you get tired of fighting to do smooth animation 
// on different browsers (some buggy!), just use css and move on
function singleClick($goingUp)
{
   var $newTop = $('#element_id').position().top + ($goingUp ? -200 : 200);
    $('#element_id').css({top: $newTop}, 20); 
}

Also check out this answer: scrolltop broken on android - it seems to work best when overflow is set to visible and position to relative - but ymmv. You might find these useful...

function repeatMeOnMouseDown() // for smooth scrolling
{
    var $newTop = $('#element_id').position().top + ($goingUp ? -20 : 20);
    $('#element_id').animate({top: $newTop}, 20, repeatMeOnMouseDown); 
}

// these should work
$('#element_id').animate({top: -200}, 200); // scroll down
$('#element_id').animate({top: 0}, 200); // scroll back up

// DON'T DO THIS - Offsets don't work with top like they do with scrollTop 
$('#element_id').animate({top: ("-= " + 200)}, 200);

// and when you get tired of fighting to do smooth animation 
// on different browsers (some buggy!), just use css and move on
function singleClick($goingUp)
{
   var $newTop = $('#element_id').position().top + ($goingUp ? -200 : 200);
    $('#element_id').css({top: $newTop}, 20); 
}
青春如此纠结 2025-01-12 22:29:46

仅供参考,如果不在顶部正文中,即在 iframe 中,只需尝试 window.top.scrollTo(0,0);

FYI, if not in top body, i.e. in an iframe, just try window.top.scrollTo(0,0);

美人如玉 2025-01-12 22:29:46

此代码已在 Chrome 中测试。 http://jsfiddle.net/wmo3o5m8/1/

(function () {
    var down = false, downX, downY;
    document.onmousedown = function (e) {
        downX = e.pageX;
        downY = e.pageY;
        down = true;
    };
    document.onmouseup = function () { down = false; };
    document.onmousemove = function (e) {
        if (down) {
            window.scrollTo(
                window.scrollX + downX - e.pageX,
                window.scrollY + downY - e.pageY
            );
        }
    };
})();

This code was tested in chrome. http://jsfiddle.net/wmo3o5m8/1/

(function () {
    var down = false, downX, downY;
    document.onmousedown = function (e) {
        downX = e.pageX;
        downY = e.pageY;
        down = true;
    };
    document.onmouseup = function () { down = false; };
    document.onmousemove = function (e) {
        if (down) {
            window.scrollTo(
                window.scrollX + downX - e.pageX,
                window.scrollY + downY - e.pageY
            );
        }
    };
})();
兰花执着 2025-01-12 22:29:46

当您在 css 中为 htmlbody dom 元素声明 overflow:hidden 时,这是正常行为,正如您可以在 < a href="https://api.jquery.com/scrollTop/" rel="nofollow">jQuery API 文档:

垂直滚动位置与滚动的像素数相同
隐藏在可滚动区域上方的视图中。如果滚动条是
在最顶部,或者如果元素不可滚动,则该数字将
为 0。

使用 overflow:hidden 时,滚动条不可见,因此元素不可滚动,因此在您将使用的每个(我想知道)浏览器中该数字将为 0。

This is the normal behaviour when in your css you are declaring overflow: hidden for html or body dom elements, as you can read in the jQuery API docs:

The vertical scroll position is the same as the number of pixels that
are hidden from view above the scrollable area. If the scroll bar is
at the very top, or if the element is not scrollable, this number will
be 0.

With overflow: hidden the scroll bar is not visible hence element not scrollable, so the number will be 0 in every (I wonder) browser you will use.

紫瑟鸿黎 2025-01-12 22:29:46

$('#element-id').prop('scrollTop', 0); 也应该可以解决问题。

$('#element-id').prop('scrollTop', 0); should also do the trick.

§对你不离不弃 2025-01-12 22:29:46

我在 MacOS 上遇到了这个问题。我尝试将:放入

$(window).scrollTop(0);
document.body.scrollTop = document.documentElement.scrollTop = 0;

$(window).load 中,但它仍然无法正常工作。

然后我尝试使用一些技巧:

$('body')
  .css('position', 'fixed')
  .delay(200)
  .promise()
  .done(function() {
    $('body').css('position', 'relative');
  });

效果很好!位置固定类型将 分离,从而有效地将窗口滚动位置设置为零。然后,延迟了 200 毫秒(只是为了安全起见),我将 position:relative 放回去。

我希望这有帮助。谢谢。

I had this issue on MacOS. I tried to put:

$(window).scrollTop(0);
document.body.scrollTop = document.documentElement.scrollTop = 0;

in the $(window).load and it still wasn't working.

Then I tried to use some trick:

$('body')
  .css('position', 'fixed')
  .delay(200)
  .promise()
  .done(function() {
    $('body').css('position', 'relative');
  });

and it worked fine! The position fixed kind of detaches the <body> from the <html> which effectively sets the window scroll position to zero. Then with a delay of 200 milliseconds (just to be on the safer side), I put the position: relative back.

I hope this helps. Thanks.

叹沉浮 2025-01-12 22:29:46

$('yourElement').animate({scrollTop: 0 })

$('yourElement').animate({ scrollTop: 0 })

凌乱心跳 2025-01-12 22:29:46

在 Chrome 中,现在可以将 element.scroll(0,0)window.scroll(0,0) 用于可滚动元素(内容大于其元素) size 和 CSS 中的一些 元素 {overflow: auto} ),或整个页面,如 jsfiddle 这里

为了改善总体用户体验,建议还使用
element {scroll-behavior: smooth;} 也是——这个选项是使用动画的替代方案——正如之前的答案所提到的(或者直接使用 html {scroll-behavior: smooth; } 代替)。

In Chrome it's now possible to use both element.scroll(0,0) and window.scroll(0,0) for a scrollable element (having a content larger than its size and some element {overflow: auto} in CSS), or for the entire page, as shown in this jsfiddle here.

To improve the general UX, it's recommended to also use
element {scroll-behavior: smooth;} too -- this option being an alternative to using animation -- as mentioned by a previous answer (or directly the html {scroll-behavior: smooth;} instead).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文