jQuery 水波纹效果是一种视觉效果,当用户点击页面上的某个元素时,会在该元素周围产生一个类似水波扩散的效果。这种效果可以增强用户的交互体验,使界面更加生动和友好。
以下是一个使用 jQuery 实现动态水波纹效果的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Ripple Effect</title>
<style>
.ripple {
position: relative;
overflow: hidden;
}
.ripple .ripple-effect {
position: absolute;
border-radius: 50%;
background: rgba(0, 0, 255, 0.3);
transform: scale(0);
animation: ripple-animation 0.6s linear;
}
@keyframes ripple-animation {
to {
transform: scale(4);
opacity: 0;
}
}
</style>
</head>
<body>
<div class="ripple" id="ripple">
Click me!
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('#ripple').on('click', function(e) {
var ripple = $('<div class="ripple-effect"></div>');
var size = Math.max(this.clientWidth, this.clientHeight);
var x = e.pageX - this.offsetLeft - size / 2;
var y = e.pageY - this.offsetTop - size / 2;
ripple.css({
width: size,
height: size,
top: y + 'px',
left: x + 'px'
});
this.appendChild(ripple);
setTimeout(function() {
ripple.remove();
}, 600);
});
});
</script>
</body>
</html>
.ripple-effect
的 background
属性,增加透明度或改变颜色。x
和 y
的计算正确,使用 e.pageX
和 e.pageY
获取点击位置,并减去元素偏移量。will-change
属性或减少不必要的动画效果。通过以上方法,可以有效地实现和优化 jQuery 水波纹效果,提升用户界面的交互性和美观度。
领取专属 10元无门槛券
手把手带您无忧上云