首页 > 解决方案 > jquery animate() 方法不流畅

问题描述

$(document).ready(function() {
  $(".first").hover(function() {
    $(this).animate({
      'font-size': '20px',
      'margin-top': '12px',
      'font-weight': '600'
    }, 500)
  })
})
.first {
  font-size: 16px;
  margin-top: 10px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="style.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
  </script>
</head>

<body>
  <p class="first">Hello</p>
</body>

</html>

我尝试使用 jquery animate() 方法为文本设置动画。但是动画并不流畅。我试图延迟动画并在css中使用transition属性,但结果是一样的。我应该怎么做才能使动画流畅?

标签: javascriptjqueryhtmlcss

解决方案


当你想使用时,animate.css你应该以正确的方式使用属性,例如在CSS margin-top: 20px中工作但在css.animate中它不起作用,你应该这样写marginTop: '20px'

注意:
在 animate.css 中,您不应该在属性名称中使用“”或“”,而只能在值中使用它。

所以你应该使用这个:

$(document).ready(function() {
  $(".first").hover(function() {
    $(this).animate({
      fontSize: '20px',
      marginTop: '12px',
      fontWeight: '600'
    }, 500)
  })
})

这是你的代码工作。JSfiddle


推荐阅读