CSS3 transform:rotate on hover - html

I have a nice animation set up so I have a bullet shooting a star that then rotates all after you hover over the gun, All works as it should but.......
After you take the mouse off the gun the star rotates the other way, Not good :( any ideas how to get it to stop?
I have tried to use 'active' instead but that doesn't work with an animation.
CSS
#star {
width:48px;
height:49px;
position:absolute;
z-index:5;
left:922px;
top:63px;
-webkit-transition: all 4s ease-out;
-moz-transition: all 4s ease-out;
-o-transition: all 4s ease-out;
-ms-transition: all 4s ease-out;
transition: all 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 the :hover css selector is that it only applies when the hover is happening on the source element. So the reverse is triggered when the user no longer hovers because the :hover no longer applies. There are two ways to achieve what you want:
Use animations instead. Animations have animation-fill-mode, which when set to forwards causes an element to retain it's computed values set by the last keyframe encountered during execution. MDN has more info about it.
Here's how you'd do it in your 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/
If you don't want to use animations, you need to write some JavaScript. Use the hover event, because events don't depend on current state like :hover does. You will also notice I moved the transition-delay css to #star, as it can apply to that element the whole time to no effect. I've used jQuery for succinctness:
JavaScript:
$('#gun').hover(function() {
$('#star').css('transform', 'rotateZ(340deg)');
});
CSS:
#star {
width: 50px;
-webkit-transition: all 4s ease-out;
-moz-transition: all 4s ease-out;
-o-transition: all 4s ease-out;
-ms-transition: all 4s ease-out;
transition: all 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 this with vanilla JavaScript too. I used a CSS class I called shot to apply the transform, since we are lacking jQuery's cross-browser help and it is cleaner that way:
JavaScript:
var gun = document.getElementById('gun');
var star = document.getElementById('star');
gun.onmouseover = function () {
star.className = 'shot';
};
CSS: (in addition to CSS from 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/

You can simply specify different transition-delay for transition from non-hovered state to hovered and from hovered to non-hovered. Very large delay for the latter transition, e.g.
#star {
/*
other styles here
*/
transition-delay: 9999s;
}
will make the transition appear to be "one way". Here is the JSFiddle example.

Related

Transition for Text

I really love transition in html but i do not know how to do a transition with text or h2. I know for images as it is
img {
opacity:0;
-moz-transition: opacity 2s; /* Firefox 4 */
-webkit-transition: opacity 2s; /* Safari and Chrome */
-o-transition: opacity 2s;
transition: opacity 2s;
}
then
<img onload="this.style.opacity='1';" src="https://s13.postimg.org/6p9r8rtrr/fgh.jpg" style="width:640px;height:360px;"/>
The Problem is when i try this with h2 or text it doesn't work:
h2 {
opacity:0;
-moz-transition: opacity 2s; /* Firefox 4 */
-webkit-transition: opacity 2s; /* Safari and Chrome */
-o-transition: opacity 2s;
transition: opacity 2s;
}
then
<h2 onload="this.style.opacity='1';">What Person/Character are you thinking of?</h2>
But this doesn't work. Can anyone help me?
Pictures take some time to load, but text doesn't have same behavior and onload isn't currently supported by text elements.
Check out CSS3 animation effects for elements on W3schools tutorial.
Snippet
h2 {
opacity: 0;
animation: fadein 2s forwards;
}
#keyframes fadein {
from { opacity: 0; }
to { opacity: 1; }
}
<h2>What Person/Character are you thinking of?</h2>

Animating an Angular js button

I'm using an angular js button, but i cant seem to use conventional css&js methods to put animations on it..i'm trying to implement an opacity animation on the button.
can anyone please help?
HTML
<span id="sign_btn">
<md-button>Button</md-button>
</span>
CSS:
#sign_btn{
-webkit-transition: opacity 0.2s ease-in-out;
-moz-transition: opacity 0.2s ease-in-out;
-o-transition: opacity .2s ease-in-out;
-ms-transition: opacity .2s ease-in-out;
transition: opacity .2s ease-in-out;
display:none;
opacity:0;
}
JS:
$("#sign_btn").css('display', 'block');
$("#sign_btn").css('opacity', '1');
You should use animation instead of transition.
First, create a custom animation
#-webkit-keyframes opanimation {
0% {
opacity:0;
}
100% {
opacity:1;
}
}
#-moz-keyframes opanimation {
0% {
opacity:0;
}
100% {
opacity:1;
}
}
#-o-keyframes opanimation {
0% {
opacity:0;
}
100% {
opacity:1;
}
}
#keyframes opanimation {
0% {
opacity:0;
}
100% {
opacity:1;
}
}
Then apply it to you element
#sign_btn {
animation: opanimation 5s; //you can modify the seconds here
}
Check this fiddle https://jsfiddle.net/2up5y71k/
Transitions only work for changes from one visible state to another. Your button is initially display:none; so the opacity change is not considered as a change in opacity from one state to another. Remove it (use other techniques like positioning, z-index, translate etc to achieve similar effect) and the transition should work.
found out the solution..
$("#sign_btn").delay(0).animate({"opacity": "1"}, 200);

How to delay the disappearance of an element on mouseout

