jQuery 滑动门(Sliding Doors)是一种常见的网页设计技术,主要用于创建具有吸引力的导航菜单或图像切换效果。滑动门技术通过使用两个背景图像(通常是按钮的左右部分),并通过 CSS 和 JavaScript 来控制它们的显示和隐藏,从而实现平滑的滑动效果。
以下是一个简单的 jQuery 水平滑动门示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Sliding Doors</title>
<style>
.nav-button {
width: 120px;
height: 40px;
background: url('button-left.png') no-repeat left center;
display: inline-block;
cursor: pointer;
}
.nav-button span {
display: inline-block;
width: 100%;
height: 100%;
background: url('button-right.png') no-repeat right center;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="nav-button">
<span>Button</span>
</div>
<script>
$(document).ready(function() {
$('.nav-button').hover(
function() {
$(this).stop().animate({width: '160px'}, 200);
},
function() {
$(this).stop().animate({width: '120px'}, 200);
}
);
});
</script>
</body>
</html>
stop()
方法来停止当前动画,避免动画队列堆积。background-position
属性进行微调。通过以上方法,可以有效地解决 jQuery 滑动门技术中常见的问题,并提升用户体验。
领取专属 10元无门槛券
手把手带您无忧上云