How to swap two cards without changing position other cards

早过忘川 提交于 2021-02-05 07:49:06

问题


I have images in grid form something like this

I am well aware of react dnd and it uses hover to get the positions and swap accordingly.

What I want

: Swap 1 with 4 and 4 with 1

What is happening:

While moving image 1 I am hovering it from 2 and 3 so the images in between are getting swapped here

I have modified my moveCard function to match my requirement.

const moveCard = useCallback((dragIndex, hoverIndex) => {
      const dragCard = cards;
      [dragCard[dragIndex], dragCard[hoverIndex]] = [
        dragCard[hoverIndex],
        dragCard[dragIndex]
      ];
      setCards([...dragCard]);
    });

Any help will be appreciated. All I want is to swap only 2 images that I am dragging and which I dropping, the rest images should not change their position.

来源:https://stackoverflow.com/questions/64913638/how-to-swap-two-cards-without-changing-position-other-cards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!