Enable hover event after certain time [duplicate] - javascript

This question already has answers here:
How to add delay to jquery mouseover? [duplicate]
(5 answers)
Closed 7 years ago.
I would like to disable hover event on an anchor tag via jQuery for a certain interval.
example: I've got some CSS styles for a hover effect on anchor (< a href="">) element. When I hover the element I want it to wait certain time (500ms or so) and then to start rendering the hover effect.
Something like this maybe?
$("#navigation").hover(function () {
$("li.has-submenu").off('hover');
setTimeout(function () {
$("li.has-submenu").on('hover');
}, 1000);
});

I checked recently, and it's 2015, this means that you can use CSS3 for this.
The following will start animating background color of all links after 500ms.
No jQuery or any JavaScript needed.
a {
-webkit-transition: all 500ms ease-in;
-moz-transition: all 500ms ease-in;
-ms-transition: all 500ms ease-in;
-o-transition: all 500ms ease-in;
transition: all 500ms ease-in;
}
a:hover {
background-color: #c00;
transition-delay: 500ms;
}
Hover me
If, however you absolutely need to do that with JavaScript, you can use setTimeout in to apply a hovered class to the element:
jQuery(function($) {
$("a").hover(function() {
var el = $(this);
var timeout = setTimeout(function() {
el.addClass("hover");
}, 500);
el.data("timeout", timeout);
}, function() {
clearTimeout($(this).removeClass("hover").data("timeout"));
});
});
a {
-webkit-transition: all 500ms ease-in;
-moz-transition: all 500ms ease-in;
-ms-transition: all 500ms ease-in;
-o-transition: all 500ms ease-in;
transition: all 500ms ease-in;
}
a.hover {
background-color: #c00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Hover me

I guess you want something like this:
<div id='a' style="border:2px solid black" >
<h3>Hove On me</h3>
For 2000 milliseconds. You will get an alert.
</br>
</div>
$(document).ready(function(){
var delay=2000, setTimeoutConst;
$('#a').hover(function(){
setTimeoutConst = setTimeout(function(){
/* Do Some Stuff*/
alert('Thank You!');
}, delay);
},function(){
clearTimeout(setTimeoutConst );
})
})
DEMO: http://jsfiddle.net/faj81qa0/

Related

Javascript - changing background color opacity with time interval

I am trying to add the background color to a .top-barz element when I click on another element, but I would like to make that as an animation in duration of 1s. I am pretty new to javascript and not sure how to do that?
I would like to animate from change opacity of rgba(36,36,36, .1) to rgba(36,36,36, 1)
I have come up with this code and put it into my on click function, but this is obviously not working:
var topBar = setInterval(function(){ topBarBackground() }, 1000);
function topBarBackground() {
for (var i = 1; i <= 9; i++) {
$('.top-barz').css('background-color', 'rgba(36,36,36,.' + i + ')');
}
}
clearInterval(topBar);
You may consider the fadeIn function of jQuery.
$('.top-barz').fadeIn(10000);
Here is some sample code to get you started
JQuery
$('.button').on('click', function() {
$('.top-barz').addClass('new-color');
});
CSS
.top-barz {
background-color:#000;
-webkit-transition: background-color 1s linear;
-moz-transition: background-color 1s linear;
-o-transition: background-color 1s linear;
-ms-transition: background-color 1s linear;
transition: background-color 1s linear;
}
.top-barz.new-color {
background-color:#eee;
}
Obviously you would change the colors to whatever color you want for your design.
EDIT
Here is the Fiddle
Seems to be working fine in chrome on my end
Michael McCoy is totally right in his comment. I would do the same as you will also benefit from GPU acceleration if you use CSS and it will make your code lighter.
This apart, your code has 2 errors:
missing i++
missing var i
_
function topBarBackground() {
for (var i = 1; i < 9; i++) {
$('.top-barz').css("background-color", "rgba(36,36,36,." + i + ")");
}
}
var myVar = setInterval(function(){topBarBackground()}, 1000);
Anyway, drop this idea.
So to add class just do $('.top-barz').addClass('changedColor');
and in css:
.top-barz {
background-color: rgba(36,36,36,.1);
-webkit-transition: 1s;
-o-transition: 1s;
transition: 1s;
}
.top-barz.changedColor {
background-color: rgba(36,36,36,1);
}

Animation inside Block in hidden state

Does animation work if the block content is in state "none"?
For example, if I want to use Load with JQuery, and I want animation to start after the page load, will this work?
.container {
display : none;
}
.container .animate {
transform : translate(0,-100px);
transition : 1s transform ;
}
.show {
display : block ;
}
in jquery
$(function() {
$(".container").addClass("show");
});
If there is another way please help me.
Looks like display:none elements can be animated...
Here's a test : the text is hidden, translates to the right, then shows up : it works.
$("p").addClass("shift");
setTimeout( function(){
$("p").css("display","block");
}, 1000)
p{
display:none;
border:green solid 1px;
width:150px;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
p.shift{
transform : translate(300px,0);
-webkit-transform: translate(300px,0);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>some text</p>
Is this what you want?
Just try to change the attr
$(function() {
$(".container").attr("display","block !important");
});

Rotate background using jquery [duplicate]

I have taken a look through stackoverflow for hours now, and I have found a topic similar to what I am trying to achieve
JavaScript, How to change the background of a div tag every x seconds
When I modify this solution in my script, I get no background image at all so I thought I would come back to the source to get some fresh eyes and thoughts.
Here is my setup:
HTML
<div id="Background"></div>
CSS:
.background-1 {
background: url('Images/mybg1.jpg');
-webkit-transition: background 500ms linear;
-moz-transition: background 500ms linear;
-o-transition: background 500ms linear;
-ms-transition: background 500ms linear;
transition: background 500ms linear;
}
.background-2 {
background: url('Images/mybg2.jpg');
-webkit-transition: background 500ms linear;
-moz-transition: background 500ms linear;
-o-transition: background 500ms linear;
-ms-transition: background 500ms linear;
transition: background 500ms linear;
}
Javascript in the head tag
<script>
$(document).ready(function(){
setInterval(function() {
if($('#background').hasClass('background-1')) {
$('#background').addClass('background-2');
setTimeout(function() {
$('#backdrop').removeClass('background-1');
}, 1000);
}
else if($('#background').hasClass('background-2')) {
$('#background').addClass('background-1');
setTimeout(function() {
$('#backdrop').removeClass('background-2');
}, 1000);
}
}, 2000);
});
I want to somehow be able to use several images for the background and have them change out every 5 or so seconds. How do I do this?
Here is what I have tried
HTML
<div id="Background">
<img src="<%=skinpath%>/images/mybg2.jpg" class="bgM"/>
<img src="<%=skinpath%>/images/mybg1.jpg" class="bgM"/>
</div>
CSS:
#Background, img.bgM {
background:#fff no-repeat 0 bottom;position:absolute;bottom:0;min-width:960px;min-height:100%;z-index:-99999;
background-position: top center;
}
Javascript:
<dnn:DnnJsInclude runat="server" FilePath="js/jquery.cycle.all.js" PathNameAlias="SkinPath" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.3.min.js"></script>
$(document).ready(function() {
$('#Background').cycle({
fx: 'fade',
pager: '#smallnav',
pause: 1,
speed: 1800,
timeout: 3500
});
});
This last solution worked however I could not get the image to position top center(even when specified in the css.) Any help on this is greatly appreciated!
You could try using different classes for each image, and then just swap out CSS classes in a given interval.
$(document).ready(function(){
var seconds = 5000;
var step = 0;
var limit = 5;
$("#Background").addClass("image-"+step);
setInterval(function(){
$("#Background").removeClass("image-"+step);
step = (step > limit) ? 0 : step + 1;
$("#Background").addClass("image-"+step);
},seconds);
});
I'm not sure what kind of animation you are trying to do, but you could fadeOut and then fadeIn.
$(document).ready(function(){
var seconds = 5000;
var step = 0;
var limit = 5;
$("#Background").addClass("image-"+step).fadeIn(500);
setInterval(function(){
$("#Background").fadeOut(500,function(){
$(this).removeClass("image-"+step);
step = (step > limit) ? 0 : step + 1;
$("#Background").addClass("image-"+step).fadeIn(500);
});
},seconds);
});
You can try swapping backgrounds like so:
Create CSS Class for each background and include transitions as part of the ruleset:
.background-1 {
background: url('background-1.jpg');
-webkit-transition: background 500ms linear;
-moz-transition: background 500ms linear;
-o-transition: background 500ms linear;
-ms-transition: background 500ms linear;
transition: background 500ms linear;
}
Add a script that will swap out the classes on a regular interval. (You would most likely wrap this script within $(document).ready) Since your transitioning backgrounds, you may not want the previous background immediately removed, so you can use a delay call to remove it after the transition would complete.
The following example would start the background switch every two seconds, and remove the previous background after 1 second. Since in my CSS, I declared that the transition should take .5 seconds, the new background is already there before the old one is removed:
setInterval(function() {
if($('#background').hasClass('background-1')) {
$('#background').addClass('background-2');
setTimeout(function() {
$('#background').removeClass('background-1');
}, 1000);
}
else if($('#background').hasClass('background-2')) {
$('#background').addClass('background-1');
setTimeout(function() {
$('#background').removeClass('background-2');
}, 1000);
}
}, 2000);
Note: The code that I've provided would require you to chain multiple if/else if statements together, there is probably a more efficient way to go about this.
I've done something similar (background colors instead of images) as a teaser for an upcoming project of mine, you can check it out here.
Update: Forgot to mention, make sure to declare one of the background classes in your Div element:
<div id="background" class="background-1"></div>
It sounds like you're trying to do a slide show. I highly recommend the JQuery Cycle Plugin or Cycle2. The Cycle2 Getting Started page has a nice incremental demo/tutorial.

Fade two images with different delay

I'm trying to fade 2 different images on the same page with a different delay. The first image appears and then the second one appears.
Here's my fiddle :http://jsfiddle.net/jarod51/4RvWY/3/
the css:
.panel img {
opacity:0;
-moz-transition: opacity 3000ms ease-in-out;
-webkit-transition: opacity 3000ms ease-in-out;
transition: opacity 3000ms ease-in-out;
}
.shown img{
opacity: 1;
}
.img2{
opacity:0;
-moz-transition: opacity 10000ms ease-in-out;
-webkit-transition: opacity 10000ms ease-in-out;
transition: opacity 10000ms ease-in-out;
}
.shown1 img2{
opacity: 1;
}
the html :
<div id="home" class="panel">
<h2>Home</h2>
<img src="http://lorempixum.com/200/200/people/3"/>
<img class="img2" src="http://lorempixum.com/200/200/people/1"/>
</div>
my jquery attempt:
$('#wrap').find('.shown').removeClass('shown');
$target.addClass('shown');
$('#wrap').find('.shown1').removeClass('shown1');
$target.addClass('shown1');
There's a couple of things you may fix to get it working:
1) You're missing a dot (.) before the img2 in the .shown1 img2 rule. You're referring to a class and not to an HTML tag. That must be like this:
.shown1 .img2{
opacity: 1;
}
2) If you want to apply a delay to the CSS transition, you can specify it after the duration in the shorthand transition property, or in the transition-delay property. For example, for a 2s delay you can use:
.panel .img2{
opacity:0;
-moz-transition: opacity 10000ms 2s ease-in-out;
-webkit-transition: opacity 10000ms 2s ease-in-out;
transition: opacity 10000ms 2s ease-in-out;
}
See it in action here: http://jsfiddle.net/FL3RK/2/
Anyway, IMHO it would be nicer if you use the same duration (3000ms or 3s) for both transitions.
EDIT: If you don't want to wait for the animation to be completed to start it over again, put the transition property in your .shown1 .img2 rule like this:
.shown1 .img2{
opacity: 1;
-moz-transition: opacity 3000ms 2s ease-in-out;
-webkit-transition: opacity 3000ms 2s ease-in-out;
transition: opacity 3000ms 2s ease-in-out;
}
Working fiddle: http://jsfiddle.net/FL3RK/3/
var finished = 0;
var callback = function (){
// Do whatever you want.
finished++;
}
$(".div"+finished).animate(params, duration, null, callback);
html
<img src="http://lorempixum.com/200/200/people/2"/>
<img src="http://lorempixum.com/200/200/people/1"/>
<img src="http://lorempixum.com/200/200/people/2"/>
<img src="http://lorempixum.com/200/200/people/4"/>
css
img {display:none;}
script
$("img").each(function(i) {
$(this).fadeIn(2000*(i+1));
});
see the fiddle http://jsfiddle.net/vishnurajv/px7U5/

How do I change the opacity of an image?

<script type="text/javascript">
$(document).ready(function(){
$('#logo').mouseenter(function() {
$('#logo').fadeTo("fast",0.3);
});
$('#logo').mouseleave(function() {
$('#logo').fadeTo("fast",1)
});
});
</script>
I made this to change the opacity of an image while hovering over it with the cursor, but this doesn't happen. :(
You don't need jQuery for that, you can use CSS:
Example HTML - you need it to have the ID logo.
<img id="logo" src="http://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Example.svg/200px-Example.svg.png" />
CSS
#logo {
opacity: 1;
filter:alpha(opacity=100);
transition: opacity 0.2s linear 0s;
-webkit-transition: opacity 0.2s linear 0s;
}
#logo:hover {
opacity: 0.3;
filter:alpha(opacity=30);
transition: opacity 0.2s linear 0s;
-webkit-transition: opacity 0.2s linear 0s;
}
http://jsfiddle.net/pFEdL/2/
What does you HTML look like for your image? Is it embedded in other divs?
SO: Jquery mouseenter() vs mouseover()
As gilly3 states in the question above, "Each time your mouse enters or leaves a child element, mouseover is triggered, but not mouseenter".

Categories

Resources