I got a li element on my page which is <li class="basket last items">.
If the user hovers over that li another div is shown which is <div class="articles">. I want to delay the disappearance of the div on mouseout.
My current css rules:
#headlinks li.basket div.articles {
padding:5px;
width:380px;
z-index:100;
position:absolute;
border:1px solid #405ade;
-webkit-transition: display .5s all;
-webkit-transition-delay: 5s;
-moz-transition: .5s all;
-moz-transition-delay: 5s;
-ms-transition: .5s all;
-ms-transition-delay: 5s;
-o-transition: .5s all;
-o-transition-delay: 5s;
transition: .5s all;
transition-delay: 5s;
}
#headlinks li.basket:hover div.articles {
z-index:1000;
display:block;
background-color:#fff;
-webkit-transition-delay: 0s;
-moz-transition-delay: 0s;
-ms-transition-delay: 0s;
-o-transition-delay: 0s;
-transition-delay: 0s;
}
I thought with that rules the mouseout should be delayed by 5 seconds but it's not working.
Edit: Here is a jsfiddle of my problem http://jsfiddle.net/21tn6bq6/ I left out unnecessary css but basically that's my problem. I want the div to stay for some more seconds after mouseout.
The display property is not "animable", the transitions don't work with it. You need to change it to opacity or something else. And also you need to swap the transitions properties to get the effect that you want.
FIDDLE: https://jsfiddle.net/21tn6bq6/4/
I think you have your delays switched. Your current CSS shows a delay on mouseover, not mouseout

background color hover fade effects

Using this css code
#onec-fourth:hover {
background-color: #F36DE1;
color: white;
}
I want that when I move the mouse off the object (#onec-fourth),
for the background color & the color text will persist for 1 second.
Because right now, when I move my mouse off it is stopped.
How do I make the :hover effect persist a short duration?
This kind of task can be easily realized with a simple CSS transition, no Javascript is needed (unless you need to support older browsers, but the basic effect will work anyway):
Example: http://codepen.io/anon/pen/nzLkf (tested on Firefox29 and Chrome35)
CSS code
#onec-fourth {
width: 300px;
height: 300px;
border: 2px dashed #ddd;
-webkit-transition: all 0s linear 1s;
-moz-transition: all 0s linear 1s;
-ms-transition: all 0s linear 1s;
transition: all 0s linear 1s;
}
#onec-fourth:hover{
background-color:#F36DE1;
color:white;
-webkit-transition-delay: 0s;
-moz-transition-delay: 0s;
-ms-transition-delay: 0s;
transition-delay: 0s;
}
For a fade-out effect (after 1 second) see instead http://codepen.io/anon/pen/AkCcm
(use transition: all 1s linear 1s and transition: all 0s linear 0s on :hover):
just play with with transition-duration and transition-delay values as you prefer, until you achieve the optimal result.
Further information on CSS transitions can be found on MDN
As a simple demonstration of the technique that might help you:
#onec-fourth {
background-color: #fff;
/* vendor prefixes stripped for brevity;
sets the transition for the background-color property: */
transition: background-color 1s linear;
transition-delay: 1s; /* delays that transition for 1 second */
}
#onec-fourth:hover {
background-color: #ffa;
}
JS Fiddle demo.
References:
CSS transitions.
transition-delay.
if you want to apply more styles than just one you may use addClass/removeClass:
<style>
.hov {
background-color:#F36DE1;
color:white;
}
</style>
and jquery code:
<script>
$(document).ready(function () {
$("#onec-fourth").mouseenter(function () {
$("#onec-fourth").addClass("hov");
});
$("#onec-fourth").mouseleave(function () {
setTimeout(function () {
$("#onec-fourth").removeClass("hov");
}, 1000);
});
});
</script>
http://jsfiddle.net/6zSJa/8/
Demo Fiddle
var self;
$('#onec-fourth').mouseover(function(){
self = $(this);
self.css('background-color','red');
}).mouseleave(function(){
setTimeout(function(){
self.css('background-color','blue');
},1000);
});
Using jQuery setTimeout() , mouseover() and mouseleave(). Check the links for more details.

Replacing one css3 animation with another

I want to be able to set an animation effect for a certain image on my site (slow transition of the background position). But after a certain user interaction, I want to be able to remove that animation and switch to a different one. How do I accomplish this?
You can add a class to it:
.element{
animation: first 1s forwards;
}
.element.clicked{
animation: second 1s forwards;
}
This way, you will not have to remove a class. With jQuery, just use this:
$('.element').click(function(){$(this).addClass('clicked');});
You add and remove a class with the animation properties
.classOne {
animation: one 5s forwards;
}
.classTwo {
animation: two 3s forwards;
}
and the javascript to do so
var obj = document.getElementById('animatedElement');
obj.onclick = function() {
obj.classList.remove('classOne');
obj.classList.add('classTwo');
}
the equivalent jQuery
$('#animatedElement').on("click", function() {
$(this).removeClass('classOne').addClass('classTwo');
}
Using :not selector and toggling an element class you can do something like this to change current element animation:
#xpto:not(.anim) {
-webkit-transition:background-position 1s ease;
-moz-transition:background-position 1s ease;
-o-transition:background-position 1s ease;
}
#xpto.anim {
-webkit-transition:opacity 1s ease-in-out;
-moz-transition:opacity 1s ease-in-out;
-o-transition:opacity 1s ease-in-out;
}
#xpto:not(.anim):hover {
background-position: 0 0;
}
#xpto.anim:hover {
opacity: 0.2;
}
See this working demo