可以在按钮禁用时停用 Framer Motion 的 whileHover 动画吗?

huangapple go评论67阅读模式
英文:

Can i disactivate framer motion whileHover when button is disabled?

问题

我有一个包含两个按钮的项目计数组件,当按钮被禁用时,我想要禁用 whileHover。这可能吗?

<div className="flex space-x-5">
    <motion.button
        whileHover={{ scale: 1.1 }}
        transition={{ type: "spring", stiffness: 100, duration: 0.2 }}
        onClick={decrement}
        className="bg-blue-600 px-4 rounded-lg enabled:hover:bg-blue-500 disabled:opacity-60 disabled:cursor-not-allowed text-white"
        disabled={!stock || count <= 1}
    >
        -
    </motion.button>
    <span>{count}</span>
    <motion.button
        whileHover={{ scale: 1.1 }}
        transition={{ type: "spring", stiffness: 100, duration: 0.2 }}
        onClick={increment}
        className="bg-blue-600 px-4 rounded-lg enabled:hover:bg-blue-500 disabled:opacity-60 disabled:cursor-not-allowed text-white"
        disabled={!stock}
    >
        +
    </motion.button>
</div>

谢谢。我还没有找到解决方案。

英文:

i have an item count component with 2 buttons, and i want to disable the whileHover when the buttons are disabled. its possible?

&lt;div className=&quot;flex space-x-5&quot;&gt;
        &lt;motion.button
          whileHover={{ scale: 1.1 }}
          transition={{ type: &quot;spring&quot;, stiffness: 100, duration: 0.2 }}
          onClick={decrement}
          className=&quot;bg-blue-600 px-4 rounded-lg enabled:hover:bg-blue-500 disabled:opacity-60 disabled:cursor-not-allowed text-white&quot;
          disabled={!stock || count &lt;= 1}
        &gt;
          -
        &lt;/motion.button&gt;
        &lt;span&gt;{count}&lt;/span&gt;
        &lt;motion.button
          whileHover={{ scale: 1.1 }}
          transition={{ type: &quot;spring&quot;, stiffness: 100, duration: 0.2 }}
          onClick={increment}
          className=&quot;bg-blue-600 px-4 rounded-lg enabled:hover:bg-blue-500 disabled:opacity-60 disabled:cursor-not-allowed text-white&quot;
          disabled={!stock}
        &gt;
          +
        &lt;/motion.button&gt;
&lt;/div&gt;

Thanks.

I haven't found any solution

答案1

得分: 0

以下是翻译好的代码部分:

const isDisableDecrement = !stock || count <= 1
const isDisableIncrement = !stock

<div className="flex space-x-5">
  <motion.button
    whileHover={{ scale: isDisableDecrement ? 1 : 1.1 }}
    transition={{ type: "spring", stiffness: 100, duration: 0.2 }}
    onClick={decrement}
    className="bg-blue-600 px-4 rounded-lg enabled:hover:bg-blue-500 disabled:opacity-60 disabled:cursor-not-allowed text-white"
    disabled={isDisableDecrement}
  >
    -
  </motion.button>
  <span>{count}</span>
  <motion.button
    whileHover={{ scale: isDisableIncrement ? 1 : 1.1 }}
    transition={{ type: "spring", stiffness: 100, duration: 0.2 }}
    onClick={increment}
    className="bg-blue-600 px-4 rounded-lg enabled:hover:bg-blue-500 disabled:opacity-60 disabled:cursor-not-allowed text-white"
    disabled={isDisableIncrement}
  >
    +
  </motion.button>
</div>

希望对你有所帮助。

英文:

maybe you can try this

const isDisableDecrement = !stock || count &lt;= 1
const isDisableIncrement = !stock
    
&lt;div className=&quot;flex space-x-5&quot;&gt;
        &lt;motion.button
          whileHover={{ scale: isDisableDecrement ? 1 : 1.1 }}
          transition={{ type: &quot;spring&quot;, stiffness: 100, duration: 0.2 }}
          onClick={decrement}
          className=&quot;bg-blue-600 px-4 rounded-lg enabled:hover:bg-blue-500 disabled:opacity-60 disabled:cursor-not-allowed text-white&quot;
          disabled={isDisableDecrement}
        &gt;
          -
        &lt;/motion.button&gt;
        &lt;span&gt;{count}&lt;/span&gt;
        &lt;motion.button
          whileHover={{ scale: isDisableIncrement ? 1 : 1.1 }}
          transition={{ type: &quot;spring&quot;, stiffness: 100, duration: 0.2 }}
          onClick={increment}
          className=&quot;bg-blue-600 px-4 rounded-lg enabled:hover:bg-blue-500 disabled:opacity-60 disabled:cursor-not-allowed text-white&quot;
          disabled={isDisableIncrement}
        &gt;
          +
        &lt;/motion.button&gt;
&lt;/div&gt;

There are better solutions than this, but you can try it out.

huangapple
  • 本文由 发表于 2023年6月22日 10:09:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定