css + js和纯css实现图片不停旋转 鼠标悬停停止旋转

8 篇文章 0 订阅
  • css + js 实现:

效果图如下动图所示:

效果图.gif

代码如下:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			#img {
				border-radius: 50%;
				cursor: pointer;
				position: absolute;
				top: 50%;
				left: 50%;
				margin-top: -150px;
				margin-left: -150px;
			}
		</style>
		<script>
			var rotateVal = 0 // 旋转角度
			var InterVal // 定时器
			window.onload = function () {
				// 网页加载完成后即运行rotate函数
				rotate()
				// 鼠标悬浮在图片上时,停止旋转,即清除定时器
				document.getElementById('img').onmousemove = function () {
					clearInterval(InterVal)
				}
				// 鼠标离开图片时,继续旋转,即继续运行定时器
				document.getElementById('img').onmouseleave = function () {
					rotate()
				}
			}
			
			// 设置定时器
			function rotate () {
				InterVal = setInterval(function () {
					var img = document.getElementById('img')
					rotateVal += 1
					// 设置旋转属性(顺时针)
					img.style.transform = 'rotate(' + rotateVal + 'deg)'
					// 设置旋转属性(逆时针)
					//img.style.transform = 'rotate(-' + rotateVal + 'deg)'
					// 设置旋转时的动画  匀速0.1s
					img.style.transition = '0.1s linear'
				}, 100)
			}
		</script>
	</head>
	<body>
		<img id="img" src="images/111.jpg" width="300px" height="300px" />
	</body>
</html>

111.jpg文件:

111.jpg

  • 纯css实现:

            #img {
				border-radius: 50%;
				cursor: pointer;
				position: absolute;
				top: 50%;
				left: 50%;
				margin-top: -150px;
				margin-left: -150px;
				animation: rotate 10s linear infinite;
			}
			#img:hover {
				animation-play-state: paused;
			}
			@keyframes rotate {
				from {
					transform: rotate(0deg);
				}
				to {
					transform: rotate(360deg);
				}
			}

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值