javascript - React-spring:包含动画图像的动画 div

标签 javascript reactjs frontend react-spring

我正在尝试使用react-spring动画 div和里面的图像 div ,代码如下。到目前为止,dangleProps工作正常:图像是动画的,但是 div 不是:hoveredProps由于某种原因无法正常工作,试图找出答案:

图标.js

import React, { useState } from "react";
import { useSpring, animated } from "react-spring";

export const Icon = ({ showPage, imageSource, altText, index }) => {
  const [isHovered, setIsHovered] = useState(false);

  const dangleProps = useSpring({
    transform: showPage ? "rotateZ(0deg)" : "rotateZ(180deg)",
    transformOrigin: "top",
    from: { transform: "rotateZ(180deg)" },
    config: { mass: 1, tension: 210 - index * 50, friction: 5 },
  });

  const hoveredProps = useSpring({
    transform: isHovered ? "rotateZ(320deg)" : "rotateZ(0)",
    from: { transform: "rotateZ(0)" },
    config: { mass: 1, tension: 210, friction: 5 },
  });

  return (
    <animated.div
      className="contact-icon"
      onMouseEnter={() => setIsHovered(true)}
      onMouseLeave={() => setIsHovered(false)}
      style={hoveredProps}
    >
      <animated.img src={imageSource} alt={altText} style={dangleProps} />
    </animated.div>
  );
};

<小时/>
section.contact {
  background-color: rgba(160, 210, 140, 0.3);
  margin: 0;
  height: 100px;
  border-radius: 15px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.contact-icon {
  margin: 0 calc(1vw + 10px);
  cursor: pointer;
}
.contact-icon img {
  height: 50px;
}

注:section.contact包裹几个 <Icon />

最佳答案

你的例子很难重现。最后我成功了。

问题是,您没有指定 roteteZ 的单位。如果将 deg 添加到所有出现的情况中,它将起作用。

  const hoveredProps = useSpring({
    transform: isHovered ? "rotateZ(320deg)" : "rotateZ(0deg)",
    from: { transform: "rotateZ(0deg)" },
    config: { mass: 1, tension: 210, friction: 5 },
  });

https://codesandbox.io/s/eager-monad-tb41s

关于javascript - React-spring:包含动画图像的动画 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61201338/

相关文章:

javascript - 如何在 React-Native 中使用 Javascript 解析时将 HTML 代码替换为相应的字符

javascript - 如何拆分具有多个符号的字符串

javascript - jQuery 为图像标签创建了不正确的元素

javascript - 当我尝试在 onsubmit 函数中调用函数时,函数未定义

html - 在这种情况下如何将 div 专门居中放置在我的容器中?

javascript - MatchMedia.js 是否包含在 Modernizr JS 中?

JavaScript 按钮不起作用

javascript - 如何在 JSX 中为带有选择器的 const 设置默认变量

javascript - 如何在 ReactJS JSX 中执行嵌套的 if else 语句?

css - Material-UI 下拉列表在对话框中溢出