forked from eracom/exemples-javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom-position.html
88 lines (69 loc) · 1.89 KB
/
random-position.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
<!doctype html>
<html>
<head>
<title>random position</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: "Roboto Mono";
}
body {
margin: 0
}
main {
border: 1px solid blue;
height: 90vh;
margin: 5vh;
}
.box {
border: 1px solid magenta;
width: 25vw;
height: 25vh;
margin: 0;
position: absolute;
}
.pos1 {
left: 20%;
top: 10%;
}
.pos2 {
left: 60%;
top: 20%;
}
.pos3 {
left: 10%;
top: 50%;
}
.pos4 {
left: 40%;
top: 60%;
}
</style>
</head>
<body>
<main>
<p class="box pos1">one</p>
<p class="box pos2">two</p>
<p class="box pos3">three</p>
</main>
<script>
// randomize the order...
function shuffle(a) {
for (let i = a.length; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
}
// Source: http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array
var numbers = ['1','2','3','4'];
shuffle(numbers);
// Ajouter les classes CSS
var boxes = document.getElementsByClassName("box");
var i;
for (i = 0; i < boxes.length; i++) {
boxes[i].className = "box pos"+numbers[i];
}
</script>
</body>
</html>