jQuery 刮刮乐是一种基于 jQuery 的网页互动效果,用户可以通过刮擦层来揭示下面的内容。这种效果常用于抽奖、游戏、促销活动等场景。
以下是一个基于 CSS 的 jQuery 刮刮乐示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery 刮刮乐</title>
<style>
.container {
width: 300px;
height: 150px;
background-color: #f0f0f0;
position: relative;
}
.mask {
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.5);
position: absolute;
top: 0;
left: 0;
}
.content {
position: absolute;
top: 0;
left: 0;
z-index: 2;
}
</style>
</head>
<body>
<div class="container">
<div class="mask"></div>
<div class="content">恭喜中奖!</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('.container').on('mousedown', function(e) {
var startX = e.pageX - $(this).offset().left;
var startY = e.pageY - $(this).offset().top;
$(document).on('mousemove', function(e) {
var endX = e.pageX - $(this).offset().left;
var endY = e.pageY - $(this).offset().top;
$('.mask').css({
'width': Math.abs(endX - startX),
'height': Math.abs(endY - startY),
'left': Math.min(startX, endX),
'top': Math.min(startY, endY)
});
});
$(document).on('mouseup', function() {
$(document).off('mousemove');
$(document).off('mouseup');
});
});
});
</script>
</body>
</html>
通过以上示例代码和解决方法,你可以快速实现一个简单的 jQuery 刮刮乐效果,并根据需要进行调整和优化。
领取专属 10元无门槛券
手把手带您无忧上云