【问题标题】:Access 'scrollLeft' property in react在反应中访问'scrollLeft'属性
【发布时间】:2020-09-17 05:36:10
【问题描述】:

当我将代码从基本 HTML 和 JS 转换为响应时,我遇到了 div 属性的问题

旧代码和期望的结果是这样的:

https://codepen.io/fadeomar/project/editor/DVWGjd

现在开始转换此项目以做出反应时,它无法更改 scrollLeft 属性的值

我的问题是如何在反应中更改某个元素的scrollLeft 的值,当我搜索这个问题时,我得到的只是将事件侦听器设置为整个窗口对象,而不是 DOM 中的特定元素

我想为拖动和滚动使用可重用组件,这里是代码

import React from 'react';
import PropTypes from 'prop-types';

export class ScrollDrag extends React.Component {
  constructor(props) {
    super(props);
    this.ref = React.createRef();
    this.state = {
      isScrolling: false,
      clientX: 0,
      scrollX: 0,
    };
  }

  onMouseDown = e => {
    this.setState({ ...this.state, isScrolling: true, 
     clientX: e.clientX });
  };

  onMouseUp = () => {
    this.setState({ ...this.state, isScrolling: false });
  };

  onMouseMove = e => {
    const { clientX, scrollX } = this.state;
    if (this.state.isScrolling) {
      this.ref.current.scrollLeft = scrollX + e.clientX - clientX;
      this.setState({scrollX: scrollX + e.clientX - clientX, clientX: e.clientX})
    }
  };

  render() {
    const { rootClass } = this.props;
    return (
      <div
        ref={this.ref}
        onMouseDown={this.onMouseDown}
        onMouseUp={this.onMouseUp}
        onMouseMove={this.onMouseMove}
        className={rootClass}
      >
        {React.Children.map(this.props.children, child =>
            React.Children.only(child))}
      </div>
    );
  }
}

ScrollDrag.defaultProps = {
  ref: { current: {} },
  rootClass: '',
};

ScrollDrag.propTypes = {
  ref: PropTypes.object,
  rootClass: PropTypes.string,
  children: PropTypes.string,
};

export default ScrollDrag;

然后我将它用作应用程序中的继承顺序组件

这是我的尝试: https://codesandbox.io/s/peaceful-butterfly-ndp6z?file

感谢任何帮助完成这项工作

【问题讨论】:

    标签: javascript reactjs dom scroll drag


    【解决方案1】:

    更新答案

    您滚动了错误的元素。我从项目中删除了包装并调整了 css。我还必须反转滚动的极性。现在可以了。

    https://codesandbox.io/s/awesome-tree-s8miv?file=/src/DragScroll.js

    原始(现已编辑)问题的原始答案

    这个错误是因为你没有使用你在这个组件中创建的ref

    render() {
        const { ref, rootClass } = this.props; // <-- this is wrong
        return (
          <div
            ref={ref}
            onMouseDown={this.onMouseDown}
            onMouseUp={this.onMouseUp}
            onMouseMove={this.onMouseMove}
            className={rootClass}
          >
            {React.Children.map(this.props.children, child =>
                React.Children.only(child))}
          </div>
        );
    

    应该是这样的:

    render() {
        const { rootClass } = this.props;
        return (
          <div
            ref={this.ref}
            onMouseDown={this.onMouseDown}
            onMouseUp={this.onMouseUp}
            onMouseMove={this.onMouseMove}
            className={rootClass}
          >
            {React.Children.map(this.props.children, child =>
                React.Children.only(child))}
          </div>
        );
    

    【讨论】:

    • 感谢重播会,实际上错误消失了但没用,我安慰scrollLeft属性仍然为0它没有改变我想知道为什么,我会编辑问题
    • 我怀疑你的数学是关闭的 - this.ref.current.scrollLeft = scrollX + e.clientX - clientX... 如果 e.clientX === clientX 那么你的 scrollLeft 将永远为零。我建议你console.log it。
    • hay @will 我已经控制了操作,它按预期工作,但问题是 offestScroll 不适用于 div
    猜你喜欢
    • 2017-01-22
    • 2017-01-19
    • 1970-01-01
    • 2015-06-21
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 2020-10-02
    相关资源
    最近更新 更多