-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy path11-crazy-buttons.html
56 lines (49 loc) · 1.53 KB
/
11-crazy-buttons.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Getting Started with JavaScript</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style>
body, .jumbotron { padding: 30px; }
.text-giant { font-size: 40px; }
.btn-crazy {
position: absolute;
top: 30px;
left: 35%;
width: 30%;
transition: 0.5s ease all;
text-align: center;
}
</style>
</head>
<body>
<button type="button" class="btn-crazy btn btn-lg btn-danger">
Click Me!
</button>
<button type="button" class="btn-crazy btn btn-lg btn-primary">
Click Me!
</button>
<button type="button" class="btn-crazy btn btn-lg btn-success">
Click Me!
</button>
<!-- 🔥🔥🔥🔥 start javascript 🔥🔥🔥🔥 -->
<script>
// grab everything we need
const crazyButtons = document.querySelectorAll('.btn-crazy');
// define our functions
function goCrazy() {
// get a random number for the left offset
// random num for top offset
const offsetLeft = Math.random() * (window.innerWidth - this.clientWidth);
const offsetTop = Math.random() * (window.innerHeight - this.clientHeight);
console.log(offsetLeft, offsetTop);
// apply those numbers to the button
this.style.top = offsetTop + 'px';
this.style.left = offsetLeft + 'px';
}
// add event listeners
crazyButtons.forEach(button => button.addEventListener('mouseenter', goCrazy));
</script>
</body>
</html>