jQuery模擬實(shí)現(xiàn)天貓購物車動(dòng)畫效果實(shí)例代碼
一、功能描述:
1、點(diǎn)擊購買按鈕,模擬拋物線將物品彈到購物車?yán)铮?/p>
2、購物車添加物品后,顯示+1動(dòng)畫;
效果圖如下:

實(shí)現(xiàn)如下:
1、導(dǎo)入jquery相關(guān)的包:
<script type="text/javascript" src="jquery-2.1.1.min.js"></script> <script src="jquery.fly.min.js"></script>
2、html文件內(nèi)容:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>jQuery實(shí)現(xiàn)模擬天貓加入購物車飛入動(dòng)畫效果</title>
<meta name="keywords" content="jquery,購物車" />
<body>
<div id="main">
<div class="demo">
<div class="box orange button addcar">
<div style="padding-top:55px">
土豪,請(qǐng)猛擊我,加入購物車
</div>
</div>
</div>
</div>
<div class="m-sidebar">
<div class="cart">
<i id="end"></i>
<span>購物車</span>
</div>
</div>
</div>
</body>
3、css樣式:
<style type="text/css">
.demo{width:820px; margin:60px auto 10px auto}
.m-sidebar{position: fixed;top: 0;right: 0;background: #000;z-index: 2000;width: 85px;height: 100%;font-size: 12px;color: #fff;}
.cart{color: #fff;text-align:center;line-height: 20px;padding: 200px 0 0 0px;}
.cart span{display:block;width:20px;margin:10 0 0 0;}
.cart i{width:50px;height:35px;display:block; background:url(car.png) no-repeat;}
#msg{position:fixed; top:300px; right:35px; z-index:10000; width:1px; height:52px; line-height:52px; font-size:20px; text-align:center; color:#fff; background:#360; display:none}
.box{width:300px; height:150px; border:1px solid #e0e0e0; text-align:center}
.u-flyer{display: block;width: 50px;height: 50px;border-radius: 50px;position: fixed;z-index: 9999;}
.button {
display: inline-block;
outline: none;
cursor: pointer;
text-align: center;
text-decoration: none;
font: 16px/100% 'Microsoft yahei',Arial, Helvetica, sans-serif;
padding: .5em 2em .55em;
text-shadow: 0 1px 1px rgba(0,0,0,.3);
-webkit-border-radius: .5em;
-moz-border-radius: .5em;
border-radius: .5em;
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.2);
-moz-box-shadow: 0 1px 2px rgba(0,0,0,.2);
box-shadow: 0 1px 2px rgba(0,0,0,.2);
}
.button:hover {
text-decoration: none;
}
.button:active {
position: relative;
top: 1px;
}
/* orange */
.orange {
color: #fef4e9;
border: solid 1px #da7c0c;
background: #f78d1d;
background: -webkit-gradient(linear, left top, left bottom, from(#faa51a), to(#f47a20));
background: -moz-linear-gradient(top, #faa51a, #f47a20);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#faa51a', endColorstr='#f47a20');
}
.orange:hover {
background: #f47c20;
background: -webkit-gradient(linear, left top, left bottom, from(#f88e11), to(#f06015));
background: -moz-linear-gradient(top, #f88e11, #f06015);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#f88e11', endColorstr='#f06015');
}
.orange:active {
color: #fcd3a5;
background: -webkit-gradient(linear, left top, left bottom, from(#f47a20), to(#faa51a));
background: -moz-linear-gradient(top, #f47a20, #faa51a);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#f47a20', endColorstr='#faa51a');
}
4、核心的JQuery代碼:
<script>
//實(shí)現(xiàn)購物車+1動(dòng)畫效果
(function ($) {
$.extend({
tipsBox: function (options) {
options = $.extend({
obj: null, //jq對(duì)象,要在那個(gè)html標(biāo)簽上顯示
str: "+1", //字符串,要顯示的內(nèi)容
startSize: "18px", //動(dòng)畫開始的文字大小
endSize: "38px", //動(dòng)畫結(jié)束的文字大小
interval: 600, //動(dòng)畫時(shí)間間隔
color: "red", //文字顏色
callback: function () { } //回調(diào)函數(shù)
}, options);
$("body").append("<span class='num'>" + options.str + "</span>");
var box = $(".num");
var left = options.obj.offset().left + options.obj.width() / 2;
var top = options.obj.offset().top - options.obj.height();
box.css({
"position": "absolute",
"left": left + "px",
"top": top + "px",
"z-index": 9999,
"font-size": options.startSize,
"line-height": options.endSize,
"color": options.color
});
box.animate({
"font-size": options.endSize,
"opacity": "0",
"top": top - parseInt(options.endSize) + "px"
}, options.interval, function () {
box.remove();
options.callback();
});
}
});
})(jQuery);
function niceIn(prop){
prop.find('i').addClass('niceIn');
setTimeout(function(){
prop.find('i').removeClass('niceIn');
},1000);
}
//實(shí)現(xiàn)拋物線動(dòng)畫功能
$(function() {
var offset = $("#end").offset();
$(".addcar").click(function(event){
var addcar = $(this);
var img = "images/lg.jpg";
var flyer = $('<img class="u-flyer" src="'+img+'">');
flyer.fly({
start: {
left: event.pageX,
top: event.pageY
},
end: {
left: offset.left+10,
top: offset.top+10,
width: 0,
height: 0
},
onEnd: function(){
$.tipsBox({
obj: $("#end"),
str: "<b style='font-family:Microsoft YaHei;'>+1</b>",
callback: function () {
}
});
niceIn($("#end"));
}
});
});
});
</script>
以上所述是小編給大家介紹的jQuery模擬實(shí)現(xiàn)天貓購物車動(dòng)畫效果實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
jquery 簡單圖片導(dǎo)航插件jquery.imgNav.js
前幾天某MM要偶幫忙實(shí)現(xiàn)栗子汀首頁的圖片導(dǎo)航效果,很簡單而且具有通用性的一個(gè)需求,點(diǎn)圖片A切換圖片A相關(guān)的內(nèi)容,點(diǎn)圖片B切換圖片B相關(guān)的內(nèi)容,僅此而已。2010-03-03
jQuery模仿阿里云購買服務(wù)器選擇購買時(shí)間長度的代碼
jQuery仿阿里云購買服務(wù)器選擇時(shí)間長度,操作簡單,只需點(diǎn)擊所要選的時(shí)間段,對(duì)實(shí)現(xiàn)代碼感興趣的朋友參考下2016-04-04
jQuery實(shí)現(xiàn)移動(dòng)端下拉展現(xiàn)新的內(nèi)容回彈動(dòng)畫
這篇文章主要介紹了jQuery實(shí)現(xiàn)移動(dòng)端下拉展現(xiàn)新的內(nèi)容回彈動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
完美兼容各大瀏覽器的jQuery插件實(shí)現(xiàn)圖片切換特效
網(wǎng)友zoeDylan寫的原創(chuàng)jquery插件,實(shí)現(xiàn)了圖片輪播功能,jquery插件名稱為zoeDylan.ImgChange,圖片路徑、跳轉(zhuǎn)鏈接、提示標(biāo)題都是有動(dòng)態(tài)數(shù)組來配置,jquery插件可靈活配置6個(gè)參數(shù), height高、width寬、mgs圖片地址、links點(diǎn)擊地址、tips圖片說明、timers自動(dòng)切換時(shí)間,2014-12-12
jQuery實(shí)現(xiàn)百葉窗焦點(diǎn)圖動(dòng)畫效果代碼分享(附源碼下載)
這篇文章主要介紹了jQuery實(shí)現(xiàn)百葉窗焦點(diǎn)圖動(dòng)畫效果代碼分享(附源碼下載)的相關(guān)資料,需要的朋友可以參考下2016-03-03
jQuery中$.each()函數(shù)的用法引申實(shí)例
jQuery中的$.each()函數(shù)比forEach更加強(qiáng)大,可以用來遍歷JavaScript中的任意集合,借來下來我們會(huì)舉幾個(gè)jQuery中$.each()函數(shù)的用法引申實(shí)例,首先先來回顧一下遍歷用法的基礎(chǔ):2016-05-05
jquery拖動(dòng)層效果插件用法實(shí)例分析(附demo源碼)
這篇文章主要介紹了jquery拖動(dòng)層效果插件用法,結(jié)合實(shí)例形式分析了jquery-ui-1.7.2.custom.min.js插件的具體使用技巧,并附帶完整demo源碼供讀者下載參考,需要的朋友可以參考下2016-04-04
jQuery Ajax Post 回調(diào)函數(shù)不執(zhí)行問題的解決方法
本文是小編給大家?guī)淼膉Query Ajax Post 回調(diào)函數(shù)不執(zhí)行的原因及解決方法,在本文最下面還給大家附加jquery Ajax 不執(zhí)行回調(diào)函數(shù)success的原因,這兩個(gè)問題都非常多見,感興趣的朋友一起看下吧2016-08-08

