"```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技术,提供一个有趣的用户体验。
相关推荐
is今夕4 分钟前
postcss.config.js 动态配置基准值青茶绿梅*27 分钟前
500字理透react的hook闭包问题计算机软件程序设计12 分钟前
vue和微信小程序处理markdown格式数据指尖时光.13 分钟前
【前端进阶】01 重识HTML,掌握页面基本结构和加载过程前端御书房16 分钟前
Pinia 3.0 正式发布:全面拥抱 Vue 3 生态,升级指南与实战教程NoneCoder31 分钟前
JavaScript系列(84)--前端工程化概述晚安72038 分钟前
idea添加web工程零凌林2 小时前
vue3中解决组件间 css 层级问题最佳实践(Teleport的使用)糟糕好吃2 小时前
用二进制思维重构前端权限系统热门推荐
01DeepSeek各版本说明与优缺点分析02如何在WPS和Word/Excel中直接使用DeepSeek功能03DeepSeek本地部署详细指南04太炸裂了!清华大学deepseek从入门到精通使用手册又出第三版了,《普通人如何抓住DeepSeek红利》(无套路,直接下载)05本地部署DeepSeek教程(Mac版本)06DeepSeek r1本地安装全指南07本地化部署AI知识库:基于Ollama+DeepSeek+AnythingLLM保姆级教程08DeepSeek R1本地化部署 Ollama + Chatbox 打造最强 AI 工具09在Windows下安装Ollama并体验DeepSeek r1大模型10保姆级教程:利用Ollama与Open-WebUI本地部署 DeedSeek-R1大模型