-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
63 lines (45 loc) · 2.12 KB
/
script.js
File metadata and controls
63 lines (45 loc) · 2.12 KB
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
/*
make a rectangle and when we move mouse on that rectangle on
the left side we want to gradually change the color of the
rectangle rectangle to red and when we move mouse to the
right side we want to change the color to blue.
*/
/*
1 sabse pahle ye pata karo ke mouse rectangle par aaya and move hua ya nahi
2 ab ye calculate karo ki hum center se left par hai ye right side par hai
3. ab ye pata karo ke hum center se kitna left side par hai ye right side par hai uske bad utni hi intensity se coor
lagao
*/
/*
1. fist of all find out that mouse is on the rectangle and moved or not.
2. now calculate that we are on the left side of the center or on the right side of the center.
3. now find out how much we are on the left side or on the right side of the center and then apply the color with the same intensity.
*/
// selection oḟ center div
var rect = document.querySelector(".center");
// code for mouse on the center div rectangle
rect.addEventListener("mousemove", function (mousePosition) {
var rectlocation = rect.getBoundingClientRect(); // getBoundingClientRect() method returns the size of an element and its position relative to the viewport.
var rectlocationval = mousePosition.clientX - rectlocation.left; // mousePosition.clientX gives the horizontal coordinate of the mouse pointer when the mouse event was triggered.
if (rectlocationval < rectlocation.width / 2) { // if mouse is on left side of the rectangle
redcolor = gsap.utils.mapRange(rectlocation.width / 2, 0, 0, 255, rectlocationval); // mapRange() method maps a value from one range to another range.
gsap.to(rect, {
backgroundColor: `rgb(${redcolor},0,0)`,
ease: Power4.easeOut
})
}
else {
BlueColor = gsap.utils.mapRange(rectlocation.width / 2, rectlocation.width, 0, 255, rectlocationval);
gsap.to(rect, {
backgroundColor: `rgb(0,0,${BlueColor})`,
ease: Power4.easeOut
})
}
})
// code for mouseleave
rect.addEventListener("mouseleave", function () {
gsap.to(rect, {
backgroundColor: "white",
ease: Power4.easeOut
})
})