PHP前端开发

css3怎样实现鼠标悬浮停止动画效果

百变鹏仔 4周前 (09-20) #CSS
文章标签 鼠标
在css中,可以利用“:hover”选择器和“animation-play-state”属性实现鼠标悬浮停止动画效果,语法为“动画元素:hover{animation-play-state:paused;}”。

本教程操作环境:windows10系统、CSS3&&HTML5版、Dell G3电脑。

css3怎样实现鼠标悬浮停止动画效果

在css中,可以利用animation属性给元素绑定动画;然后再使用@keyframes规则设置动画的作。

我们播放动画时,如要暂停动画,就要用到animation-play-state这个属性。animation-play-state属性有两个值:paused: 暂停动画;running: 继续播放动画;

立即学习“前端免费学习笔记(深入)”;

我们通过hover选择器选中鼠标悬浮在元素时的状态,就可以进行暂停动画。

下面我们通过示例来看一下,示例如下:

<!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>    <style>        div{            width:100px;            height:100px;            background-color:pink;            animation:fadenum 5s infinite;        }        @keyframes fadenum{   100%{transform:rotate(360deg);}}div:hover{            animation-play-state:paused;}    </style></head><body>    <div></div></body></html>

输出结果:

大家感兴趣的话,可以继续访问:css视频教程。