jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。下拉菜单箭头旋转是一种常见的 UI 动画效果,用于指示下拉菜单的展开和收起状态。
下拉菜单箭头旋转通常分为两种类型:
transform
属性和 transition
属性实现平滑的旋转效果。animate
方法实现旋转效果。下拉菜单箭头旋转广泛应用于各种需要用户交互的网站和应用中,例如导航栏、设置菜单、筛选器等。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dropdown Arrow Rotation</title>
<style>
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown-arrow {
border: solid black;
border-width: 0 2px 2px 0;
display: inline-block;
padding: 3px;
transform: rotate(45deg);
transition: transform 0.3s;
}
.dropdown:hover .dropdown-arrow {
transform: rotate(-135deg);
}
</style>
</head>
<body>
<div class="dropdown">
<button>Dropdown</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
<span class="dropdown-arrow"></span>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dropdown Arrow Rotation</title>
<style>
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-arrow {
border: solid black;
border-width: 0 2px 2px 0;
display: inline-block;
padding: 3px;
transition: transform 0.3s;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="dropdown">
<button>Dropdown</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
<span class="dropdown-arrow"></span>
</div>
<script>
$(document).ready(function() {
$('.dropdown').hover(
function() {
$(this).find('.dropdown-content').show();
$(this).find('.dropdown-arrow').css('transform', 'rotate(-135deg)');
},
function() {
$(this).find('.dropdown-content').hide();
$(this).find('.dropdown-arrow').css('transform', 'rotate(45deg)');
}
);
});
</script>
</body>
</html>
transition
属性或 animate
方法的时间设置合理,并且没有其他 CSS 样式干扰。transform
属性或 animate
方法中的旋转角度,确保它们符合预期。通过以上示例代码和解决方法,你应该能够实现一个平滑的下拉菜单箭头旋转效果。
领取专属 10元无门槛券
手把手带您无忧上云