With Jquery
Using this way, as in the other answer will make all elements that have the class="openn"
class to be affected, which can be a great headache:
$('.openn').hover(function(){
// afeta TODOS os elementos com classe openn de uma vez
$('.openn').css('transform', 'rotate(90deg)');
});
Then use this
, because if you have more than one element with class .openn
all elements will be affected instead of affecting only with hover
.
$('.openn').hover(function(){
// afeta somente o elemento que disparou o evento, não afetando os outros elementos com classe openn
$(this).css('transform', 'rotate(90deg)');
});
Another detail might be preferable to use a CSS class with the name .hover
(not to be confused with :hover
), because then the CSS animation gets organized inside the same CSS instead of mixing with JavaScript. p>
$(document).on('mouseover', '.openn', function() {
$(this).addClass("hover");
});
.openn{
width: 32px;
height: 32px;
transition: transform 0.5s ease-in;
}
.openn.hover{
transform: rotate(90deg);
}
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="openn">
<i class="fa fa-chevron-right fa-2x openn" aria-hidden="true"></i>
</div>
<br>
<br>
<br>
<div class="openn">
<i class="fa fa-chevron-right fa-2x openn" aria-hidden="true"></i>
</div>
With pure JavaScript
Importing jQuery just for a simple effect of this is totally unnecessary, if you do not use jQuery, you can simply do this:
function classOpenn() {
this.classList.add("hover");
}
var openns = document.querySelectorAll('.openn');
for (var i = 0, j = openns.length; i < j; ++i) {
openns[i].addEventListener("mouseover", classOpenn);
}
.openn{
width: 32px;
height: 32px;
transition: transform 0.5s ease-in;
}
.openn.hover{
transform: rotate(90deg);
}
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="openn">
<i class="fa fa-chevron-right fa-2x openn" aria-hidden="true"></i>
</div>
<br>
<br>
<br>
<div class="openn">
<i class="fa fa-chevron-right fa-2x openn" aria-hidden="true"></i>
</div>