<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>种花</title>
<style>
.flower {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.flower div {
position: absolute;
width: 10px;
height: 10px;
border:3px solid #000;
border-radius: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div
class="flower"
@click="add"
>
<div
v-for="(d, i) in list"
:key="i"
:style="getStyle(d, i)"
></div>
</div>
<script src="js/vue.min.js"></script>
<script>
new Vue({
el: '.flower',
data: {
list: []
},
methods: {
getRandomColor() {
const c = () => Math.floor(Math.random() * 255);
return `rgb(${c()}, ${c()}, ${c()})`;
},
getStyle(obj, idx) {
return {
top: obj.top + 'px',
left: obj.left + 'px',
width: (10 + idx * 2) + 'px',
height: (10 + idx * 2) + 'px',
background: obj.background
};
},
add(event) {
const { clientX, clientY } = event;
this.list.push({
top: clientY,
left: clientX,
background: this.getRandomColor()
});
}
}
});
</script>
</body>
</html>