简体   繁体   English

同时设置 scrollLeft 和 scrollTop

[英]Set scrollLeft and scrollTop simultaneously

Is there any way to set scrollLeft and scrollTop of a div simultaneously?有没有办法同时设置div的scrollLeft和scrollTop? In Chrome, Safari and Opera it works right away by setting them sequentially but in Firefox(older than 4) and IE(even in IE9!) setting one of them causes reflow resulting in ugly glitches because the page will first move left then down, like a stair-motion.在 Chrome、Safari 和 Opera 中,它通过按顺序设置它们立即起作用,但在 Firefox(4 岁以上)和 IE(甚至在 IE9 中!)中设置其中之一会导致回流,从而导致丑陋的故障,因为页面将首先向左移动然后向下移动,就像一个楼梯运动。

I see that window has a method called scrollTo(x,y), is there any equivalent for normal divs?我看到那个窗口有一个名为 scrollTo(x,y) 的方法,普通 div 有什么等价的吗?

Or, is it possible to change the behavior of the browser so the reflow won't trigger on just changing the scroll.或者,是否可以更改浏览器的行为,以便仅更改滚动就不会触发回流。 One source i found said this would only happen if i have onscroll-event registered on the div but i don't have any so that can't be the problem.我发现的一个消息来源说,只有当我在 div 上注册了 onscroll-event 时才会发生这种情况,但我没有,所以这不是问题。

I was having this same issue just moments ago. 我刚刚遇到同样的问题。 I found that the animate suggestion was not good (jumpy, laggy, ultimately worse), but the supplied jQuery plugin that came with it was at least marginally better. 我发现动画建议并不好(跳跃,迟滞,最终更糟),但随附的jQuery插件至少稍微好一些。 For me, the overhead it required was not really worth the tiny gain. 对我来说,它所需的开销并不值得获得微小的收益。

In my situation, I have a scrollable div with one large image inside. 在我的情况下,我有一个可滚动的div,里面有一个大图像。 The larger this image is, the worse the reflow. 该图像越大,回流越差。 I was able to quite drastically improve the situation by hiding the div immediately before setting the scroll properties, then showing it again immediately afterward . 通过在设置滚动属性之前立即隐藏div,然后立即再次显示div,我能够彻底改善这种情况。 This allows IE to ignore re-rendering the contents after the first property is set , and instead wait until the element is "visible" again (after they are both set). 允许IE在设置第一个属性后忽略重新呈现内容 ,而是等待元素再次“可见”(在它们都被设置之后)。

There doesn't seem to be any flicker in any current version of any major browser (which was my first concern). 在任何主流浏览器的当前版本中似乎没有任何闪烁(这是我首先关注的)。 Tested in Firefox 4, Opera 11, Chrome 10, IE 8, IE8 Compatibility, and Safari 5. 在Firefox 4,Opera 11,Chrome 10,IE 8,IE8兼容性和Safari 5中进行了测试。

In jQuery: 在jQuery中:

var my_pane = $('#my_scroll_pane');
my_pane.css( 'visibility', 'hidden' );
my_pane.scrollTop( 100 ).scrollLeft( 100 );
my_pane.css( 'visibility', 'visible' );

Regular ole' JavaScript: 常规''JavaScript:

var scroll_pane = document.getElementById('my_scroll_pane');
scroll_pane.style.visibility = 'hidden';
scroll_pane.scrollTop = 100;
scroll_pane.scrollLeft = 100;
scroll_pane.style.visibility = 'visible';

UPDATE : This does flicker pretty roughly in FF3.6. 更新 :这在FF3.6中大致闪烁。 If anybody has any ideas that don't involve browser sniffing, any input would be welcome. 如果任何人有任何不涉及浏览器嗅探的想法,那么欢迎任何输入。

You should not be using scrollLeft and scrollTop, you should be setting the scrollLeft and scrollTop using .animate(). 你不应该使用scrollLeft和scrollTop,你应该使用.animate()设置scrollLeft和scrollTop。

.animate({
    .scrollLeft: x,
    .scrollTop: y
})

There are also several plugins built from http://plugins.jquery.com/project/ScrollTo to simplify the process. 还有几个从http://plugins.jquery.com/project/ScrollTo构建的插件,以简化该过程。

I would imagine that an animation would be the best method for this (most JavaScript frameworks will do it), but you could also try this: 我认为动画将是最好的方法(大多数JavaScript框架都会这样做),但你也可以试试这个:

setTimeout("verticalScrollFunction", 1);
setTimeout("horizontalScrollfunction", 1);

You might be able to use Object.assign :您也许可以使用Object.assign

const {scrollLeft, scrollTop} = computeGoodScrollPosition(myDiv);
Object.assign(myDiv, {
  scrollLeft,
  scrollTop
});

使用绝对定位或减去边距而不是滚动。

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

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