I'm trying to hide a <div> via javascript hide() - javascript

$("#" + id).hide(2000);
I have a div I'm trying to hide thusly, but doesn't seem to be doing the animation properly.
Just disappears.

Based on your comments, my guess is that your code is deleting it right after the animation STARTED. The animation is an asychronous process. Your code will continue to run right after the animation is started. If you are then removing the object after the call to hide(), then you will be removing it before the animation has completed and it will "just disappear" rather than fade out slowly.
To fix that, you will need a completion event on the animation and you will need to remove it upon completion.
You will need something like this:
$("#" + id).hide(2000, function() {
// remove it from the page here upon completion of the animation
});
Just to show people that the .hide(2000) function works just fine, here's a working example: http://jsfiddle.net/jfriend00/XDQwU/.

Related

Why shouldn't I use delay? [duplicate]

According to jQuery document on .delay(),
The .delay() method is best for delaying between queued jQuery
effects. Because it is limited—it doesn't, for example, offer a way to
cancel the delay—.delay() is not a replacement for JavaScript's native
setTimeout function, which may be more appropriate for certain use
cases.
Could someone please expand on this? When is it more appropriate to use .delay(), and when is it better to use .setTimeout()?
I think what you posted explains itself really.
Use .delay() for jQuery effects including animations.
setTimeout() is best used for everything else. For example when you need to trigger an event at a certain elapsed time.
As far as I understand, .delay() is meant specifically for adding a delay between methods in a given jQuery queue. For example, if you wanted to fade an image into view during the span of 1 second, have it visible for 5 seconds, and then spend another second to fade it out of view again, you could do the following:
$('#image').fadeIn(1000).delay(5000).fadeOut(1000);
In this instance, .delay() is more intuitive to use since it is specifically meant for delaying events in a given jQuery queue. setImeout(), on the other hand, is a native JavaScript method that isn't intended explicitly for a queue line. If you wanted an alert box to pop up 1 second after clicking on a button, you could do the following:
function delayAlert() {
var x = setTimeout("alert('5 seconds later!')", 5000);
}
<input type="submit" value="Delay!" onclick="delayAlert();" />
You can use delay with animations, for example:
$('.message').delay(5000).fadeOut();
You can also use timeOut to delay the start of animations, for example:
window.setTimeout(function(){
$('.message').fadeOut();
}, 5000);
As you see, it's easier to use delay than setTimeout with animations.
You can delay pretty much anything with setTimeout, but you can only delay animations with delay. Methods that aren't animations are not affected by delay, so this would not wait a while before hiding the element, it would hide it immediately:
$('.message').delay(5000).hide();
.delay() is mostly used for chaining together animation effects with pauses in between.
As the docs mention, there is no way to cancel the delay. In the case where you may want to cancel the delay, a setTimeout() should be used instead so you can cancel it with clearTimeout()
Another side effect of delay(): it seems to disable the ability to hide (or fadeOut, etc) the objecting being delayed, until the delay is over.
For example, I set up the following code (perhaps a stackoverflow developer will recognize the css names....) to hide a 'div':
$j(document).ready(function(){
var $messageDiv = $j("<div>").addClass('fading_message')
.text("my alert message here").hide();
var $closeSpan = $j("<span>").addClass('notify_close').text("x");
$closeSpan.click(function() {$j(this).parent().slideUp(400);});
$messageDiv.append($closeSpan);
$j('.content_wrapper_div').prepend($messageDiv);
$messageDiv.fadeTo(500, .9).delay(5000).fadeTo(800,0);
});
Clicking the "x" that's in the span (that's in the 'div') did fire off the click function (I tested with an alert in there), but the div didn't slideUp as directed. However, If I replace the last line with this:
$messageDiv.fadeTo(500, .9);
..then it did work - when I clicked the "x", the surrounding div slideUp and and away. It seems as if the background running of the "delay()" function on the $messageDiv "locked" that object, so that a separate mechanism trying to close it couldn't do so until the delay was done.

Preventing queues with JQuery effect() with fixed position elements

The Situation
I am using the effect() function of JQuery UI. The type of effect doesn't really matter but for the purpose of this question lets use "bounce". This effect is called when a link is clicked, so my complete example code is as follows:
$('#button').click(function (e) {
e.preventDefault();
$('#box').effect('bounce');
});
Here is a demo
The Problem
The problem I have, or more the behavior I want to get rid of, is that when you click the link multiple times in quick succession then it queues up the animations. (See the demo, click the link 10 times fast, then release and watch it continue to animate)
The Requirement
I just want to prevent effects/animations from being queued. In other words, I am looking for clicks to be ignored if the box is already bouncing. Is there anyway I can do this?
The Failed Attempts
I have already done some research, and I tried a couple of method below but to no success:
$('#box').stop().effect('bounce');
stop() just doesn't seem to have any effect in this case.
$('#box').clearQueue().effect('bounce');
clearQueue actually works in the sense that the effects don't queue, however there are side-effects which causes the layout to mess up. I assume this is because it prevents the effect from returning the styles to their defaults. It may also be related to using a fixed position for the box.
This should do the trick.
$('#button').click(function (e) {
e.preventDefault();
if( !$('#box').is(':animated') ){
$('#box').effect('bounce');
}
});
Yes you are right, clearQueue makes position changes in div but i tried that and it looks like working
$('#button').click(function (e) {
e.preventDefault();
if ($("#box").is(':animated')) {
$('#box').effect = null
}
else {
$('#box').effect('bounce');
}
});
js fiddle example
Try this:
$('#button').click(function (e) {
e.preventDefault();
$('#box').stop(true).effect({
effect: 'bounce',
complete: function() {
$('#box').removeAttr('style');
}
});
});
stop(true) stops and clears the queue immediately but when stopped, effect() leaves inline styles after completion. They just need to be removed afterwards.
The benefit of this approach is that the clicks feel a little more responsive (for the lack of a better word). The box will seem to react quickly to user's clicks.
However, if you cannot afford to simply remove the style attribute upon completion of animation, say you have previously applied inline styles on your object, you can cache them beforehand and re-apply them upon completion, which is demonstrated in this fiddle.
Hope this helps.

