-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.html
93 lines (82 loc) · 3.42 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<html>
<style>
#svgboard {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
}
</style>
<body>
<svg id="svgboard" version="1.1" xmlns="http://www.w3.org/2000/svg">
</svg>
</body>
<script>
const getX = (box=new Array(), config = {}) => {
// box可传引用,也可以return。
let maxX = document.documentElement.clientWidth
config = {
num: 15,
minWidth: 104,
maxWidth: 146,
minHeight: 145,
maxHeight: 204,
durBase: 10,
imageLink: 'bg.png',
...config,
};
let { num, minWidth, maxWidth, minHeight, maxHeight, durBase, imageLink } = config;
for (let i = 0; i < num; i++) {
let height = Math.random() * (maxHeight - minHeight) + minHeight;
box.push({
key: +new Date() + Math.random() + i,
width: Math.random() * (maxWidth - minWidth) + minWidth,
height: height,
x: Math.random() * maxX + 1,
y: `-${height}`,
dur: Math.random() * durBase + 1,
imageLink: imageLink
});
}
return box
};
function createSVGElement(elementName) {
let node = document.createElementNS('http://www.w3.org/2000/svg', elementName)
return node
}
function setAnimation(board, lists) {
let maxY = document.documentElement.clientHeight
for (let item of lists) {
let image = createSVGElement('image')
image.addEventListener('click', () => {
// alert会阻塞住整个渲染进程,但动画好像还在继续。
// 这里最好用Promise直接写一个弹窗。
alert('哦吼,红包1个亿')
})
image.setAttribute('href', item.imageLink)
image.setAttribute('width', item.width)
image.setAttribute('height', item.height)
image.setAttribute('style', 'transform-box: fill-box; transform-origin: center; cursor: pointer')
image.setAttribute('transform', 'rotate(25)')
let motion = createSVGElement('animateMotion')
motion.setAttribute('path', `M ${item.x} ${item.y} L ${item.x - 500} ${maxY + 500}`)
motion.setAttribute('begin', '0s')
motion.setAttribute('dur', `${item.dur}s`)
motion.setAttribute('repeatCount', 'indefinite')
image.appendChild(motion)
board.appendChild(image)
}
}
function insertImage() {
let board = document.querySelector('#svgboard')
let bgArguments = getX()
let starArguments = getX(new Array(), {minWidth: 21, maxWidth: 21, minHeight: 21, maxHeight: 21, imageLink: 'star.png'})
let lineArguments = getX(new Array(), { num: 13, minWidth: 16, maxWidth: 16, minHeight: 114, maxHeight: 114, imageLink: 'line.png'})
setAnimation(board, bgArguments)
setAnimation(board, starArguments)
setAnimation(board, lineArguments)
}
insertImage()
</script>
</html>