简体   繁体   English

在PhantomJS中动画身体scrollTop不会更改scrollTop值

[英]Animating body scrollTop in PhantomJS doesn't change scrollTop value

As part of a component test I'm trying to verify an element is scrolled in PhantomJS. 作为组件测试的一部分,我试图验证元素是否在PhantomJS中滚动。 While the scroll code works fine in our app, the scrollTop of the element never changes when run in Phantom. 虽然滚动代码在我们的应用程序中可以正常工作,但是在Phantom中运行时,元素的scrollTop永远不会改变。

For the test, I ensure the body has a max-height, just in case. 为了进行测试,我确保身体具有最大高度,以防万一。

$body = $('body').css({
    'max-height': '1000px',
    overflow: 'scroll'
});

Then I add some elements which extend further than the body. 然后,我添加了一些扩展到比主体还远的元素。

$body.append(
    '<div style="height: 2000px;"></div>' +
    '<div style="height: 2000px;"></div>' +
    '<div style="height: 2000px;" data-uid="1"></div>');

Then I animate the scrollTop, given the third div as $element (so the offset().top == 4000). 然后给第三个div赋予$element ,使scrollTop动画(所以offset().top == 4000)。

$body.animate({
    scrollTop: $element.offset().top
}, 150, function() {
    // $body.scrollTop() is still 0
});

Yet after the animation has finished, the body isn't reporting any new scrollTop. 然而,动画结束后,主体将不会报告任何新的scrollTop。

I've been unable to find documentation or other examples that explain this issue. 我一直找不到解释此问题的文档或其他示例。

Have You tried setting viewportSize of the page? 您是否尝试过设置页面的viewportSize I'd guess that if the size is not set, the whole page gets rendered so that there is no need for scrolling (whole page is visible in the viewport). 我猜想,如果未设置大小,则会渲染整个页面,因此无需滚动(整个页面在视口中可见)。

Here is the description of that property 这是该属性的描述

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

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