jQuery stop listening event while animate - javascript

I'm writing some code with jQuery.
There are some div with class 'icon' in my html, and I hide a div with class 'hide' beside each 'icon'.
I want that when user's mouse get in to 'icon', the corresponding 'hide' will make animate.
But I found that if I quickly move the mouse in and out the 'icon'. The animate will excute twice. And it make my web's visual effects very terrible.
So I want stop the event while animate excuting, I read a lot of artical and document then try, but still can't solve it.
This is my code.
$(".icon").on("mouseenter", function(){
$(this).closest(".slideli").find(".hide").show("slide", {direction: 'right'}).animate({duration: 500, easing: "swing", queue: false});
});
$(".icon").on("mouseleave", function(){
$(this).closest(".slideli").find(".hide").hide("slide", {direction: 'right'}).animate({duration: 500, easing: "swing", queue: false});
});

To avoid event to be fired on animated element(s), you should use pseudo selector :animated as following: :not(:animated)
$(".icon").on("mouseenter", function(){
$(this).closest(".slideli").find(".hide").filter(":not(:animated)").show("slide", {direction: 'right'}).animate({duration: 500, easing: "swing", queue: false});
});
$(".icon").on("mouseleave", function(){
$(this).closest(".slideli").find(".hide").filter(":not(:animated)").hide("slide", {direction: 'right'}).animate({duration: 500, easing: "swing", queue: false});
});

You should use jQuery's .stop() to prevent new animations from being added to the queue all the time. You can either use .stop(true,true) or .stop(true,false) depending on your need:
if you only want to clear the animation queue and let it animate to completion by itself, use .stop(true,false)
if you want to clear the animation queue and force the animation to jump to its end state immediately, use .stop(true,true)
Here's your improved code:
$(".icon").on("mouseenter", function(){
$(this)
.closest(".slideli")
.find(".hide")
.stop(true,true)
.show("slide", {direction: 'right'})
.animate({duration: 500, easing: "swing", queue: false});
});
$(".icon").on("mouseleave", function(){
$(this)
.closest(".slideli")
.find(".hide")
.stop(true,true)
.hide("slide", {direction: 'right'})
.animate({duration: 500, easing: "swing", queue: false});
});
You may also use .finish(), but since your animation is rather simple (you are not chaining consecutive animations in any instances), .stop() can do the same job, too.
p/s: A shameless plug to a quick article on handling jQuery animations I published some time ago: How to .stop() writing bad jQuery animations

Related

How to use embedded functions(One Scroll Page by Pete - peachananr)

recently I am working on a page using this awesome plugin:
https://github.com/peachananr/onepage-scroll
However I am not familiar with JavaScript so I will be obliged if someone could explain to me few things:
I'd like to combine one-scroll-page behaviour with anchor scrolling, simple example:
function scrollContent(id) {
$('html, body').animate({ scrollTop: $('#' + id).offset().top }, 1000 );
}
as far as I know there is an embedded function called "$.fn.moveTo(page_index)"(see plugin link) but I wasn't able to use it successfully.
I'd like to turn a slider when leaving page #1 to save resources, slider launches with function:
$('#da-slider').cslider({
autoplay : true,
interval : 8000,
});
tried to simply turn off autoplay by messing with callbacks but it turned out to be bad solution:
$(".main").onepage_scroll({
sectionContainer: "section", // sectionContainer accepts any kind of selector in case you don't want to use section
easing: "ease", // Easing options accepts the CSS3 easing animation such "ease", "linear", "ease-in",
// "ease-out", "ease-in-out", or even cubic bezier value such as "cubic-bezier(0.175, 0.885, 0.420, 1.310)"
animationTime: 1000, // AnimationTime let you define how long each section takes to animate
pagination: true, // You can either show or hide the pagination. Toggle true for show, false for hide.
updateURL: true, // Toggle this true if you want the URL to be updated automatically when the user scroll to each page.
beforeMove: function(index) {}, // This option accepts a callback function. The function will be called before the page moves.
afterMove: function(#1) {
$('#da-slider').cslider({
autoplay : off,
interval : 8000,
});
}, // This option accepts a callback function. The function will be called after the page moves.
loop: true, // You can have the page loop back to the top/bottom when the user navigates at up/down on the first/last page.
keyboard: true, // You can activate the keyboard controls
responsiveFallback: 600, // You can fallback to normal page scroll by defining the width of the browser in which
// you want the responsive fallback to be triggered. For example, set this to 600 and whenever
// the browser's width is less than 600, the fallback will kick in.
direction: "horizontal" // You can now define the direction of the One Page Scroll animation. Options available are "vertical" and "horizontal". The default value is "vertical".
});
Every tip, even link to jquery tutorial will be very helpfull. Thanks in advance!
Solution found:
$("#your-div-id").on("click", function(){$(".main").moveTo(2);});

Can you toggle a pulsing animation?

