With Jquery
Using so, as in the other answer will make all elements that has the class class="openn"
are affected, which can be a major headache:
$('.openn').hover(function(){
// afeta TODOS os elementos com classe openn de uma vez
$('.openn').css('transform', 'rotate(90deg)');
});
Then use the this
, for if you have more than one element with the class .openn
all elements will be affected instead of only affecting the 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 this way the CSS animation is all organized inside the same CSS, instead of mixing with Javascript.
$(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>
<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>
With pure Javascript
Importing jQuery only for a simple effect like this is totally unnecessary, if you don’t use jQuery, you can just do so:
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>
Thank you for your reply, although it was more developed, the reply of @Bsalvo was the one that helped me.
– I_like_trains
@I_like_trains is not a matter of more developed, it is the question that I pointed out possible problems and situations, as the
this
, what I pointed out was more than details, it was a more "guaranteed" implementation. Got it? The choice of best answer is your decision, I don’t want to influence you, I just want to help you, pointing out that the code of the other answer may fail in certain scenarios– Guilherme Nascimento
@I_like_trains edited the answer, see if it’s clear now.
– Guilherme Nascimento
although you have commented William, his first code still uses the class instead of the
$(this)
, changing them all the same.– Dudaskank
@Dudaskank you tested in "Run", did you not pass the mouse quickly, accidentally over the 2? I think there was some confusion.
– Guilherme Nascimento
In fact, the excerpt does not have the "Run", it is the one first. Rereading, in fact you say not to do with 2x the class
.openn
and put as you should not do, instead of placing the section already with thethis
. Further down, you do use thethis
, but in a version already with other amendments.– Dudaskank
@Dudaskank the problem in my text interpretation, got it, I’m improving the text. Grateful!
– Guilherme Nascimento
@Dudaskank corrected, thanks again!
– Guilherme Nascimento
@Guilhermenascimento, good, but I took advantage and edited also in your reply, I hope you do not find bad rs
– Dudaskank
@Dudaskank was great, already approved! Grateful!
– Guilherme Nascimento