首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >连续循环卷轴

连续循环卷轴
EN

Stack Overflow用户
提问于 2019-10-23 01:03:16
回答 1查看 1.3K关注 0票数 1

我使用react中的https://github.com/locomotivemtl/locomotive-scroll插件来实现平滑滚动。我试图让它做一个连续的滚动,这样无论你滚动到哪个方向,它都会循环,通过添加和删除状态数组中的项来改变顺序。

这是有效的,但会导致一些超级闪烁。

演示的小结是

https://codesandbox.io/s/heuristic-resonance-0ybkl

https://codesandbox.io/s/young-wind-ozeeq

https://codesandbox.io/s/patient-darkness-6gxj1

任何帮助都将不胜感激

下面的代码

代码语言:javascript
复制
import React from "react";
import ReactDOM from "react-dom";
import LocomotiveScroll from "locomotive-scroll";
import "./styles.css";

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      limit: null,
      items: [
        "https://www.fillmurray.com/640/460",
        "https://www.fillmurray.com/g/640/560",
        "https://www.fillmurray.com/640/760",
        "https://www.fillmurray.com/g/640/860",
        "https://www.fillmurray.com/640/160",
        "https://www.fillmurray.com/g/640/260",
        "https://www.fillmurray.com/g/640/360",
        "https://www.fillmurray.com/g/640/460"
      ]
    };
  }
  componentDidMount() {
    const target = document.querySelector("#view");
    const scroll = new LocomotiveScroll({
      el: target,
      smooth: true,
      smoothMobile: true,
      getDirection: true
    });

    scroll.on("scroll", obj => {
      console.log(obj.scroll.y);
      if (this.state.limit !== null) {
        if (obj.direction === "up") {
          var current = this.state.items;

          var first = current.shift();
          current.push(first);
          this.setState(
            {
              items: current
            },
            () => {
              scroll.update();
            }
          );
        }

        if (obj.direction === "down") {
          current = this.state.items;
          var last = current.pop();
          current.unshift(last);
          this.setState(
            {
              items: current
            },
            () => {
              scroll.update();
            }
          );
        }
      } else {
        var halfway = obj.limit / 2;
        scroll.scrollTo(target, halfway);
        this.setState({
          limit: obj.limit
        });
      }
    });

    scroll.scrollTo(target, 1);
  }
  render() {
    return (
      <div className="App">
        <div id="view">
          <div className="left">
            {this.state.items.map(item => {
              return <img src={item} alt="" />;
            })}
          </div>
        </div>
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
EN

回答 1

Stack Overflow用户

发布于 2019-10-23 05:25:53

出现闪烁的原因是因为整个组件都会在scroll事件上重新呈现,而您的元素上没有键。

代码语言:javascript
复制
this.setState(
    {
        items: current
    },
    () => {
        scroll.update();
    }
);

在上面的代码中,您设置了scroll事件的状态,该事件将导致列表的完全重新呈现。您需要在map函数中的元素上设置一个key

Key docs

Code Sandbox

代码语言:javascript
复制
import React from "react";
import ReactDOM from "react-dom";
import LocomotiveScroll from "locomotive-scroll";
import "./styles.css";

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      limit: null,
      items: [
        "https://www.fillmurray.com/640/460",
        "https://www.fillmurray.com/g/640/560",
        "https://www.fillmurray.com/640/760",
        "https://www.fillmurray.com/g/640/860",
        "https://www.fillmurray.com/640/160",
        "https://www.fillmurray.com/g/640/260",
        "https://www.fillmurray.com/g/640/360",
        "https://www.fillmurray.com/g/640/460"
      ]
    };
  }
  componentDidMount() {
    const target = document.querySelector("#view");
    const scroll = new LocomotiveScroll({
      el: target,
      smooth: true,
      smoothMobile: true,
      getDirection: true
    });

    scroll.on("scroll", ({ limit, direction }) => {
      if (this.state.limit !== null) {
        if (direction === "up") {
          console.log("up");
          var current = this.state.items;
          var last = current.pop();
          current.unshift(last);
          this.setState(
            {
              items: current
            },
            () => {
              scroll.update();
            }
          );
        }

        if (direction === "down") {
          current = this.state.items;
          var first = current.shift();
          current.push(first);
          this.setState(
            {
              items: current
            },
            () => {
              scroll.update();
            }
          );
        }
      } else {
        var halfway = limit / 2;
        scroll.scrollTo(target, halfway);
        this.setState({
          limit
        });
      }
    });

    scroll.scrollTo(target, 1);
  }
  render() {
    return (
      <div className="App">
        <div id="view">
          <div className="left">
            {this.state.items.map((item, idx) => {
              return <img key={`${item}${idx}`} src={item} alt="" />;
            })}
          </div>
        </div>
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58509162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档