-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunning_text.js
55 lines (43 loc) · 1.33 KB
/
running_text.js
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
// Mousemove
class FollowMouse {
constructor(elementSelector) {
this.element = document.querySelector(elementSelector);
this.mouseX = 0;
this.mouseY = 0;
this.elementX = 0;
this.elementY = 0;
this.speed = 0.2;
this._animate();
this._listener();
}
_animate() {
let distX = this.mouseX - this.elementX;
let distY = this.mouseY - this.elementY;
this.elementX = this.elementX + distX * this.speed;
this.elementY = this.elementY + distY * this.speed;
this.element.style.left = this.elementX + "px";
this.element.style.top = this.elementY + "px";
window.requestAnimationFrame(() => this._animate());
}
_listener() {
const thisClass = this;
document
.querySelector(".move-container")
.addEventListener("mousemove", function (event) {
let offset = event.target.getBoundingClientRect();
thisClass.mouseX = event.clientX - offset.left;
thisClass.mouseY = event.clientY - offset.top;
});
document
.querySelector(".move-container")
.addEventListener("mouseout", function () {
thisClass.element.style.opacity = 0;
});
document
.querySelector(".move-container")
.addEventListener("mouseover", function () {
thisClass.element.style.opacity = 1;
});
}
}
new FollowMouse(".move-element");