Reputation: 4914
I'm building a site (with jQuery 1.4) which has a floating (fixed position) basket.
Current working functionality (Demo here):
What I'm struggling with
javascript so far
//Toggle basket directly
$("#basket-button").click(function(){
$("#basket-contents").slideToggle();
$("#floating-basket").toggleClass("locked on");
return false
});
//Toggle basket with a 'buy now' button
$(".buy-button").click(function(){
//open basket
$("#basket-contents").slideDown();
$("#floating-basket").addClass("on");
//set track name
$("#basket-trackname").text($(this).attr('name'));
//Display basket message
$("#basket-message").fadeIn();
return false
});
html
<a class="buy-button" name="Item Name one" href="#">Buy</a>
<a class="buy-button" name="Item Name two" href="#">Buy</a>
<div id="floating-basket">
<div id="basket-message">
<strong id="basket-trackname"></strong> added to basket!
</div>
<a href="/basket" id="basket-button">My Basket <span id="basket-number">0</span></a>
<div id="basket-contents">
lorem
<a href="#">Checkout</a>
</div>
</div>
http://www.webresourcesdepot.com/ajaxed-sliding-shopping-cart-with-jquery is the closest thing I found - but even this is problematic (if you add an item and quickly close the basket for example).
Would really appreciate some input :)
Upvotes: 3
Views: 2402
Reputation: 11859
learn about setTimeout
and clearTimeout
var t = setTimeout(function(){ whattodo(); }, 3000);
will do anonym function, which basically consists only of whattodo()
function (which could hide basket, etc.)
then in each click, .stop()
animation and call:
clearTimeout(t); // clears previous timeout
t = setTimeout(function(){ whattodo(); }, 3000); //set new timeout
so, setTimeout
/clearTimeout
are to time hiding in 3 seconds, while .stop()
stops ongoing animation - learn more @ http://api.jquery.com/stop/ - basic usage:
$(myelementwithanimation).stop()
and more about timeouts: http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/
Upvotes: 3
Reputation: 16915
This is an idea of what You need:
free=true;
function doSomeAnimation(){
//do what is done either way
if(free){
free=false;
$(something).addClass('foo').slideDown(function(){free=true;});
}
}
instead of cancelling the animations - just don't trigger them if they're already running
PS. Timeouts are ok, but You have to keep all of them handlers to be able to clear Timeout, so it gets ugly... ;)
Upvotes: 0