使用前端技术画一棵圣诞树,可以用手机打开
时间: 2023-06-26 18:04:00 浏览: 106
使用Python画了一棵圣诞树的实例代码
好的,以下是一个基于HTML、CSS和JavaScript的简单圣诞树实现:
HTML部分:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>圣诞树</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="tree-container">
<div class="tree">
<div class="tree-top"></div>
<div class="tree-body">
<div class="tree-layer"></div>
<div class="tree-layer"></div>
<div class="tree-layer"></div>
<div class="tree-layer"></div>
<div class="tree-layer"></div>
</div>
</div>
<div class="tree-bottom"></div>
</div>
<script src="./main.js"></script>
</body>
</html>
```
CSS部分:
```css
body {
margin: 0;
padding: 0;
background-color: #f7f7f7;
}
.tree-container {
position: relative;
margin-top: 50px;
width: 100%;
height: 100%;
text-align: center;
}
.tree {
position: relative;
display: inline-block;
}
.tree-top {
position: relative;
top: 0;
left: 50%;
margin-left: -50px;
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 100px solid #2d882d;
}
.tree-body {
position: relative;
top: -50px;
width: 0;
height: 0;
}
.tree-layer {
position: relative;
margin: 0 auto;
width: 0;
height: 0;
border-left: 100px solid transparent;
border-right: 100px solid transparent;
border-bottom: 50px solid #2d882d;
}
.tree-layer:nth-child(2) {
top: -30px;
}
.tree-layer:nth-child(3) {
top: -60px;
}
.tree-layer:nth-child(4) {
top: -90px;
}
.tree-layer:nth-child(5) {
top: -120px;
}
.tree-bottom {
position: relative;
top: -50px;
width: 150px;
height: 50px;
margin: 0 auto;
background-color: brown;
border-radius: 0 0 30px 30px;
}
```
JavaScript部分:
```javascript
// 让圣诞树动起来
setInterval(function() {
var layers = document.getElementsByClassName('tree-layer');
for (var i = 0; i < layers.length; i++) {
layers[i].style.transform = 'rotate(' + (i * 5) + 'deg)';
}
}, 100);
```
在浏览器中打开HTML文件,即可看到一个简单的圣诞树效果。同时,为了更好地适配手机屏幕,可以添加以下CSS代码:
```css
@media screen and (max-width: 600px) {
.tree-container {
margin-top: 20px;
}
.tree {
transform: scale(0.6);
}
}
```
这样在手机屏幕上也可以得到较好的显示效果。
阅读全文