【问题标题】:Add fade out animation on dropped item with React Beautiful DND使用 React Beautiful DND 在丢弃的项目上添加淡出动画
【发布时间】:2020-08-06 23:46:30
【问题描述】:

我正在使用 React Beautiful DND 库在列之间拖动项目(方形 div)或在同一列中重新排序。我按照他们的 Egghead 视频教程来更改 div 的背景颜色,因为它被拖动。当它被丢弃时,它会切换回所有其他项目的默认颜色。我希望它在掉落后慢慢褪色(可能是 1 秒)到默认颜色。

这是我当前对 div 进行拖放时的代码样式。我添加了过渡线,但没有做任何事情。

const MyOrder = styled.div`
    background-color: ${(props) =>
        props.isDragging ? '#4FB740' : '#193DF4'};
    transition: background-color 1s ease;
`;

我已尝试将此代码添加到 onDragEnd 事件:

        setDroppedOrderID(theOrder.orderNumber);
        setTimeout(() => {
          setDroppedOrderID('');
        }, 2000);

我让被拖动的订单 div 看起来像这样:

<MyOrder
        id={orderNumber}
        className={`order size-${size} ${
            droppedOrderID === orderNumber ? ' dropped' : ''
        } ${palletLoc === 'OUTSIDE' ? 'outside' : ''}`}

但如果有人试图在不到 2 秒的时间间隔内拖动同一个项目,则会出现问题。

【问题讨论】:

  • 你能发布一个可运行的代码吗?

标签: css reactjs css-animations react-beautiful-dnd


【解决方案1】:

你实际上可以style the dropdo animation

See working demo & full code here in the codesandbox

您需要使用snapshot 中的isDropAnimating 属性来检查动画是否正在完成,以便您可以有条件地返回原始样式。

代码 sn-p

const OpportunityContainer = styled.div`
  border: 1px solid #ddd;
  border-radius: 0.3rem;
  background: #fff;
  padding: 1rem;
  margin-bottom: 0.8rem;
  transition: background-color 5s ease;
  background-color: ${props => (props.isDragging ? "#4FB740" : "#193DF4")};
`;
function getStyle(style, snapshot) {
  if (!snapshot.isDropAnimating) {
    return style;
  }

  // patching the existing style
  return {
    ...style,
    transition: `all 3s ease`,
    backgroundColor: "blue"
  };
}
const Opportunity = React.memo(({ index, id, title }) => {
  return (
    <Draggable draggableId={id} index={index}>
      {(provided, snapshot) => (
        <OpportunityContainer
          ref={provided.innerRef}
          {...provided.draggableProps}
          {...provided.dragHandleProps}
          isDragging={snapshot.isDragging && !snapshot.isDropAnimating}
          style={getStyle(provided.draggableProps.style, snapshot)}
        >
          {title}
        </OpportunityContainer>
      )}
    </Draggable>
  );
});

export default Opportunity;

注意 - 请务必阅读this note in the library documentationisDragging 将在动画/淡出完成之前为真。因此尽量缩短动画的持续时间(例如:1 秒或少于 1 秒)

【讨论】:

  • 哇,这令人印象深刻!快速跟进问题,是否可以让它保持绿色 2 秒,然后褪色为蓝色?
  • 是的 - 你可以添加transitionDelay: '2s' ...我已经更新了代码框...刷新浏览器并检查。
  • 我刚刚注意到一些奇怪的事情。在您将某个项目拖动到某处后,该项目不再能够被拖动。任何想法为什么?
  • ok - 刚刚看到库中关于不一致的注释see here ... 放下后,如果您轻轻触摸/移动其他元素,您将能够再次移动放下的项目......这是图书馆的问题... ...这里已经很晚了...早上,我会找到一种方法来克服这种不一致并使其为您工作
  • 嗨-抱歉延迟响应-我很忙....实际上,修复起来很棘手,但修复很简单....在getStyle方法中,只需将all添加到transition ...基本上你需要完成所有转换..不仅仅是background ...代码框已更新,只需刷新即可
猜你喜欢
  • 2020-04-14
  • 1970-01-01
  • 2020-12-30
  • 2020-10-04
  • 1970-01-01
  • 2020-06-07
  • 2014-02-21
  • 1970-01-01
  • 2021-05-13
相关资源
最近更新 更多