I would like to have an image of an arrow pulsating, when the user clicks the arrow a div slides down and the pulsating arrow animation stops. When the user clicks the arrow again the div slides up and the arrow continues to pulsate.
I can toggle. I can pulse. However, I am unsure how to go about toggling a pulsating animation. If someone could point me in the right direction that would be great!
Many thanks.
$(document).ready(function(){
{
$(".arrow_down_grey").effect( "pulsate",
{times:5}, 3000 );
}
$('.arrow_down_grey').click(function(){
$(".arrow_down_grey").stop().effect();
$(".hiddenDiv").slideToggle();
});
});
jQuery has a .stop() method that halts animations. You would need to listen for clicks, and then start/stop the animation accordingly using .stop(). I'm thinking you would need to use the true flag to clear your animation queue, so the stop doesn't just "pause" the animation, but that's up to you. You would then use a closure to keep track of the "toggle status" and start the animation back up when your div is toggled the other way.
$('#animation').stop(false);
jQuery .stop documentation: http://api.jquery.com/stop/
$(document).ready(function() {
function pulsate() {
$(".pulsate").animate({ opacity: 0.2 }, 1200, 'linear')
.animate({ opacity: 1 }, 1200, 'linear', pulsate)
.click(function() {
$(this).animate({ opacity: 1 }, 1200, 'linear');
$(this).stop();
});
}
pulsate();
});
This code works but how would you start the pulse animation again?

How to stop (in order to reverse) a fabricjs animation

I want to create a mouse rollover effect, like we used to see in flash websites - when the mouse rolls over an element it begins to animate, but if in the middle of the anim the mouse rolls out the animation would stop and run back.
I would like to achieve the same effect with fabric, but I can seem to find a way to stop the animation. For example:
rect.animate('top', '200', {
duration: 1000,
onChange: canvas.renderAll.bind(canvas),
onComplete: function() {
//callback code goes here
}
});
This will animate until the top value of the rect will become 200. Is there a way to stop animation before that?
You need to specify abort function.
rect.animate('top', '200', {
duration: 1000,
onChange: canvas.renderAll.bind(canvas),
onComplete: function() {
//callback code goes here
},
abort: function(){
return someConditionWhichAbortsAnimationWhenItsTrue;
}
});
The only problem is that abort was not passed in to the underlying fabric.util.animate, which I just fixed, so you'll need to use latest version :)

jQuery Cycle plugin: scroll slides horizontally at constant speed, without easing

I'm using jQuery Cycle plugin. When I use 'scrollHorz' as the value of fx property, slides move not at constant speed, but with easing (see demo).
How can I make the slides move at constant speed?
Just use
easing: "linear"
and you should be fine!
For example:
$('#s1').cycle({
fx: 'scrollHorz',
easing: 'linear',
prev: '#prev1',
next: '#next1',
timeout: 0
});

Multiple hover instances jQuery

I have a div to animate from the top to the bottom of another div. I'm currently playing w/ mouseenter/leave and JS animations w/ easing where its original state is up/top. I want to hover/mouseenter and have it move down and stay down if I mouseleave/hover off. When I hover again it will animate back to the top/start.
I initially used mouseenter/leave which obviously doesn't do what I need as I would like the state to remain the same upon mouseleave. So what function would be best for this need? I'm still learning the terminology and am stumbling over how to better phrase the question.
Code:
function init() {
mouseenter: function(){
$(".ltrP").stop(true, true).animate({
marginTop:"170px"
},
{
duration: 1000,
easing: "easeOutBounce"
});
},
mouseleave: function(){
$(".ltrP").stop(true, true).animate({
marginTop: "0px"
},
{
duration: 1000,
easing: "easeInBounce"
});
}
});
}
window.onload = init;
I've edited your piece of code, see the comments for explanation:
$(document).ready(function(){ // Runs when document is loaded
$(".ltrP").mouseenter(function(){ // Mouseenter event on the given object (.ltrP)
var goTo = $(this).css("marginTop") == '0px' ? 170 : 0; // When the current margin-top is 0px animate to 170px, otherwise animate it back to 0px
$(this).stop(true,false).animate({ // Changed the stop(true, true) to stop(true, false), looks nicer in my opinion
marginTop: goTo // Animate to the just set variable
}, 1000);
});
});
And see here a demo: http://jsfiddle.net/hnDmt/
(And the easing "easeInBounce" was not working for me, so I removed it. (Maybe a jQuery UI easing?))
You can rewrite your code this way:
$(document).ready(function(){
init();
});
function init() {
$.hover(function(){
$(".ltrP").stop(true, true).animate({
marginTop:"170px"
},
{
duration: 1000,
easing: "easeOutBounce"
});
},
function(){
$(".ltrP").stop(true, true).animate({
marginTop: "0px"
},
{
duration: 1000,
easing: "easeInBounce"
});
});
}
There are lots of ways to do this. Maybe the easiest to to conceptualize is by adding a class to the animated item. You want to write two separate mouseenter functions.
For the first function, trigger your down animation, and add a class to the entered item. Call the class "moveddown" or something obvious.
Then, write a second mouseenter function. When an item with the class is mousentered, animate it up, and remove the class.
Forget about jQuery hover for this. It's just a wrapper for mouseenter/mouseleave. It can cause problems. The jQuery docs warn about it. It's usually better to write mouseenter and mouseleave functions separately, especially when you're trying to do something tricky, like this.

Categories

Resources