Trigger mouse leave when animated sprite is clicked - css

I have a link containing a sprite image, with a CSS animation on mouse hover.
a {
display:inline-block;
width:48px;
height:48px;
overflow:hidden;
}
a img {
-webkit-transition: 250ms ease-out;
-moz-transition: 250ms ease-out;
-o-transition: 250ms ease-out;
transition: 250ms ease-out;
}
a:hover img{
-webkit-transform: translate(0,-50%);
-moz-transform: translate(0,-50%);
-ms-transform: translate(0,-50%);
-o-transform: translate(0,-50%);
transform: translate(0,-50%);
}
Animation works fine, but when I click the link, it remains on hover state.
Here's a fiddle showing the problem. When you click the link, a new tab is opened as expected. But when you come back to the fiddle, the sprite is still in hover state. How can I bring it back to normal (not-hover) state? I've tried to trigger mouseleave event with jQuery, but it failed.

The event is fired correctly but the CSS still sees it as ':hover' state (and I don't think you can change that with javascript). Therefore you could change your css from
a:hover img {
to
a.hovered img {
thus setting it as a class on its own and not a css state. Then in your JS you can leave the click handler as before (even though I used 'mouseout' but it shouldn't matter) and add a hover() handler like
$('a').click(function() {
$(this).trigger('mouseout');
});
$('a').hover(function() {
$(this).addClass('hovered');
}, function(){
$(this).removeClass('hovered');
});

Customize hover behavior with jQuery instead of CSS :hover pseudo class:
<a id='theLink' href="http://www.facebook.com" target="_blank"><img src="http://bradsknutson.com/wp-content/uploads/2013/05/facebook-hover.png" alt="Facebook"/></a>
a {
display:inline-block;
width:48px;
height:48px;
overflow:hidden;
}
a img {
-webkit-transition: 250ms ease-out;
-moz-transition: 250ms ease-out;
-o-transition: 250ms ease-out;
transition: 250ms ease-out;
}
.hover{
-webkit-transform: translate(0,-50%);
-moz-transform: translate(0,-50%);
-ms-transform: translate(0,-50%);
-o-transform: translate(0,-50%);
transform: translate(0,-50%);
}
$('#theLink').mouseenter(function(){
$(this).children().first().addClass('hover');
}).mouseleave(function(){
$(this).children().first().removeClass('hover');
}).click(function(){
$(this).children().first().removeClass('hover');
});
See it in action: http://jsfiddle.net/gssw0dm6/

Related

CSS transition is not working in Safari

When you hover over image1div, it scales to 0.95 and fades to 80% opacity. It works in Chrome and Firefox but not Safari. It fades and scales instantly in Safari rather than smoothly in 0.5s.
.image1div {
width: 350px;
height: 350px;
margin-top: 0px;
float: right;
background-color: #5a89ad;
background-size: cover;
filter:alpha(opacity=100);
-webkit-transform: scale(1,1);
-ms-transform: scale(1,1);
transform: scale(1,1);
-webkit-transition: opacity 0.5s ease, transform 0.5s ease;
transition: opacity 0.5s ease, transform 0.5s ease;
}
.image1div:not(.no-hover):hover {
-webkit-transform: scale(0.95,0.95);
-ms-transform: scale(0.95,0.95);
transform: scale(0.95,0.95);
opacity:0.8;
filter:alpha(opacity=80);
}
I think it has to do with the filter property.
Transition is supported by safari: http://caniuse.com/#feat=css-transitions
Also the filter property, but you need to add a prefix: http://caniuse.com/#feat=css-filters
Let me know if it helps, if not, provide more details and we will find a workaround.
-- EDIT
Instead of transition: opacity, transform. Use all, or check out how you can add multiple properties CSS transition shorthand with multiple properties?

CSS Translate On Normal

I'm making a modal popup in CSS that will appear and and translate down (translateY) after clicking on the link. My problem is popup did appear but it already at downside. I know how to make translate on hover but I cant figure out how to translate this without hover. Can anyone give me some advice?
HTML
<a href='#A'>Open</a>
<div id='A' class='B'>
<div class='C'>
Test</div>
</div>
CSS
.B {
position:fixed;
top:0px;
right:0px;
bottom:0px;
left:0px;
background:rgba(0,0,0,0.8);
opacity:0;
z-index:99999;
pointer-events:none;
}
.B:target {
opacity:1;
pointer-events:auto;
}
.B > .C {
width:100px;
height:100px;
background:#FFF;
background:-moz-linear-gradient(#FFF, #999);
background:-webkit-linear-gradient(#FFF, #999);
background:-o-linear-gradient(#FFF, #999);
-webkit-border-radius:15px;
-moz-border-radius:15px;
-ms-border-radius:15px;
-o-border-radius:15px;
border-radius:15px;
position:relative;
}
.C {
-webkit-transition: all 1s ease-out;
-moz-transition: all 1s ease-out;
-ms-transition: all 1s ease-out;
-o-transition: all 1s ease-out;
transition: all 1s ease-out;
-webkit-transform: translate(0px, 50px);
-moz-transform: translate(0px, 50px);
-o-transform: translate(0px, 50px);
-ms-transform: translate(0px, 50px);
transform: translate(0px, 50px);
}
JSFiddle
http://jsfiddle.net/M5tcL/16/
CSS transitions make changes on a property's value appear smooth to the viewer (tween?).
You don't ever change a css property's value here, so there is nothing to animate.
There has to be two css states with different values. You need javascript or a checkbox to make this work.
You can use javascript to toggle a class on the body element for example, or a checkbox with an associated label.
Then you can use
/* Normal state */
.C{
opacity: 0;
transition: all .3s;
}
/* Open state */
:checked ~ .C,
/* OR */
body.open .C{
opacity: 1;
}
In this snippet example, opacity will get animated while changing between 0 and 1.

Simple hover effect not working on mobile safari

I m trying to make a simple hover effect with some images in an html website.
The plan is : When you hover over an image , the image shades and some details appear.
The css code is this.
/* Hover overs */
.folio4:hover .face {
opacity:1;
cursor: pointer;
}
.folio4:hover img {
opacity: 1;
}
.folio4:hover h2 {
opacity: 1;
}
.folio4:hover a.icon {
opacity:1;
transform: translateY(75px);
-webkit-transform: translateY(75px);
-o-transform: translateY(75px);
-ms-transform: translateY(75px);
transition: 500ms;
-webkit-transition: 500ms;
-o-transition: 500ms;
-ms-transition: 500ms;
}
.folio4:hover a.icon2 {
transition: 500ms;
-webkit-transition: 500ms;
-o-transition: 500ms;
-ms-transition: 500ms;
transform: translateY(75px);
-webkit-transform: translateY(75px);
-o-transform: translateY(75px);
-ms-transform: translateY(75px);
opacity: 1;
}
It works on all browsers except mobile Safari. What can I do for this to work?
Thanks in advance for every answer.
As far as I know it, the hover effect can not work with mobile devices, due to the fact that the system does not see the hover effect, as you know it from your computer.
The only thing you can do is, is to set that hover effect as a on click effect only for the responsive design of your page, so the user has to tap on a picture and gets the details then.
Sorry if this is not the solution

I Want to Fade in and Rotate at the Same Time

I'm using ccs3 to fade in an image on hover. I'd like that same image that fades in on hover to rotate. I seem to be missing something.
Here is a jsfiddle. http://jsfiddle.net/5ftZ7/
<div id="cf">
<img class="bottom" alt="" src="http://s513195336.onlinehome.us/wp-content/uploads/2014/02/pin-over.png" /> <img class="top" alt="" src="http://s513195336.onlinehome.us/wp-content/uploads/2014/02/pin.png" />
</div>
#cf {
position:relative;
margin:30px auto;
width:200px;
height:200px;
}
#cf img {
margin-top:30px;
position:absolute;
left:0;
top:0;
-webkit-transition: opacity .2s ease-in-out;
-moz-transition: opacity .2s ease-in-out;
-o-transition: opacity .2s ease-in-out;
transition: opacity .2s ease-in-out;
-webkit-transition: -webkit-transform 0.2s ease-in;
}
#cf img.top:hover {
opacity:0;
position:absolute;
left:0;
top:0;
-ms-transform: rotate(30deg); /* IE 9 */
-webkit-transform: rotate(30deg); /* Chrome, Safari, Opera */
transform: rotate(30deg);
}
There are a variety of issues that culminate in this not working the way you want:
Understanding of transition rules
CSS properties cannot accumulate. There is nothing special about the transition rule:
transition: opacity .2s ease-in-out;
transition: transform .2s ease-in-out;
The second declaration overrides the first. This would be no different than:
color: red;
color: blue;
being blue. You can use multiple comma-separated transition definitions, or use the special all property:
transition-property: opacity, transform;
transition-duration: .2s;
transition-timing-function: ease-in-out;
/* or */
transition: opacity .2s ease-in-out, transform .2s ease-in-out;
/* or, but this may affect properties you do not want */
transition: all .2s ease-in-out
:hover with stacked elements.
.top is on top of .bottom, so .bottom cannot be hovered even when .top is transparent. This prevents rules that you would want to apply to .bottom from being applied. The simplest solution to this is just to check for :hover on #cf instead, as in #cf:hover img.top as the selector.
transform missing from .bottom
.bottom also needs the transform rules when it is hovered so it can rotate in sync with .top.
Here is a working example using only -webkit and increasing the transition durations for effect.
http://jsfiddle.net/ExplosionPIlls/5ftZ7/1/
I guess what you are trying to achieve is this:
Fiddle: http://jsfiddle.net/marionebl/5ftZ7/2/.
Includes -webkit- only for brevity. What this does:
Uses the former .bottom as first layer in stack
Replaces .bottom with a html element mimicking the image in your fiddle. Could be a png with transparency, too.
Listen for :hover state on #cf instead of .bottom or .top
Fade the former .bottom in, rotate the former .top
you can't use several transitions on an element,
if you want to apply transition to several properties you can use "all"
transition: all 1s;
or comma separated transition:
transition: opacity 1s, transform 0.8s;
#keyframes rotation {
0% {
transform: rotate(0deg);
opacity: 0;
}
100% {
transform: rotate(359deg);
opacity: 1;
}
}

FireFox transform and transition not working

So, this is my CSS:
img.buttonImg {
-webkit-transition: all 0.5s ease-in-out;
-moz-transition: all 0.5s ease-in-out;
-o-transition: all 0.5s ease-in-out;
-ms-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
}
img.buttonImg:hover {
-webkit-transform: rotate(360deg);
-moz-transform: rotate(360deg);
-o-transform: rotate(360deg);
-ms-transform: rotate(360deg);
transform: rotate(360deg);
}
Yet no animation seems to happen, the image isn't rotating at all on FireFox, but on other browsers it does.
Here is your problem - demonstrated by this example.
The transition doesn't work when hovering over the img element because of the fact that it is within a button element. I assume this is a rendering issue, as this only seems to be the case for FF. It works fine in Chrome and other modern browsers.
As for a solution, removing the img element from the button will obviously solve the problem.
Alternatively, you could add the rotation transition effect when hovering over the button as opposed to the child img element. Updated example - it works in FF.
button.t:hover img {
transform: rotate(360deg);
/* other vendors.. */
}
Both solutions work; however, I don't even know if it is valid to have an img element within a button element. This is probably the reason for the rendering bug; if it even is a bug.

Resources