Fly-To-Basket Effect With jQuery
In this 3rd day of the “WRD E-Commerce Week“, we will be adding a chic fly-to-basket effect to our previously created Ajaxed shopping cart using jQuery.
Rather than the complete HTML structure & PHP code that adds/removes the products, we'll be focusing on the details related with the effect.
To findout the details of the complete HTML structure & PHP code, please check our post: Creating A Slick Ajaxed Add-To-Basket With jQuery And PHP. And, a full working example can be found in the download package.
The HTML
HTML For The Product's Wrapper:
1.
<
div
class
=
"productWrap"
>
2.
<
div
class
=
"productImageWrap"
><
img
alt
=
"Product1"
src
=
"images/product1.jpg"
/></
div
>
3.
<
div
class
=
"productNameWrap"
>Krups Coffee Maker</
div
>
4.
<
div
class
=
"productPriceWrap"
>
5.
<
div
class
=
"productPriceWrapLeft"
>$95</
div
>
6.
<
div
class
=
"productPriceWrapRight"
><
a
href
=
"inc/functions.php?action=addToBasket&productID=1"
onclick
=
"return false;"
> <
img
alt
=
"Add To Basket"
height
=
"32"
id
=
"featuredProduct_1"
src
=
"images/add-to-basket.gif"
width
=
"111"
/> </
a
></
div
>
7.
</
div
>
8.
</
div
>
The part we'll focus is the contents inside <div class="productPriceWrapRight">
which wraps the “add-to-basket” button.
The Highlights:
- a link with
onClick="return false;
value which means it won't be active if JavaScript is active (to make the script unobtrusive) - add-to-basket image has an unique ID:
id="featuredProduct_1"
which we'll use to understand the button of which product is clicked
HTML For The Basket:
1.
<div id=
"basketWrap"
>
2.
<div id=
"basketTitleWrap"
>Your Basket</div>
3.
<div id=
"basketItemsWrap"
>
4.
<ul>
5.
<li> </li>
6.
<!--?php getBasket(); ?--></ul>
7.
</div>
8.
</div>
The Highlights:
- we have an empty
<span>
withid="notificationsLoader"
to show a loading image - we keep an empty div to be able to insert any data before/after them
- we call a PHP function:
<?php getBasket(); ?>
to get the basket data when the page is first loaded.
The JavaScript (jQuery)
01.
$(
"#basketItemsWrap li:first"
).hide();
02.
03.
$(
".productPriceWrapRight a img"
).click(
function
() {
04.
var
productIDValSplitter = (
this
.id).split(
"_"
);
05.
var
productIDVal = productIDValSplitter[1];
06.
07.
var
productX = $(
"#productImageWrapID_"
+ productIDVal).offset().left;
08.
var
productY = $(
"#productImageWrapID_"
+ productIDVal).offset().top;
09.
10.
if
( $(
"#productID_"
+ productIDVal).length > 0){
11.
var
basketX = $(
"#productID_"
+ productIDVal).offset().left;
12.
var
basketY = $(
"#productID_"
+ productIDVal).offset().top;
13.
}
else
{
14.
var
basketX = $(
"#basketTitleWrap"
).offset().left;
15.
var
basketY = $(
"#basketTitleWrap"
).offset().top;
16.
}
17.
18.
var
gotoX = basketX - productX;
19.
var
gotoY = basketY - productY;
20.
21.
var
newImageWidth = $(
"#productImageWrapID_"
+ productIDVal).width() / 3;
22.
var
newImageHeight = $(
"#productImageWrapID_"
+ productIDVal).height() / 3;
23.
24.
$(
"#productImageWrapID_"
+ productIDVal +
" img"
)
25.
.clone()
26.
.prependTo(
"#productImageWrapID_"
+ productIDVal)
27.
.css({
'position'
:
'absolute'
})
28.
.animate({opacity: 0.4}, 100 )
29.
.animate({opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200,
function
() {
30.
$(
this
).remove();
31.
32.
$(
"#notificationsLoader"
).html(
'<img src="images/loader.gif">'
);
33.
34.
$.ajax({
35.
type:
"POST"
,
36.
url:
"inc/functions.php"
,
37.
data: { productID: productIDVal, action:
"addToBasket"
},
38.
success:
function
(theResponse) {
39.
40.
if
( $(
"#productID_"
+ productIDVal).length > 0){
41.
$(
"#productID_"
+ productIDVal).animate({ opacity: 0 }, 500);
42.
$(
"#productID_"
+ productIDVal).before(theResponse).remove();
43.
$(
"#productID_"
+ productIDVal).animate({ opacity: 0 }, 500);
44.
$(
"#productID_"
+ productIDVal).animate({ opacity: 1 }, 500);
45.
$(
"#notificationsLoader"
).empty();
46.
47.
}
else
{
48.
$(
"#basketItemsWrap li:first"
).before(theResponse);
49.
$(
"#basketItemsWrap li:first"
).hide();
50.
$(
"#basketItemsWrap li:first"
).show(
"slow"
);
51.
$(
"#notificationsLoader"
).empty();
52.
}
53.
54.
}
55.
});
56.
57.
});
58.
59.
});
Highlights:
- splitting the ID of the clicked “add-to-basket” image which is “featuredProduct_1″ from the “_” character & get the databaseID of the product
- using jQuery's offset, we get the current x-y position of the product
- run a “if-then-else” clause which:
- if that product is already in the basket:
if( $("#productID_" + productIDVal).length > 0){
(if an element with that ID exists in the basket), if exists, then we get the x-y positions of that element using “offset” again, so we can target that element while flying our product - if that product isn't in the basket: then we get the x-y positions of a static element in the basket HTMl area (#basketTitleWrap in our case)
- if that product is already in the basket:
- very important: we create 2 variables; gotoX & gotoY by subtracting the element in the basket's position from the product's position. And we get the distance product needs to animate (fly) in x's & y's
- again create 2 variables named newImageWidth & newImageHeight which are x times (3 in our case) smaller the size of our product's image. This is used to create a resizing effect while flying the product to the basket
- and, let's fly the product: targeting the product image to be flied;
$("#productImageWrapID_" + productIDVal + " img")
. We clone it with jQuery's clone, place inside the same element where the original image exists with prependTo & set it's position toabsolute
. The next thing, we decrease its opacity so it starts in a little transparent look before the animation. And, we fly it by mentioning themarginLeft
&marginTop
values we calculated. At the same time, we make it more transparent & resize it. Also, we set the duration of the animation (1200 in our case). - As a callback function (after the animation ends), we remove the cloned image from the DOM, show the Ajax loader & post a query to our PHP page. That's all.
P.S. To make the example work on your side, you should be creating a new database with the jBasket.sql file inside the download package & configure the database connection information inside “inc/db.php” file.
- Tags:
Javascript Php
- Filed under: E-Commerce, Extras, MIT License, Tutorials
- 46 Comments
46 Responses for "Fly-To-Basket Effect With jQuery"
-
Francesco July 29th, 2009 at 3:24 am
This is beautiful and creative.
-
Nils Riedemann July 29th, 2009 at 4:09 am
Nice… but buggy… at least under Safari.
Double Click => 3 Items
Tripple Click => 7 Items -
Umut M. July 29th, 2009 at 4:17 am
@Nils,
Nice catch. An easy fix can be disabling the button until the function is completed.But, thinking why this may happen and couldn’t findout of something solid, any ideas?
-
Jonathan Foucher Web Design July 29th, 2009 at 9:04 am
Same with Firefox. You need to wait for the function to complete before accepting another click. I’ve already seen the same type of problems occur with jQuery, although in different contexts. I think it’s probably a bug in jQuery rather than in the code presented here.
A workaround is to put an apropriate timer in place at the first click, and then only do the second click actions if the timer has expired.
Anyway, good snippet! -
maria July 31st, 2009 at 3:57 pm
anyone tested with magento?
-
10 New jQuery Plugins To Spice Up Your Website - Webexpedition18 August 9th, 2009 at 4:48 am
[...] Go to Plugin Homepage [...]
-
John August 13th, 2009 at 4:38 am
Doesn’t seam to support keyboard access, which is kind of silly as you would be ruling out a proportion of your users.
-
Bry August 16th, 2009 at 6:05 am
Hi there, great cart WRD thanks for posting this and all the other great stuff on your site…can i ask a favour? can you please add a checkout facility to the cart…with a form for details or at least let me know how I could add that to the cart. I would really appreciate it. Thanks B
-
Noteefy - Be aware » Best Jquery Lessons Of The Month August 19th, 2009 at 4:40 am
[...] Fly-To-Basket Effect With jQuery [...]
-
I migliori plugin di jQuery in circolazione sono qui | Italian webdesign August 24th, 2009 at 1:38 am
[...] Fly-To-Basket Effect With jQuery [...]
-
Rob August 30th, 2009 at 2:47 pm
Nice tutorial. Although these look good I personally don’t it’s wise to depend on users having JavaScript active on such an important part of your site, as if a user doesn’t have js turned on you’re ruling out some users – just my personal view.
This also crashes my Firefox if I click multiple times E.g. to test added 32 times Think you definately need a function to either disbale the button or start a timer.
-
Lio September 26th, 2009 at 11:09 pm
Hello bro..
im confuse to download this, would you explain to me ow to download it?
thanks before hehehe lol
-
Umut M. September 27th, 2009 at 2:30 am
@Lio,
After subscribing to the RSS feed, while reading th elatest WRD post, at the bosttom of the post, you’ll a title named “Special Downloads”, you can find the download link there.
-
xaviergnu September 30th, 2009 at 3:22 am
hi all!
tnx for a great script, however I have an issue with posting vars from products page.
I’m having a variables from form (checkboxes, radio buttons, dropdown menus…) that calculates price of a product and then I have to POST it to DIV on a right hand box basket.
Is it possible to change present link (a href=”inc/functions.php?action=addToBasket&productID=1″ rel=”nofollow”) to ‘submit’ button of a form with all values from it?
tnx in advance,
X.
-
Umut M. September 30th, 2009 at 5:00 am
@xaviergnu,
With the click of the submit button, with a function like:
$(“.button”).click(function() {you can pass all your form parameters with jQuery “ajax” like:
$.ajax({
type: “GET”, …..Suggest digging: http://docs.jquery.com/Ajax/jQuery.ajax
Cheers.
-
cajrix October 2nd, 2009 at 3:23 am
Nice… but buggy… at least under Safari.
Double Click => 3 Items
Tripple Click => 7 Items— did anyone solve the problem?
it looks ok in chrome, but in firefox, ie it add 2 items, can u help me? -
yalamber subba October 8th, 2009 at 10:54 pm
is there any way we can limit the quantity of items to add in the basket? I need this feature in this implementation. It would be nice feature if any one could make it. thanks
-
indialike December 16th, 2009 at 5:52 am
Very nice and useful tutorials to web designers,
Thanks for posting. -
hadiya May 1st, 2010 at 1:57 pm
thank u it;s amazing >>>>.
how can i add Quantity to the basket -
Giu May 29th, 2010 at 4:10 am
This is nice, but how can I limit to just one item to the basket. In the case that the items are unique, is there a way to disable button “add to basket” after one click and the resume the button if it will be removed from the basket?
Thaks for helping me. -
Mageshkumar June 2nd, 2010 at 5:26 am
This is nice, but how can i create the Order Id,that means if one person selecting more that products means ,for that i have to create order id,indeed i tried up my level, i didn’t get any can help me……
Sample:
Person-1
Id Product Name No.of Items Price OrderId
1 Krups Coffee Maker(2 items) – $190 5001
2 Keurig Cup(7 items) – $266 5001Person-1
Id Product Name No.of Items Price OrderId
1 Coffee Pro (6 items) – $774 5002
2 Keurig Cup (7 items) – $266 5002
3 Coffee Maker (29 items) – $2755 5002 -
Robin Henriksson June 6th, 2010 at 4:03 pm
Hello!
When i have implemeted the code on my site, and uses the link to add a product to my basket, it makes to entries in the table, do you know why?
Thanks in advance
Robin -
Dante June 18th, 2010 at 10:05 am
A solution to the fact that it crashes when clicked multiple times can be seen here : http://stackoverflow.com/questions/3064886/disable-link-while-animated-basket
thanks to EndangeredMassa
-
vishal September 4th, 2010 at 2:39 am
Hello,
if you add” total:” at the end of basket, it will looks more professional, please do it.thanks,
vishal -
oscar December 6th, 2010 at 12:48 pm
thanks…! (:
-
ashok April 5th, 2011 at 7:23 am
i want to add total in basket ….how to do…
-
pahaya May 5th, 2011 at 11:02 pm
cool
-
Charlene May 8th, 2011 at 9:18 pm
helped me a lot ! thx !!
-
Ciro August 27th, 2011 at 11:48 am
Excelente!!!!GOOD JOB, tranks for share it
-
Deby November 21st, 2011 at 2:30 am
Sorry for someone can show me how to get solution if I get the something wrong an error, insert query failed ?
-
Shanmuga Sundaram March 19th, 2012 at 11:16 am
Hi Frds
I want Fly to Basket Effect ( without php ) with asp.net
-
Utham April 20th, 2012 at 3:36 am
Very great post… finally I got what I am looking for… I implemented this effect in my application.. But the code is not exact mentioned above… I just copied the LOGIC not the CODE.
Working in IE, Chrome, Firefox… -
Narendra May 2nd, 2012 at 10:47 am
how to store data into the database using jQuery?
-
Umut M. May 3rd, 2012 at 5:34 am
@Narendra,
It is a good idea to send an Ajax request to a PHP file with jQuery and saving the data to db using PHP (or any other scripting language).
-
Luis May 8th, 2012 at 12:11 pm
When de page reload de items into the basket disappear. Solution or suggestions?
-
shihabudheen May 30th, 2012 at 2:10 am
Excellent…i like very much..thank you a lot
-
lopar September 10th, 2012 at 11:34 am
Hello, It looks great. Is it for jquerry 1.3.2 ? My question is – what code should I remove so I can use only the js script? I need only the fly effect, no need to connect to database at all.
Thank you
This comment has been removed by the author.
ReplyDelete