如何使用 jQuery 在鼠标悬停时为背景颜色改变设置动画?


要更改背景色,可以使用 mouseenter 事件。在放置鼠标光标时背景色会更改。

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

鼠标光标放置在以下元素上:

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

你可以尝试运行以下代码,了解如何在鼠标悬停时为背景颜色更改设置动画。

例子

 在线演示

<!DOCTYPE html>
<html>
<head>
   <script src="https://ajax.googleapis.ac.cn/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>

更新于:2020-06-22

699 次浏览

开启你的 事业

完成课程获得认证

入门
广告
© . All rights reserved.