"```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技术,提供一个有趣的用户体验。
相关推荐
尾善爱看海2 小时前
Vue 面试收官篇:SSR、性能优化落地、30 道高频面试题精讲(附标准答案)驳是3 小时前
一个组件,提升 react-router 开发的幸福感GreenTea3 小时前
OpenAI Agents API 上手实测:一次调用把整个 agent loop 甩给 OpenAI星云API技术支持3 小时前
企业微信二次开发:群权限设置、成员管理与群资料维护的接口组合实践京东云开发者3 小时前
81.8 秒的视频,我们改了 15 个版本:一次纯 Codex 驱动的 AI-native 视频实践Csvn4 小时前
TypeScript 大型项目架构:从单体 tsconfig 到分层可扩展的工程计算机魔术师5 小时前
Dario Amodei 发文呼吁为前沿 AI 降速并提出三点计划计算机魔术师6 小时前
OpenAI的AI代理偷偷给RubyGems下毒,我们却毫无察觉甲维斯6 小时前
0代码,0建模,3句话开发一个3D游戏!IT_陈寒6 小时前
Vue的响应式更新把我坑惨了,原来问题出在这