jQuery實(shí)現(xiàn)加入購物車飛入動(dòng)畫效果

HTML
首先載入jQuery庫文件和jquery.fly.min.js插件。
<script src="jquery.js"></script>
<script src="jquery.fly.min.js"></script>
接著,將商品信息html結(jié)構(gòu)布置好,本例中,我們用四個(gè)商品并排布置,每個(gè)商品box中包括有商品圖片、價(jià)格、名稱以及加入購物車按鈕等信息。
<div class="box">
<img src="images/lg.jpg" width="180" height="180">
<h4>¥<span>3499.00</span></h4>
<p>LG 49LF5400-CA 49寸IPS硬屏富貴招財(cái)銅錢設(shè)計(jì)</p>
<a href="#" class="button orange addcar">加入購物車</a>
</div>
<div class="box">
<img src="images/hs.jpg" width="180" height="180">
<h4>¥<span>3799.00</span></h4>
<p>Hisense/海信 LED50T1A 海信電視官方旗艦店</p>
<a href="#" class="button orange addcar">加入購物車</a>
</div>
<div class="box">
<img src="images/cw.jpg" width="180" height="180">
<h4>¥<span>¥3999.00</span></h4>
<p>Skyworth/創(chuàng)維 50E8EUS 8核4Kj極清酷開系統(tǒng)智能液晶電視</p>
<a href="#" class="button orange addcar">加入購物車</a>
</div>
<div class="box">
<img src="images/ls.jpg" width="180" height="180">
<h4>¥<span>6969.00</span></h4>
<p>樂視TV Letv X60S 4核1080P高清3D安卓智能超級(jí)電視</p>
<a href="#" class="button orange addcar">加入購物車</a>
</div>
然后,我們還需要在頁面的右側(cè)加上購物車以及提示信息。
<div class="m-sidebar">
<div class="cart">
<i id="end"></i>
<span>購物車</span>
</div>
</div>
<div id="msg">已成功加入購物車!</div>
CSS
我們使用CSS先將商品排列美化,然后設(shè)置右側(cè)購物車樣式,具體請(qǐng)看代碼:
.box{float:left; width:198px; height:320px; margin-left:5px; border:1px solid #e0e0e0; text-align:center}
.box p{line-height:20px; padding:4px 4px 10px 4px; text-align:left}
.box:hover{border:1px solid #f90}
.box h4{line-height:32px; font-size:14px; color:#f30;font-weight:500}
.box h4 span{font-size:20px}
.u-flyer{display: block;width: 50px;height: 50px;border-radius: 50px;position: fixed;z-index: 9999;}
.m-sidebar{position: fixed;top: 0;right: 0;background: #000;z-index: 2000;width: 35px;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:0 auto;}
.cart i{width:35px;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}
jQuery
我們要實(shí)現(xiàn)的效果是,當(dāng)用戶點(diǎn)擊“加入購物車”按鈕時(shí),當(dāng)前商品圖片會(huì)變成一個(gè)縮小的圓球,以按鈕為起點(diǎn),向右側(cè)以拋物線的形式飛出,最后落入頁面右側(cè)的購物車?yán)铮⑻崾静僮鞒晒?。在飛出之前,我們要獲取當(dāng)前商品的圖片,然后調(diào)用fly插件,之后的拋物線軌跡都是由fly插件完成,我們只需定義起點(diǎn)和終點(diǎn)等參數(shù)即可。
<script>
$(function() {
var offset = $("#end").offset();
$(".addcar").click(function(event){
var addcar = $(this);
var img = addcar.parent().find('img').attr('src');
var flyer = $('<img class="u-flyer" src="'+img+'">');
flyer.fly({
start: {
left: event.pageX, //開始位置(必填)#fly元素會(huì)被設(shè)置成position: fixed
top: event.pageY //開始位置(必填)
},
end: {
left: offset.left+10, //結(jié)束位置(必填)
top: offset.top+10, //結(jié)束位置(必填)
width: 0, //結(jié)束時(shí)寬度
height: 0 //結(jié)束時(shí)高度
},
onEnd: function(){ //結(jié)束回調(diào)
$("#msg").show().animate({width: '250px'}, 200).fadeOut(1000); //提示信息
addcar.css("cursor","default").removeClass('orange').unbind('click');
this.destory(); //移除dom
}
});
});
});
</script>
復(fù)制上面的代碼,保存并運(yùn)行即可看到效果,F(xiàn)ly插件的項(xiàng)目官網(wǎng)地址是:https://github.com/amibug/fly,值得一提的是,IE10以下需要添加以下js:
<script src="requestAnimationFrame.js"></script>
以上就是本文的全部內(nèi)容了,希望大家能夠喜歡
- jQuery實(shí)現(xiàn)購物車多物品數(shù)量的加減+總價(jià)計(jì)算
- JQuery實(shí)現(xiàn)的購物車功能(可以減少或者添加商品并自動(dòng)計(jì)算價(jià)格)
- jQuery實(shí)現(xiàn)購物車計(jì)算價(jià)格功能的方法
- 純jquery實(shí)現(xiàn)模仿淘寶購物車結(jié)算
- 基于JQuery實(shí)現(xiàn)的類似購物商城的購物車
- jQuery實(shí)現(xiàn)購物車數(shù)字加減效果
- jQuery+HTML5加入購物車代碼分享
- jquery實(shí)現(xiàn)購物車基本功能
- jquery購物車結(jié)算功能實(shí)現(xiàn)方法
- jQuery實(shí)現(xiàn)購物車
相關(guān)文章
addEventListener—jQuery的事件監(jiān)聽方法
在Javascript中,事件監(jiān)聽是非常重要的,通過事件監(jiān)聽,我們可以在用戶執(zhí)行某些操作時(shí)觸發(fā)相應(yīng)的處理程序。最初,Javascript監(jiān)聽事件的方式是addEvent。addEvent()比較麻煩,所以jQuery為我們提供了一個(gè)更為便捷的事件監(jiān)聽方法:addEventListener。2023-06-06
jQuery中圖片展示插件highslide.js的簡單dom
Highslide是一個(gè)非常美觀的圖片燈箱插件,下面這篇文章主要給大家介紹了關(guān)于jQuery中圖片展示插件highslide.js的簡單dom,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
jQuery+css實(shí)現(xiàn)非常漂亮的水平導(dǎo)航菜單效果
這篇文章主要介紹了jQuery+css實(shí)現(xiàn)非常漂亮的水平導(dǎo)航菜單效果,通過each方法遍歷元素再使用removeClass與addClass變換頁面元素樣式實(shí)現(xiàn)導(dǎo)航菜單的切換效果,需要的朋友可以參考下2016-07-07
jquery實(shí)現(xiàn)拖拽調(diào)整Div大小
這里給大家分享的是一段自己編寫的使用jQuery實(shí)現(xiàn)拽調(diào)整Div層大小的代碼,非常實(shí)用,推薦給有需要的小伙伴們。2015-01-01
jquery使用$(element).is()來判斷獲取的tagName
這篇文章主要介紹了jquery使用$(element).is()來判斷獲取的tagName以及將取到標(biāo)簽用作到別的地方,需要的朋友可以參考下2014-08-08

