"```markdown
使用HTML5创建刮刮乐奖券布局
1. 基本结构
首先,创建一个基本的HTML结构,包括<!DOCTYPE html>声明和<html>,<head>,<body>标签。
html
<!DOCTYPE html>
<html lang=\"zh-CN\">
<head>
<meta charset=\"UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
<title>刮刮乐奖券</title>
<link rel=\"stylesheet\" href=\"styles.css\">
</head>
<body>
<div class=\"scratch-card\">
<div class=\"reward\">
<h2>恭喜你!</h2>
<p>你赢得了</p>
<h1>100元现金!</h1>
</div>
<div class=\"scratch-area\"></div>
</div>
<script src=\"script.js\"></script>
</body>
</html>
2. CSS样式
接下来,使用CSS来设计刮刮乐的外观。设置奖券的尺寸、背景颜色、边框和其他样式。
css
/* styles.css */
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
.scratch-card {
width: 300px;
height: 400px;
border: 2px solid #d5d5d5;
border-radius: 10px;
background: linear-gradient(135deg, #f9d423, #ff4e50);
position: relative;
overflow: hidden;
}
.reward {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
color: white;
}
.scratch-area {
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.8);
position: absolute;
top: 0;
left: 0;
cursor: pointer;
}
3. 刮刮乐功能
使用JavaScript来实现刮刮乐的刮除效果。监听鼠标事件并在划过的地方显示透明区域。
javascript
// script.js
const scratchArea = document.querySelector('.scratch-area');
scratchArea.addEventListener('mousemove', (e) => {
if (e.buttons !== 1) return; // 仅在按下鼠标时触发
const x = e.offsetX;
const y = e.offsetY;
const circle = document.createElement('div');
circle.style.width = '20px';
circle.style.height = '20px';
circle.style.borderRadius = '50%';
circle.style.position = 'absolute';
circle.style.left = `${x - 10}px`;
circle.style.top = `${y - 10}px`;
circle.style.backgroundColor = 'rgba(255, 255, 255, 0)'; //透明区域
scratchArea.appendChild(circle);
});
// 添加清除功能
scratchArea.addEventListener('mouseleave', () => {
scratchArea.style.pointerEvents = 'none'; // 禁用鼠标事件
});
4. 整体效果
上述代码将创建一个简单的刮刮乐奖券布局。用户可以使用鼠标在刮刮乐区域上"刮"出隐藏的信息,展现出奖品内容。
通过这种方式,我们可以轻松地创建一个交互式的刮刮乐奖券,结合HTML5、CSS和JavaScript技术,提供一个有趣的用户体验。
相关推荐
米米商聊1 小时前
同名附件为什么被 Map 覆盖?重复键与一对多索引echome8882 小时前
Python 异常处理:try、else、finally 的正确搭配时间的情敌2 小时前
OpenLayers 实战|Vue3 + OpenLayers 完整工程案例(加载天地图、要素、弹窗、绘制、WFS)陈随易2 小时前
下载神器!Motrix 停更三年后重写回归前端的日常3 小时前
一个人+AI做情侣食谱小程序,0 费用的服务器计算机魔术师3 小时前
谷歌搜索广告踩刹车,AI广告成为救命稻草总要学点什么3 小时前
第81篇:数据可视化-PM2.5分布——让圆点的胖瘦替每座城市报出浓度IT_陈寒3 小时前
Vue的v-for里用了个临时变量,页面直接挂了hunter_0764 小时前
一次 iOS 真机签名 409 的完整排查:证书、设备、描述文件为什么总是"对不上"枣把儿4 小时前
开源!公众号排版+一键发布工具懒人版