基于JQuery的購物車添加刪除以及結(jié)算功能示例
前段時(shí)間了解到購物車結(jié)算算是一個(gè)難點(diǎn)部分,在網(wǎng)上也找了一些,但是網(wǎng)上除了插件之外,就是一些半成品,比如一部分只有添加刪除效果,另一部分只有結(jié)算功能,很少見到整合在一起的購物車效果,因此自己寫了一個(gè),方便大家查看
(添加效果沒有飛入,實(shí)在懶得寫動畫效果了,湊合看吧)
HTML部分
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="css/index.css" rel="external nofollow" >
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/index.js" type="text/javascript" charset="utf-8" async defer></script>
</head>
<body>
<div id="banner"></div>
<div id="container">
<ul>
<li num="1"><img src="images/1.jpg"/><span class="things_name">2014年春季爆品A<p>$<i>10</i><span class="buy">點(diǎn)擊購買</p></li>
<li num="2"><img src="images/2.jpg"/><span class="things_name">2014年春季爆品B<p>$<i>20</i><span class="buy">點(diǎn)擊購買</p></li>
<li num="3"><img src="images/3.jpg"/><span class="things_name">2014年春季爆品C<p>$<i>30</i><span class="buy">點(diǎn)擊購買</p></li>
<li num="4"><img src="images/4.jpg"/><span class="things_name">2014年春季爆品D<p>$<i>40</i><span class="buy">點(diǎn)擊購買</p></li>
</ul>
</div>
<div id="carlist">
<div class="car">
<div>
<span class="carLogo"><span><img src="images/car.png"/>
<span class="txt">購<br />物<br />車
</div>
</div>
<div class="list">
<!--此處添加動態(tài)元素-->
<div class="total">
<span>總價(jià):<span>0元
</div>
</div>
</div>
<script type="text/javascript">
function view(){
return{
w:document.documentElement.clientWidth,
h:document.documentElement.clientHeight
};
}
document.body.style.height=view().h+"px";
</script>
</body>
</html>
JS部分
$(function(){
var mark=0;
$(".car").on("click",function(){
if(mark==0){
$("#carlist").animate({marginRight:"0px"},500)
mark=1;
}else{
$("#carlist").animate({marginRight:"-260px"},500)
mark=0
}
})
//點(diǎn)擊購買按鈕添加至購物車
var buyButton=$(".buy");
buyButton.on("click",BuyClick)
function BuyClick(){
var thingsName=$(this).parents("li").find(".things_name").text();
var thingsPrice=$(this).parent().find("i").text();
var thingsImage=$(this).parents("li").find("img").attr("src");
var kNum=$(this).parents("li").attr("num")
var Geshu=1;
$(this).off("click").text("已經(jīng)添加至購物車");
$(".list").append('<div class="select things" num='+kNum+'><img src='+thingsImage+'/><p class="name">'+thingsName+'</p><p class="price">$<i>'+thingsPrice+'</i></p><ul class="caozuo"><li class="zengjian"><span class="minus">-<span>1<span class="add">+</li><li class="del">刪除</li></ul></div>');
countTotalPrice();
totalGeshu();
//點(diǎn)擊加號添加商品個(gè)數(shù)
$(".add").off("click").on("click",function(){
Geshu=parseInt($(this).parent().find("span:nth-of-type(2)").text())
Geshu++
$(this).parent().find("span:nth-of-type(2)").text(Geshu)
countTotalPrice();
totalGeshu();
})
//動態(tài)生成的元素點(diǎn)擊減號減少商品個(gè)數(shù)
$(".minus").off("click").on("click",function(){
Geshu=parseInt($(this).parent().find("span:nth-of-type(2)").text());
if(Geshu>1){
Geshu--;
$(this).parent().find("span:nth-of-type(2)").text(Geshu)
}else{
Geshu==1;
}
countTotalPrice();
totalGeshu();
})
//刪除購物車內(nèi)的商品
var del=$(".del");
del.each(function(){
$(this).off("click").on("click",function(){
var delName=$(this).parents(".things").find(".name").text();
$(this).parents(".things").remove();
countTotalPrice();
totalGeshu();
var oldBtn=$("#container ul li").find("span:contains("+delName+")").parents("li").find(".buy")
oldBtn.on("click",BuyClick).text("點(diǎn)擊購買")
})
})
//計(jì)算總價(jià)函數(shù)
function countTotalPrice(){
var totalPrice=0,listThings=$(".list").find(".things");
for (var i=0;i<listThings.length;i++) {
var this_geshu=parseInt(listThings.eq(i).find(".zengjian span:nth-of-type(2)").text());
var this_price=parseInt(listThings.eq(i).find(".price i").text());
totalPrice+=this_geshu*this_price;
}
$(".total span").eq(1).text(totalPrice);
totalGeshu();
}
//購物車上的商品總數(shù)
function totalGeshu(){
var listThings=$(".list").find(".things");
if (listThings.length>0) {
var totalGeshu=0;
listThings.each(function(){
var this_geshu=parseInt($(this).find(".zengjian span:nth-of-type(2)").text());
totalGeshu+=this_geshu;
})
$(".carLogo span").html(totalGeshu)
} else{
$(".carLogo span").css("display","none")
}
}
}
})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery實(shí)現(xiàn)移動端手機(jī)商城購物車功能
- jQuery實(shí)現(xiàn)購物車多物品數(shù)量的加減+總價(jià)計(jì)算
- jQuery實(shí)現(xiàn)加入購物車飛入動畫效果
- JQuery實(shí)現(xiàn)的購物車功能(可以減少或者添加商品并自動計(jì)算價(jià)格)
- 純jquery實(shí)現(xiàn)模仿淘寶購物車結(jié)算
- 基于JQuery實(shí)現(xiàn)的類似購物商城的購物車
- jQuery+HTML5加入購物車代碼分享
- jQuery實(shí)現(xiàn)類似淘寶購物車全選狀態(tài)示例
- jquery購物車結(jié)算功能實(shí)現(xiàn)方法
- jQuery仿天貓實(shí)現(xiàn)超炫的加入購物車
- jquery制作的移動端購物車效果完整示例
相關(guān)文章
jquery實(shí)現(xiàn)倒計(jì)時(shí)小應(yīng)用
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)倒計(jì)時(shí)小應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
用jquery實(shí)現(xiàn)的一個(gè)超級簡單的下拉菜單
這篇文章主要介紹了用jquery實(shí)現(xiàn)的一個(gè)超級簡單的下拉菜單,需要的朋友可以參考下2014-05-05
jquery 刪除節(jié)點(diǎn) 添加節(jié)點(diǎn) 找兄弟節(jié)點(diǎn)的簡單實(shí)現(xiàn)
下面小編就為大家?guī)硪黄猨query 刪除節(jié)點(diǎn) 添加節(jié)點(diǎn) 找兄弟節(jié)點(diǎn)的簡單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12
jQuery實(shí)現(xiàn)web頁面櫻花墜落的特效
這篇文章主要介紹了jQuery實(shí)現(xiàn)web頁面櫻花墜落的特效,效果非常棒,需要的朋友可以參考下2017-06-06
JQuery驗(yàn)證jsp頁面屬性是否為空(實(shí)例代碼)
JQuery驗(yàn)證jsp頁面屬性是否為空的實(shí)例代碼。需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11

