简体   繁体   English

使用React,如何在可滚动div中水平滚动到所选项目?

[英]With React, how can I horizontally scroll to the selected item in a scrollable div?

The idea is to have a horizontal scrollable list, and when selecting an item, the list scrolls to that item, which should make the item centered in the list. 想法是要有一个水平可滚动列表,选择一个项目时,列表会滚动到该项目,这应使该项目在列表中居中。

I have tried to create an example using React, but I struggle to calculate the scrollLeft value. 我试图使用React创建一个示例,但是我很难计算出scrollLeft值。

http://jsfiddle.net/remisture/zug42kh8/ http://jsfiddle.net/remisture/zug42kh8/

const scrollContainer = ReactDOM.findDOMNode(this.scrollRef);
const activeItem = ReactDOM.findDOMNode(this.activeRef);

const scrollRect = scrollContainer.getBoundingClientRect();
const activeRect = activeItem.getBoundingClientRect();
const activeWidth = activeRect.width;
const activeLeft = activeRect.left;
const activeRight = activeRect.right;
const scrollWidth = scrollContainer.scrollWidth;
const scrollLeft = scrollRect.left;

scrollContainer.scrollLeft = (scrollWidth / 2) + (activeLeft / 2) + (scrollLeft * 2);

Desired result: 所需结果:

在此处输入图片说明

JSFiddle 的jsfiddle

First of all, yeah, calculating: 首先,是的,计算:

scrollLeftPosition: activeRect.left - scrollRect.left - (scrollRect.width / 2) + (activeRect.width / 2)

Second. 第二。 I changed manipulation with scrollLeft to "+=". 我将scrollLeft的操作更改为“ + =“。 As you should not just assign in your case, but take in account current scroll state. 因为您不应该只是根据情况分配,还应该考虑当前的滚动状态。

scrollContainer.scrollLeft += this.state.scrollLeftPosition;

And the main point: you called this.centerActiveItem() before new state applied. 要点:在应用新状态之前,您调用了this.centerActiveItem()。 this.setState is async function. this.setState是异步函数。 You need to pass function to call it after updating state. 更新状态后,您需要传递函数来调用它。

this.setState({}, callback);

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

相关问题 如何滚动到可滚动div中的元素? - How can I scroll to an element inside a scrollable div? 在反应中滚动到可滚动div的底部 - Scroll to the bottom of a scrollable div in react 如何通过鼠标滚轮水平滚动div并锁定主体滚动? - How can I horizontally scroll div by mouse wheel, locking the body scroll? 包含垂直可滚动div的水平可滚动div不会在iOS上水平滚动 - Horizontally scrollable div which contains vertical scrollable div will not scroll horizontally on iOS 当行不在视图中时,如何让可滚动的 div 元素与已设置为通过箭头键选择的行一起滚动? - How can I get a scrollable div element to scroll with rows that have been set up to be selected via arrow keys when the rows are out of view? 不能在水平滚动div中调整表列的大小 - can not resize a table Column in a horizontally Scrollable div 我需要使用React.JS水平滚动到div id吗? - I need to horizontally scroll to a div id using React.JS ? 如何将列表框中的选定项目滚动到顶部? - How can I make selected item in listbox scroll to the top? 无论我横向滚动多少,怎样才能在页面左侧粘贴div? - How can i have a div stuck on the left side of the page no matter how much i scroll horizontally? 如何在水平滚动表中显示可见的垂直溢出? - How can I have visible vertical overflow in a horizontally scrollable table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM