javascript - 如何在 React 中拖动带有动画的组件
问题描述
我正在尝试拖动其 css 具有变换动画的组件。但它无法正常工作。组件只是根据动画移动,无法拖动。有没有办法实现拖拽功能?
import * as React from "react";
import {
theComponent
} from "../style.css";
import Draggable from "react-draggable";
class DragMe extends React.Component {
render() {
return (
<Draggable
axis="both"
handle=".handle"
defaultPosition={{ x: 0, y: 0 }}
position={null}
grid={[25, 25]}
scale={1}
onStart={this.handleStart}
onDrag={this.handleDrag}
onStop={this.handleStop}
>
<div className={theComponent + " handle"}>
<p>Drag Me!</p>
</div>
</Draggable>
);
}
}
. theComponent{
animation: moveAnimation 3.5s infinite;
}
@keyframes moveAnimation {
0%,
100% {
transform: translate(-200%, 20%);
}
50% {
transform: translate(-200%, 25%);
}
}
解决方案
如果您将 css 动画从 更改 transform
为top, left
,则内联样式transform
将起作用并被拖动。尝试像这个例子一样重写css
.theComponent {
position: relative;
animation: moveAnimation 3.5s infinite;
}
@keyframes moveAnimation {
0%,
100% {
top: 5px;
}
50% {
top: 0px;
}
}
推荐阅读
- python - 当用户仅单击输入时,我试图停止循环?
- rust - 如果 str 不实现 Copy 特征,如何复制它?
- r - NLS 模型回归的标准误
- netlogo - 仅使用补丁在 Netlogo 中显示字母?
- html - 如何让我的 SVG 使用 css 或 javascript 在鼠标悬停时进行动画处理?
- python - sklearn 的分类报告是否要求输入 x 和 y 的长度相同?
- terminal - 腻子是否支持 SET_ANY_EVENT_MOUSE ,如果是,如何启用它?
- console - Where does ActiveMQ Artemis Console store address and queue definitions?
- linux - 错误:尝试进行简单的系统调用时类型冲突
- javascript - req.body 是使用 express 的简单 HTML 表单中的空对象