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.
Related
I am trying to set the transition for the portfolio section of my web, I need the effects on hover for portfolio thumbs and i have the following codes in CSS:
.proimg img {
height: 100%;
max-width: 100%;
}
.proimg img:hover {
opacity: 0.5;
filter: alpha(opacity=50);
transition: all 0.55s ease-in-out;
}
That's the portfolio page http://goo.gl/Gaja7v
On hover, images didn't look good. Transition works but it messed up the thumbs, doesn't look good. I would like to make the transition to similar as this website http://goo.gl/0hb56Z
Anyone can help?
First of all, you have to resize list images for that!
--
I recommend jQuery, fadeTo function
//you have to include jquery lib
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
//HTML :
<img src="http://pjvarquitetura.com.br/wp-content/uploads/casad.jpg">
//Javascript :
<script>
$('img').mouseenter(function() {
$(this).fadeTo('fast', 0.7);
}).mouseleave(function(){
$(this).fadeTo('fast', 1);
});
</script>
you can get more information about fade to function
- http://www.w3schools.com/jquery/eff_fadeto.asp
If you don't want to use fadeTo function.
//CSS
.fadeeffect {
-webkit-transition: opacity 250ms ease-in-out;
-moz-transition: opacity 250ms ease-in-out;
-o-transition: opacity 250ms ease-in-out;
transition: opacity 250ms ease-in-out;
}
//Javascript
$(document).ready(function() {
$('img').mouseenter(function() {
$(this).css('opacity', 0.7);
}).mouseleave(function(){
$(this).css('opacity', 1);
});
});
//HTML
<img class="fadeeffect" id="a" src="http://pjvarquitetura.com.br/wp-content/uploads/casad.jpg">
It's easy to think of the transition property as an "action": eg, "When this :hover state begins, transition the given properties." But you really need to think of it as a constant state, which means "when the following properties change, for any reason, transition them in this manner."
So you really want the transition property to be on your first CSS rule, so that it always applies. Otherwise, the transition is only when the mouse starts to hover, not when it leaves.
You need to set the transition property to your img as well:
.proimg img {
height: 100%;
max-width: 100%;
transition: all 0.55s ease-in-out; /* this line */
}
.proimg img:hover {
opacity: 0.5;
filter: alpha(opacity=50);
transition: all 0.55s ease-in-out;
}
http://codepen.io/anon/pen/ZbPKVR
This is explained by Katana314's answer.
I am transitioning a couple of background images. However, I am noticing that when if the user acts before the transition has fully taken affect, the transition is instant.
For example, I hover over the div and the image takes 1 second to completely transition. However, I remove the cursor .5s into the transition. So it transitions back to the original image but instead of smoothly transitioning, it changes instantly. How do I make it smooth at all times?
.class {
width:500px;
height:500px;
background-image:url("http://activite-paranormale.net/extended/photo/news/nature-harmony-girl.jpg");
-webkit-transition: all 1s ease 0;
-moz-transition: all 1s ease 0;
-o-transition: all 1s ease 0;
transition: all 1s ease 0;
}
.class:hover {
background-image:url("http://images4.fanpop.com/image/photos/22700000/Spring-beautiful-nature-22727146-500-500.jpg");
}
<div class="class"></div>
http://codepen.io/john84/pen/vEdPEW
fiddle
I changed ease to ease-in-out
edit: Firefox doesn't support this, but there are many workarounds, example:
fiddle
HTML:
<div class="class"></div>
<div class="class2"></div>
CSS:
.class {
position:absolute;
width:500px;
transition:all 500ms;
height:500px;
background-image:url("http://activite-paranormale.net/extended/photo/news/nature-harmony-girl.jpg");
}
.class2{
width:500px;
height:500px;
background-image:url("http://images4.fanpop.com/image/photos/22700000/Spring-beautiful-nature-22727146-500-500.jpg");
}
.class:hover {
opacity:0;
}
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
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.
I'm in the middle of making a site that will showcase my graphic work. For one of the thumbnails of my work i've got it to do this
.example1:not:hover {
opacity:1;
-webkit-transition: all .15s linear;
-moz-transition: all .15s linear;
transform:rotate(-7deg);
-ms-transform:rotate(-7deg);
-webkit-transform:rotate(-7deg);
transition: transform 0.25s linear;
}
now, i want it to do
transform:rotate(7deg);
-ms-transform:rotate(7deg);
-webkit-transform:rotate(7deg);
when you take your mouse of the thumbnail
The html of the thumbnail is
<div id ="maintext">
<img src="Images/example.png" class="example1" >
</div>
thanks all.
No there is no explicit property for mouse leave in CSS. Now I understood what you want.
jQuery
$('.example1').on('mouseleave', function () {
var $this = $(this);
$this.removeClass('mouseenter');
$this.addClass('mouseleave');
window.setTimeout(function () {
$this.removeClass('mouseleave');
}, 150);
}).on('mouseenter', function () {
$(this).addClass('mouseenter');
});
CSS
.example1 {
-webkit-transition: all .15s linear;
-moz-transition: all .15s linear;
transition: transform .15s linear;
transform:rotate(0deg);
-ms-transform:rotate(0deg);
-webkit-transform:rotate(0deg);
}
.example1.mouseenter {
transform:rotate(7deg);
-ms-transform:rotate(7deg);
-webkit-transform:rotate(7deg);
}
.example1.mouseleave {
transform:rotate(-7deg);
-ms-transform:rotate(-7deg);
-webkit-transform:rotate(-7deg);
}
Live Example:
JS Fiddle
First of all, you can't use the :not selector like that.
If you want to specify something that is not hovered, you don't need any pseudoclass. I think you want something like this:
.example1 {
/* no rotation */
}
.example1:hover {
transform:rotate(7deg);
-ms-transform:rotate(7deg);
-webkit-transform:rotate(7deg);
}
Then, if you want a different state after it has been hovered, you'll need to add a class with javascript:
$(document).on("mouseleave", ".example1", function() {
$(this).addClass("hovered");
});
Now you can have a CSS class like this:
.example1.hovered {
transform:rotate(-7deg);
-ms-transform:rotate(-7deg);
-webkit-transform:rotate(-7deg);
}
Put what you want for the non-hover state in just .example1 { ... }. Then put the hover state in .example1:hover { ... }. So, if you want to make images rotated when not hovering, do it like this:
.example1 {
opacity:1;
-webkit-transition: all .15s linear;
-moz-transition: all .15s linear;
transform:rotate(-7deg);
-ms-transform:rotate(-7deg);
-webkit-transform:rotate(-7deg);
transition: transform 0.25s linear;
}
.example1:hover {
/* set all transforms to none or whatever you want when hovering */
}