简体   繁体   English

反应:试图用砌体布局实现顺序淡入效果,但是顺序不对

[英]React: Trying to achieve a sequential fade-in effect with a Masonry layout but it's out of order

I been trying to make a Masonry gallery with a sequential fade-in effect so that the pictures fade in one by one. 我试图制作一个具有连续淡入效果的砌体画廊,以便图片一张一张地淡入淡出。 And there is also a shuffle feature which will randomize the images and they fade in again after being shuffled. 还有一个随机播放功能,可以随机化图像,并在随机播放后再次淡入。

here is the demo and the code: 这是演示和代码:

https://tuo1t.csb.app/ https://tuo1t.csb.app/

https://codesandbox.io/s/objective-swartz-tuo1t https://codesandbox.io/s/objective-swartz-tuo1t

When first visiting the page, the animation is correct. 首次访问该页面时,动画是正确的。 However once we click on the shuffle button, something weird happened: There are often some pictures don't fade-in sequentially after the image before them faded in, there is even no fade-in animation on them, they just show up out of order. 但是,一旦我们单击随机播放按钮,就会发生一些奇怪的事情:有些图片在淡入图像后通常没有顺序地淡入,甚至没有淡入动画,它们只是从订购。

The way I achieved this animation is by adding a delay transition based on the index of the image, and use ref to track images. 我实现此动画的方式是通过添加基于图像索引的延迟过渡,并使用ref跟踪图像。

first I initialize the ref 首先,我初始化ref

 let refs = {};
  for (let i = 0; i < images.length; i++) {
    refs[i] = useRef(null);
  }

and I render the gallery 我渲染了画廊

 <Mansory gap={"1em"} minWidth={minWidth}>
        {imgs.map((img, i) => {
          return (
            <PicContainer
              index={img.index}
              selected={isSelected}
              key={img.index}
            >
              <Enlarger
                src={img.url}
                index={img.index}
                setIsSelected={setIsSelected}
                onLoad={() => {
                  refs[i].current.toggleOpacity(1); <--- start with zero opacity images till those are loaded
                }}
                ref={refs[i]}
                realIndex={i}
              />
            </PicContainer>
          );
        })}
      </Mansory>

for the every image component 对于每个图像组件

class ZoomImg extends React.Component {
  state = { zoomed: false, opacity: 0 };

  toggleOpacity = o => {
    console.log("here");
    this.setState({ opacity: o }); <-- a setter function to change the opacity state via refs:
  };

  render() {
    const {
      realIndex,
      index,
      src,
      enlargedSrc,
      setIsSelected,
      onLoad
    } = this.props;
    return (
      <div style={{ margin: "0.25rem" }} onLoad={onLoad}>
        <Image
          style={{
            opacity: this.state.opacity,
            transition: "opacity 0.5s cubic-bezier(0.25,0.46,0.45,0.94)",
            transitionDelay: `${realIndex * 0.1}s` <--- add a delay transition based on the index of the image.
          }}
          zoomed={this.state.zoomed}
          src={src}
          enlargedSrc={enlargedSrc}
          onClick={() => {
            this.setState({ zoomed: true });
            setIsSelected(index);
          }}
          onRequestClose={() => {
            this.setState({ zoomed: false });
            setIsSelected(null);
          }}
          renderLoading={
            <div
              style={{
                position: "absolute",
                top: "50%",
                color: "white",
                left: "50%",
                transform: "translateY(-50%} translateX(-50%)"
              }}
            >
              Loading!
            </div>
          }
        />
      </div>
    );
  }
}

I used console.log("here"); 我使用console.log("here"); in the setter function, which will be called for changing the opacity state via refs. 在setter函数中,将通过refs来更改不透明度状态。 There are 16 images, so initially it is called 16 times. 有16张图像,因此最初被称为16次。 But when I clicked on the shuffle button, you can see that it is called fewer than 16 times because some of the pictures show up directly without fading in. 但是,当我单击随机播放按钮时,您会看到它被调用的次数少于 16次,因为某些图片直接显示而不会褪色。

I been struggling with this problem for days and really hope someone can give me some hints. 我已经为这个问题苦苦挣扎了好几天,并真的希望有人能给我一些提示。

The problem is that your are adding only some new images in the shuffle method, one approach is to apply 0 opacity to all refs first, then wait a few ms to add 1 opacity again, like here . 问题是您在shuffle方法中仅添加了一些新图像,一种方法是先对所有refs应用0不透明度,然后等待几毫秒再次添加1不透明度,如此

But, I would recommend a better approach for animation, I love shifty and its Tweenable module. 但是,我建议对动画一个更好的方法,我喜欢缩骨及补间的模块。

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

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