如何使用 jQuery 在鼠标悬停时实现背景颜色变化的动画效果?

jqueryweb developmentfront end technology

要更改背景颜色,请使用 mouseenter 事件。鼠标悬停时,背景颜色会发生变化:

mouseenter: function(){
   $(this).css("background-color", "gray");
}

鼠标光标悬停在以下元素上:

<p class="my">点击并移动鼠标指针即可更改背景颜色。</p>

您可以尝试运行以下代码,了解如何在鼠标悬停时实现背景颜色变化的动画效果:

示例

<!DOCTYPE html>
<html>
<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
   <script>
      $(document).ready(function(){
         $("body").on({
            mouseenter: function(){
               $(this).css("background-color", "gray");
            },
   
            mouseleave: function(){
               $(this).css("background-color", "red");
            },
   
         });
      });
   </script>
</head>
<body>

   <p class="my">Click and move the mouse pointer to change the background color.</p>

</body>
</html>

相关文章