Upon observation, it appears that the <a>
tags on those websites react to hover actions. Achieving this effect can be accomplished through JavaScript or CSS, depending on the desired reaction.
If you aim to display an image around your cursor similar to the showcased websites, you must utilize either an event listener or a custom cursor. It is worth noting that the websites examined did not employ a custom cursor, as the regular pointer cursor remains visible.
Below is an example of how this can be achieved with JavaScript and some CSS (the image appears and follows the cursor, disappearing when the link is no longer hovered over).
HTML:
<div id="container">
<a href="" data-src="road.png" class="hover">
<h1>Hover here!</h1>
</a>
</div>
Ensure that the href
attribute contains the desired destination URL when clicked. If no hyperlink is needed (assuming both referenced websites have links), the element could be changed to a <div>
or another suitable tag.
The data-src
attribute plays a crucial role by specifying which image to use in JavaScript. While alternative methods exist, utilizing datasets for passing small information between HTML and JavaScript is preferred. The naming convention isn't critical as long as it begins with "data-" (e.g., data-image
or data-href
).
CSS:
#container {
position: relative;
}
.followMouse {
z-index: -1;
position: absolute;
}
The class followMouse
will be used when displaying the image. A negative z-index
places it behind other elements on the page, aligning with the examples provided. Setting its position to absolute
is crucial for defining coordinates without affecting surrounding styling. Therefore, assigning a relative
position to the parent element (in this case, the one with the id "container") is essential.
Now onto the implementation with JavaScript:
for (let el of document.querySelectorAll('.hover')) {
let image = new Image();
image.src = el.dataset.src;
image.className = "followMouse";
el.addEventListener('mouseover',(e)=>{
document.getElementById('container').append(image);
image.style.left = `${e.x - (image.width/2)}px`;
image.style.top = `${e.y - (image.height/2)}px`;
})
el.addEventListener('mouseout',(e)=>{
image.remove();
})
}
window.addEventListener('mousemove',(e)=> {
let image = document.querySelector('.followMouse');
if (image) {
image.style.left = `${e.x - (image.width/2)}px`;
image.style.top = `${e.y - (image.height/2)}px`;
}
})
By using document.querySelectorAll
, all elements with the 'hover' class can trigger this behavior together. Accessing the data-src
attribute involves referencing the element's dataset. Subsequently, an image is loaded based on this attribute linked with the class followMouse
. Delaying appending ensures the image only appears upon hovering over the element.
For each element with the 'hover' class, two event listeners are added: one to show the image within the container on mouseover and another to remove it onmouseout. When showing the image, adjustments to its style ensure proper alignment with the cursor at the center.
Since direct access to CSS attributes such as 'left' and 'top' requires string input, template literals allow dynamic content insertion via ${insert js here}.
Lastly, a window-wide event listener monitors cursor movement, dynamically updating the image position while hovering over elements triggering the interactive response.