Create Things and Build Stuff

Hover effects can make your site more alive and interactive. User will give more attention to the element that has hover effects since it is very fancy to look at the element performs some dynamic change when your cursor in the top of it.
In this tutorial we’re gonna show you how to add hover effects in your HTML elements using 3 different ways.

1. Hover Effects CSS Way

It used to be JavaScript that was required to do really interesting effects but these days CSS has come and capable of doing a ton of cool stuff. To add hover effects in your Html elements, simply use :hover in corresponding elements in our CSS file.

Let’s say we have first class in our Html elements.

1

2

3

4

5

<div id="container">

<img class="first"title="temple"src="candi.jpg"/>

<img class="second"title="Pugar"src="pugar.jpg"/>

<img class="third"title="Friends"src="bareng.jpg"/>

</div>

All we need to do in our Css styling is

1

2

3

4

5

6

.first:hover{

-webkit-transform:scale(1.25);

-moz-transform:scale(1.25);

position:relative;

z-index:5;

}

2. Jquery Way : hover() method

The hover() method needs two functions to run when the mouse pointer hovers over the selected elements.
This method triggers both the mouseenter and mouseleave events.

Note:
If only one function is specified, it will be run for both the mouseenter and mouseleave events.

Let’s add hover in our next Html elements, second class

1

2

3

4

5

<div id="container">

<img alt="hover effects"class="first"title="temple"src="candi.jpg"/>

<img class="second"title="Pugar"src="pugar.jpg"/>

<img class="third"title="Friends"src="bareng.jpg"/>

</div>

In the head of Html we need to specify what method will be used to give a hover effects.

1

2

3

4

5

6

7

8

9

10

11

12

<script type="text/javascript">

$(document).ready(function(){

$(".second").hover(function(){

$(this).css("background","yellow");

},function(){

$(this).css("background","");

});

});

</script>

3. Jquery Way : mouseover and mouseout events

Although Jquery provides us hover() method. Sometime it can be a trouble when we dynamically add elements during running time. The dynamically added elements will not have hover effects not like other elements that are created in design code (we will explain it later in the upcoming tutorials).
Here we use mouseover and mouseout events that are triggered inside on() method.