<!DOCTYPE html>
<html>
<head>
	<title>Animation Example</title>
	<style>
		#box {
			width: 100px;
			height: 100px;
			background-color: red;
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);
			animation: move 2s infinite;
		}
<pre><code>	@keyframes move {
		0% {
			transform: translate(-50%, -50%) rotate(0deg);
		}
		50% {
			transform: translate(-50%, -50%) rotate(180deg);
		}
		100% {
			transform: translate(-50%, -50%) rotate(360deg);
		}
	}
&lt;/style&gt;
</code></pre>
</head>
<body>
	<div id="box"></div>
<pre><code>&lt;script&gt;
	var box = document.getElementById(&quot;box&quot;);
	box.addEventListener(&quot;mouseover&quot;, function() {
		box.style.animationPlayState = &quot;paused&quot;;
	});
	box.addEventListener(&quot;mouseout&quot;, function() {
		box.style.animationPlayState = &quot;running&quot;;
	});
&lt;/script&gt;
</code></pre>
</body>
</html>
<p>这个动画效果是一个红色的正方形在不断旋转,同时也有鼠标悬停时暂停的效果。在CSS中使用@keyframes定义动画的关键帧,然后在元素中使用animation属性来应用动画。在JavaScript中使用addEventListener来监听鼠标事件,并使用style属性来操作CSS样式。</p>
用CSS和JavaScript写一个动画效果。

原文地址: https://www.cveoy.top/t/topic/bfsY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录