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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| <style>
body {
cursor: none;
min-height: 100%;
background: rgb(0, 0, 0);
}
#myCustomCursor {
position: fixed;
width: 30px;
height: 30px;
background: #000000;
border-radius: 50%;
top: var(--y, 0);
left: var(--x, 0);
transform: translate(-50%, -50%);
mix-blend-mode: normal;
pointer-events: none;
transition-duration: 50ms;
transition-timing-function: ease-out;
z-index: 999999!important;
}
#myCustomCursor.myCursorHoverState {
cursor: none;
width: 90px;
height: 90px;
background: pink;
}
}
</style>
<script>
function createCustomCursor() {
let cursor = document.getElementById('myCustomCursor');
if (cursor) {
console.log('myCustomCursor already exist');
addCursorSpecialEffectToAllPageLinks(cursor);
} else {
cursor = document.createElement("div");
cursor.setAttribute("id", "myCustomCursor");
document.body.appendChild(cursor);
initCustomCursor(cursor);
addCursorSpecialEffectToAllPageLinks(cursor);
}
}
function initCustomCursor(cursor) {
document.body.onmousemove = function(e) {
cursor.style.setProperty('--x', (e.clientX) + 'px');
cursor.style.setProperty('--y', (e.clientY) + 'px');
}
}
function addCursorSpecialEffectToAllPageLinks(cursor) {
var links = document.querySelectorAll("a"); // Get page links
// This ״for loop״ is used to find all the page links and add the "myCursorHoverState" css class to create special effect on hover
for (var i = 0; i < links.length; i++) {
links[i].addEventListener("mouseenter", function(event) {
console.log('In');
cursor.classList.add("myCursorHoverState"); // Add the hover class
}, false);
links[i].addEventListener("mouseleave", function(event) {
console.log('Out');
cursor.classList.remove("myCursorHoverState"); // Removethe hover class
}, false);
}
}
function myFunction(x) {
if (x.matches) { // If media query matches
createCustomCursor();
}
}
var x = window.matchMedia("(min-width: 1001px)") //desktop
myFunction(x) // Call listener function at run time
x.addListener(myFunction) // Attach listener function on state changes
</script> |
Partager