First, we’ll prepare our HTML. Here’s the HTML we’ll use:
Okay, now we can start coding. First, we’ll style the 6 items in the dock.
We can get this result:
Now for the Javascript, let’s first define our variables.
And then this is the most important part. We’ll use the onmouseover event to scale the items.
Ah now, don’t get confused. I’ll explain the code.
The first line is items.forEach(item => {. This is a forEach loop. It’s a loop that runs for each div with the class item.
Then for each item, we add an event listener to the mouseover event. Then the hovered item will scale up. And it checks if there is a before or after item. If there is, it will scale up the next or previous item. And again, it checks if there is a next or previous item. If there is, it will scale up the next or previous item.
Okay, now we can see the items scale up when hovered but, it doesn’t scale down when the mouse leaves the item. Let’s fix that.
Okay, now we can see the items scale up when hovered and scale down when the mouse leaves the item.
The End
And that’s it! We’ve created an awesome hover effect using CSS and JavaScript. You can see the final result in the gif above. If you have any questions, feel free to ask in the comments below. Thanks for reading!