Css3 stop after finishing rotation?

Asked

Viewed 158 times

5

I am wearing transform: rotate(90deg) to rotate a div when I put the mouse on top. Once I remove it, it returns to its initial position. There is a way to stop it?

I have my code like this

.openn{
    transition: transform 0.5s ease-in; 
}
.openn:hover{
    transform: rotate(90deg);
}

<div class="openn">
  <i class="fa fa-chevron-right fa-2x openn" aria-hidden="true"></i>
</div>

3 answers

10

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 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

  • @I_like_trains edited the answer, see if it’s clear now.

  • although you have commented William, his first code still uses the class instead of the $(this), changing them all the same.

  • @Dudaskank you tested in "Run", did you not pass the mouse quickly, accidentally over the 2? I think there was some confusion.

  • 1

    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 the this. Further down, you do use the this, but in a version already with other amendments.

  • @Dudaskank the problem in my text interpretation, got it, I’m improving the text. Grateful!

  • @Dudaskank corrected, thanks again!

  • @Guilhermenascimento, good, but I took advantage and edited also in your reply, I hope you do not find bad rs

  • 1

    @Dudaskank was great, already approved! Grateful!

Show 5 more comments

6


Use jQuery to make the element hover do not go back to its originality when the mouse comes out of the top:

Reading some comments, let’s go to the editions!

$('.openn').hover(function(){

   $(this).css('transform', 'rotate(90deg)');

});

Remembering that you must include the jQuery library for this to work (If not already included).

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

The .hover() is an event capable of combining events mouseenter() and mouseleave(), that is written here. (The famous two birds with one stone). This facilitates us in writing the code, and we have the power to manipulate events in any way we want. An example of the manipulation of the two events:

$('.l').hover(function(){


 $(this).css('border-radius','50%');

},function(){

  $(this).css('background-color','white');
  $('body').css({'transform':'rotateY(-180deg)','background-color':'black'});
  
  
  
})
.l{

  padding: 5px;
  transition: 0.5s;
  width: 100px;
  height: 100px;
  background-color: yellow;

}

.openn{
   transition: transform 0.3s ease-in
 }


body{transition: 2s;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='l'></div>

The advantage of using the .hover() is that we can manipulate any element of the page without the need to manually declare the two events that would make this service. He is similar to :hover CSS but infinitely more powerful since we’re not stuck with just one element and its originality.

  • Thank you for your reply, to improve it, I would recommend that you put the css that has transition: transform 0.3s ease-in in the .openn for better animation.

  • To rotate only the element in question, instead of turning all elements with class .openn at once, replace the function body with $(this).css('transform', 'rotate(90deg)');, as commented in @Guilherme Nascimento’s reply

0

Although the question does not mention the tags jquery or javascript, a solution is to detect by jQuery when the transition ends and change the style of the element:

Using jQuery:

$(".openn")
.on("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd",
function(){
	$(this).css('transform','rotate(90deg)');
});
.openn{
	transition: transform 0.5s ease-in; 
}
.openn:hover{
	transform: rotate(90deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="openn" style="display: block; width: 100px; height: 100px; background: red;">
  <i class="fa fa-chevron-right fa-2x openn" aria-hidden="true"></i>
</div>

  • Forgive me, but I must point out that 99999999999s is a huge patch. PS: move the transition pro selector without hover, I think it gets better.

  • "but it works" is usually part of the excuse in situations where there is patching, sorry if it sounds gross, but it’s just my technical view of the situation. Anyway, being or not patching, I think it will work better if you put Transition on the selector .openn {} instead of .openn:hover {} ;) See you. PS: the downvote is not mine.

  • 2

    One of the votes is mine, because I also consider to be gambiarra. A solution like this will spend unnecessary browser resources finitely (see here). You don’t have to edit your solution, or delete it, let alone change the way you program, the negative votes are from people who disagree with what you did and that’s the spirit of the community.

  • @Andersoncarloswoss Blz, it had not attacked me to that.

  • Ah, now yes, a reasonable solution, fix the effect after the animation :D +1 (ps: it appears that it was not I who gave you downvotes, because I vote only for the quality of the post and not for nitpicking ;))

  • @Guilhermenascimento That’s good. I don’t usually give DV even when I see odd response (except some rare cases). I don’t like the arrogance of the answer when it wants to be a great answer and says nothing, or says a lot when it could say little. Kind of like filling sausages. I’ve noticed here that many experienced programmers do extensive answers to say very little just to get attention. Well, that’s not wrong, but anyone who does understands the trickery. I’m not saying it’s your case, on the contrary, I see your answers and I’ve been giving them mt +1 for being great.

  • @Dvd thanks. I don’t know what the answers refer to, but there are 3 users who usually post really big responses, I don’t know if it refers to any of them, however what you might assume is self-perpetuation may be something that we sometimes don’t understand what the author is talking about, these 3 users have already received similar accusations from other users, who said that they could make smaller answers, however although I do not master all the subjects they speak, when reading and researching I started to notice that a lot of things in the text were made necessary yes...

  • ... So we can’t confuse big technical responses that might have something that we don’t master to understand motivation with arrogance or with any other kind of attitude. I myself have read many questions that I do not know anything about, but when talking to the author of the answer (via chat) I sometimes got an understanding, from which I noticed that the reason for not understanding why the answer is because I did not really dominate the subject. I believe this is something valid for any participant of the site. Again thank you and good luck.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.