bootstrap 3 collapse show and hide overlapping eachother

I have a website that is using bootstrap 3 and is doing an collapse panel when images are clicked. I have code in place that watches for the 'show' event that will 'hide' all panels. The issue im having is that the transition animation is not waiting for eachother to be completed resulting in some weird animation happening. example page is here ( http://urbantimber.ca/newsite/product-flooring.html).
my function looks like:
$(function () {
var active = true;
$('#accordion').on('show.bs.collapse', function () {
if (active) $('#accordion .in').collapse('hide');
});
});
I have also tried to negate the transition by setting the speed to 0.001s but the blip still shows.
looking for any ideas on how to watch that the animation is completed before the show event is finished. im thinking it needs to be a click event that happens before the show even starts but im not sure how to go about this. any ideas or comments are greatly appreciated.
I'm wondering if you're not better off just creating your own implementation. You're not really using any of the 'accordion' like functionality. Perhaps it would be easier to just have a click event handler attached to each of your images that checks if the details panel is displayed, if not, you would just display it using jquery show method. If it is displayed already, you could just replace the panel html with the item's details. (btw, nice looking site).

JQuery Slider With Multiple Animations/Effects

I'm looking to create a slider where one image fades in, then another fades in next to it, then the slide changes. Thus far I've tried using "wowslider" and "rhinoslider" and googled it extensively... however I cannot seem to find a slider tool where this can be accomplished. Every tool I've tried and read about allows for a fade in effect - but only for one image per slide - I need to fade in two images [one, then the other] then change slides.
How might this be accomplished? (either with an existing tool such as wowslider - or perhaps using pure code)
Any suggestions are GREATLY appreciated!
you could use http://getbootstrap.com/javascript/#carousel Bootstrap Carousel Plugin.
it has an event handler for between slide times:
$('#myCarousel').on('slide.bs.carousel', function () {
// do something…
// This event fires immediately when the slide instance method is invoked.
})
or
$('#myCarousel').on('slid.bs.carousel', function () {
// do something…
// This event is fired when the carousel has completed its slide transition.
})
you can use the second one and instead of //do something you can write your fade function. I mean by this: you put two images under each slide. after slide is complete, faceOut the first shown image and fadeIn the second hidden image.

Utilise the Bootstrap Carousel "slide" event and the .next class

So I've got a little problem (similar to this one I posted the other day: http://bit.ly/11JpbdY) with using SlabText on a piece of content that is hidden on load. This time, I'm trying to get slabText to update the display of some content that is in a slider (using Twitter Bootstrap's Carousel plugin).
Following Twitter's documentation (http://twitter.github.com/bootstrap/javascript.html#carousel) for Bootstrap's Carousel plugin, I'm trying to use slide event so that I re-call SlabText to make it display correctly.
Using developer tools I can see that Carousel adds a .next class as it processes the slide of one .item element to the next. This is then removed before the .active class is transferred.
I can access the "slide" event without any issue, but trying to get hold of the .next element is proving troublesome. Here is a JSFiddle of my code thus far: http://jsfiddle.net/peHDQ/
To put my question simply; how should I correctly use the slide event to trigger a function?
Please let me know if any additional information would be useful.
Further notes:
As I've been unable to get hold of the .next class, I'm attempting to do this with some jQuery. Here is my code thus far:
$('.carousel').carousel({
interval: 5000
}).on('slide', function (e) {
$(this).find('.active').next().find('.slab').slabText();
});
From what I understand this should be grabbing each .slab element and triggering the SlabText plugin.... alas I get an error:
"Uncaught TypeError: Object [object Object] has no method 'slabtext' "
Can anyone advise what I am doing wrong here...? I've used the exact same process to add a class and it works fine (as per this JSFiddle: http://jsfiddle.net/peHDQ/2/)
I identified the problem. The issue is that the event 'slide' is called before the next slide is made visible. I added a little delay, and it works fine now. Try this:
$('.carousel').carousel({
interval: 5000
}).on('slide', function (e) {
var xx = $(this);
setTimeout(function() {
xx.find('.active').next().find('.slab').slabText();
} , 0);
});
Per the documentation you should be using Slid not Slide
https://getbootstrap.com/docs/4.3/components/carousel/#events
Event Type: slid.bs.carousel
This event is fired when the carousel has completed its slide transition.

Categories

Resources