jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。图标旋转是一种常见的视觉效果,通常用于指示加载状态或表示某种动态变化。
animate
方法可以实现复杂的动画效果,包括图标的旋转。transform
属性来实现旋转效果。animate
方法来实现旋转效果。<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Icon Rotation</title>
<style>
.icon {
transition: transform 0.5s;
}
.rotating {
transform: rotate(360deg);
}
</style>
</head>
<body>
<i class="icon">🔄</i>
<button onclick="rotateIcon()">Rotate</button>
<script>
function rotateIcon() {
const icon = document.querySelector('.icon');
icon.classList.toggle('rotating');
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Icon Rotation</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
.icon {
transition: transform 0.5s;
}
</style>
</head>
<body>
<i class="icon">🔄</i>
<button id="rotateBtn">Rotate</button>
<script>
$(document).ready(function() {
$('#rotateBtn').click(function() {
$('.icon').toggleClass('rotating');
if ($('.icon').hasClass('rotating')) {
$('.icon').animate({deg: 360}, {
duration: 1000,
step: function(now) {
$(this).css({transform: 'rotate(' + now + 'deg)'});
}
});
} else {
$('.icon').css({transform: 'rotate(0deg)'});
}
});
});
</script>
</body>
</html>
通过以上方法,你可以实现一个流畅且功能齐全的图标旋转效果。
领取专属 10元无门槛券
手把手带您无忧上云