html5 - CSS3 transform:rotate on hover -


i have nice animation set have bullet shooting star rotates after hover on gun, works should but.......

after take mouse off gun star rotates other way, not :( ideas how stop?

i have tried use 'active' instead doesn't work animation.

css

#star {   width:48px;   height:49px;   position:absolute;   z-index:5;   left:922px;   top:63px;   -webkit-transition: 4s ease-out;   -moz-transition: 4s ease-out;   -o-transition: 4s ease-out;   -ms-transition: 4s ease-out;   transition: 4s ease-out; }     #gun:hover ~ #star {   -webkit-transform:rotatez(340deg);   -moz-transform:rotatez(340deg);   -o-transform:rotatez(340deg);   -ms-transform:rotatez(340deg);   transform:rotatez(340deg);   -webkit-transition-delay: 1s;   -moz-transition-delay: 1s;   -o-transition-delay: 1s;   -ms-transition-delay: 1s;   transition-delay: 1 } 

the nature of :hover css selector applies when hover happening on source element. reverse triggered when user no longer hovers because :hover no longer applies. there 2 ways achieve want:

  1. use animations instead. animations have animation-fill-mode, when set forwards causes element retain it's computed values set last keyframe encountered during execution. mdn has more info it.

    here's how you'd in css:

    #gun:hover ~ #star {     -webkit-animation: rotate 4s forwards;     animation: rotate 4s forwards; } @-webkit-keyframes rotate {     100% {         -webkit-transform: rotate(360deg);     } } @keyframes rotate {     100% {         transform: rotate(360deg);     } } 

    demo: http://jsfiddle.net/fpcmt/

  2. if don't want use animations, need write javascript. use hover event, because events don't depend on current state :hover does. notice moved transition-delay css #star, can apply element whole time no effect. i've used jquery succinctness:

    javascript:

    $('#gun').hover(function() {   $('#star').css('transform', 'rotatez(340deg)'); }); 

    css:

    #star {     width: 50px;     -webkit-transition: 4s ease-out;     -moz-transition: 4s ease-out;     -o-transition: 4s ease-out;     -ms-transition: 4s ease-out;     transition: 4s ease-out;     -webkit-transition-delay: 1s;     -moz-transition-delay: 1s;     -o-transition-delay: 1s;     -ms-transition-delay: 1s;     transition-delay: 1 } 

    demo: http://jsfiddle.net/fpcmt/4/

    --or--

    you can achieve vanilla javascript too. used css class called shot apply transform, since lacking jquery's cross-browser , cleaner way:

    javascript:

    var gun = document.getelementbyid('gun'); var star = document.getelementbyid('star');  gun.onmouseover = function () {     star.classname = 'shot'; }; 

    css: (in addition css jquery example)

    #star.shot {     -webkit-transform:rotatez(340deg);     -moz-transform:rotatez(340deg);     -o-transform:rotatez(340deg);     -ms-transform:rotatez(340deg);     transform:rotatez(340deg); } 

    demo: http://jsfiddle.net/fpcmt/6/


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -