购物车效果实现

先放效果图:
效果图1
效果图2
还是jQuery的应用,猛然发现以前的笔记中还是有很多东西的;
html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>购物车的实现</title>
    <link rel="stylesheet" href="css/homework.css">
</head>
<body>
<div class="catbox">

    <table id="cartTable">
        <thead>
        <tr>
            <th><label><input class="check-all check" type="checkbox"/>&nbsp;全选</label></th>
            <th>商品</th>
            <th>单价</th>
            <th>数量</th>
            <th>小计</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td class="checkbox"><input class="check-one check" type="checkbox"/></td>
            <td class="goods"><img src="images/cart1.jpg" alt=""/><span>Casio/卡西欧 EX-TR350</span></td>
            <td class="price">5999.88</td>
            <td class="count"><span class="reduce"></span><input class="count-input" type="text" value="1"/><span class="add">+</span></td>
            <td class="subtotal">5999.88</td>
            <td class="operation"><span class="delete">删除</span></td>
        </tr>
        <tr>
            <td class="checkbox"><input class="check-one check" type="checkbox"/></td>
            <td class="goods"><img src="images/cart2.jpg" alt=""/><span>Canon/佳能 PowerShot SX50 HS</span></td>
            <td class="price">3888.50</td>
            <td class="count"><span class="reduce"></span><input class="count-input" type="text" value="1"/><span class="add">+</span></td>
            <td class="subtotal">3888.50</td>
            <td class="operation"><span class="delete">删除</span></td>
        </tr>
        <tr>
            <td class="checkbox"><input class="check-one check" type="checkbox"/></td>
            <td class="goods"><img src="images/cart3.jpg" alt=""/><span>Sony/索尼 DSC-WX300</span></td>
            <td class="price">1428.50</td>
            <td class="count"><span class="reduce"></span><input class="count-input" type="text" value="1"/><span class="add">+</span></td>
            <td class="subtotal">1428.50</td>
            <td class="operation"><span class="delete">删除</span></td>
        </tr>
        <tr>
            <td class="checkbox"><input class="check-one check" type="checkbox"/></td>
            <td class="goods"><img src="images/cart4.jpg" alt=""/><span>Fujifilm/富士 instax mini 25</span></td>
            <td class="price">640.60</td>
            <td class="count"><span class="reduce"></span><input class="count-input" type="text" value="1"/><span class="add">+</span></td>
            <td class="subtotal">640.60</td>
            <td class="operation"><span class="delete">删除</span></td>
        </tr>
        </tbody>
    </table>

    <div class="foot" id="foot">
        <label class="fl select-all"><input type="checkbox" class="check-all check"/>&nbsp;全选</label>
        <a class="fl delete" id="deleteAll" href="#">删除</a>
        <div class="fr closing">结 算</div>
        <div class="fr total">合计:¥<span id="priceTotal">0.00</span></div>
        <div class="fr selected" id="selected">已选商品<span id="selectedTotal">0</span><span class="arrow up"></span><span class="arrow down"></span></div>
        <div class="selected-view">
            <div id="selectedViewList" class="clearfix">
<!--<div><img src="images/cart1.jpg"><span>取消选择</span></div>-->
</div>
            <span class="arrow"><span></span></span>
        </div>
    </div>

</div>

<script type="text/javascript" src="jquery-1.12.1.min.js"></script>
<script type="text/javascript" src="js/homework_priceAndDel.js"></script>
<script type="text/javascript" src="js/homework_moneyAndChosen.js"></script>
</body>
</html>

css代码:

* {
    margin: 0;
    padding: 0;
    list-style-type: none;
}

a {
    color: #666;
    text-decoration: none;
}

table {
    border-collapse: collapse;
    border-spacing: 0;
    border: 0;
}

body {
    color: #666;
    font: 12px/180% Arial, Helvetica, sans-serif, "微软雅黑";
}

.clearfix:after {
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;/*visible是默认值,即是可见的*/
}

.clearfix {
    display: inline-table;
}

* html .clearfix {
    height: 1%;
}

.clearfix {
    display: block;
}

* + html .clearfix {
    min-height: 1%;
}

.fl {
    float: left;
}

.fr {
    float: right;
}

.catbox {
    width: 940px;
    margin: 0 auto;
}

.catbox table {
    text-align: center;
    width: 100%;
}

.catbox table th, .catbox table td {
    border: 1px solid #CADEFF;
}

.catbox table th {
    background: #e2f2ff;
    border-top: 3px solid #a7cbff;
    height: 30px;
}

.catbox table td {
    padding: 10px;
    color: #444;
}

.catbox table tbody tr:hover {
    background: RGB(238, 246, 255);
}

.checkbox {
    width: 60px;
}

.goods {
    width: 300px;
}

.goods span {
    width: 180px;
    margin-top: 20px;
    text-align: left;
    float: left;
}

.goods img {
    width: 100px;
    height: 80px;
    margin-right: 10px;
    float: left;
}

.price {
    width: 130px;
}

.count {
    width: 90px;
}

.count .add, .count input, .count .reduce {
    float: left;
    margin-right: -1px;
    position: relative;
    z-index: 0;
}

.count .add, .count .reduce {
    height: 23px;
    width: 17px;
    border: 1px solid #e5e5e5;
    background: #f0f0f0;
    text-align: center;
    line-height: 23px;
    color: #444;
    -webkit-user-select: none;
}

.count .add:hover, .count .reduce:hover {
    color: #f50;
    z-index: 3;
    border-color: #f60;
    cursor: pointer;
}

.count input {
    width: 50px;
    height: 15px;
    line-height: 15px;
    border: 1px solid #aaa;
    color: #343434;
    text-align: center;
    padding: 4px 0;
    background-color: #fff;
    z-index: 2;
}

.subtotal {
    width: 150px;
    color: red;
    font-weight: bold;
}

.operation span:hover, a:hover {
    cursor: pointer;
    color: red;
    text-decoration: underline;
}

.foot {
    margin-top: 10px;
    color: #666;
    height: 48px;
    border: 1px solid #c8c8c8;
    background-color: #eaeaea;
    background-image: linear-gradient(RGB(241, 241, 241), RGB(226, 226, 226));
    position: relative;
    z-index: 8;
}

.foot div, .foot a {
    line-height: 48px;
    height: 48px;
}

.foot .select-all {
    width: 100px;
    height: 48px;
    line-height: 48px;
    padding-left: 5px;
    color: #666;
}

.foot .closing {
    border-left: 1px solid #c8c8c8;
    width: 100px;
    text-align: center;
    color: #000;
    font-weight: bold;
    background: RGB(238, 238, 238);
    cursor: pointer;
}

.foot .total {
    margin: 0 20px;
    cursor: pointer;
}

.foot #priceTotal, .foot #selectedTotal {
    color: red;
    font-family: "Microsoft Yahei";
    font-weight: bold;
}

.foot .selected {
    cursor: pointer;
}

.foot .selected .arrow {
    position: relative;
    top: -3px;
    margin-left: 3px;
}

.foot .selected .down {
    position: relative;
    top: 3px;
    display: none;
}

.show .selected .down {
    display: inline;
}

.show .selected .up {
    display: none;
}

.foot .selected:hover .arrow {
    color: red;
    -webkit-user-select: none;
}

.foot .selected-view {
    width: 935px;
    border: 1px solid #c8c8c8;
    position: absolute;
    height: auto;
    background: #ffffff;
    z-index: 9;
    bottom: 48px;
    left: -1px;
    display: none;
}

.show .selected-view {
    display: block;
}

.foot .selected-view div {
    height: auto;
}

.foot .selected-view .arrow {
    font-size: 16px;
    line-height: 100%;
    color: #c8c8c8;
    position: absolute;
    right: 330px;
    bottom: -9px;
}

.foot .selected-view .arrow span {
    color: #ffffff;
    position: absolute;
    left: 0px;
    bottom: 1px;
}

#selectedViewList {
    padding: 10px 20px 10px 20px;
}

#selectedViewList div {
    display: inline-block;
    position: relative;
    width: 100px;
    height: 80px;
    border: 1px solid #ccc;
    margin: 10px;
    float: left;
}

#selectedViewList div img {
    width: 100px;
    height: 80px;
    margin-right: 10px;
    float: left;
}

#selectedViewList div span {
    display: none;
    color: #ffffff;
    font-size: 12px;
    position: absolute;
    top: 0px;
    right: 0px;
    width: 60px;
    height: 18px;
    line-height: 18px;
    text-align: center;
    background: #000;
    cursor: pointer;
}

#selectedViewList div:hover span {
    display: block;
}

js1–价格和删除:

/**
 * Created by Administrator on 2016/3/15.
 */
$(document).ready(function(){
$(".price").each(function(){
var _this=this;//保存--得到对应行数单价的元素
//alert(+$(this).text());//获取当前的单价
//alert(+$(this).closest("tr").find(".subtotal").text());//获取小计
//alert($(this).next().find("input").val());//获得数量
//alter($(this).next(".add"));// 获得加的按钮(事实上不是按钮,只是一个div)
var count = $(this).next().find(".count-input").val();//得到对应的数量

//点击数量增加的时候的事件
$(this).next().find(".add").click(function(){
var num1 = +$(this).prev(".count-input").val();
if(num1==null||num1==""||num1==0||num1=="0"){
num1=1;
            }
num1++;
showReduce(_this);
$(this).prev(".count-input").val(num1);
total(_this);
        });
//点击数量减的时候的事件
$(this).next().find(".reduce").click(function(){
var num1 = +$(this).next(".count-input").val();
if(num1>1){
num1--;
$(this).next(".count-input").val(num1);
total(_this);
            }else{
hiddenReduce(_this);
return;
            }
        });
// 数量改变时触发事件(前提是失去焦点才会触发)-----键盘输入数字的时候
$(this).next().find(".count-input").change(function(){
if(check(_this)){
if((+$(this).val())>=1){
showReduce(_this);
                }
total(_this);
            }else{
hiddenReduce(_this);
$(this).val(1);
alert("输入错误,请重新输入!");
            }
        });
//点击删除的时候触发的事件
$(this).closest("tr").find(".delete").click(function(){
$(this).closest("tr").remove();
        });
    });
});
function showReduce(obj){
$(obj).next().find(".reduce").text("-");
$(obj).next().find(".reduce").css("cursor","pointer");
}
function hiddenReduce(obj){
$(obj).next().find(".reduce").text("");
$(obj).next().find(".reduce").css("cursor","default");
}
function check(obj){
var num = $(obj).next().find(".count-input").val();
if(isNaN(num)||parseInt(num)<=0){
hiddenReduce(obj);
return false;
    }else {
$(obj).next().find(".count-input").val(parseInt(num));
return true;
    }
}
function total(obj){
var price = +$(obj).text();//得到对应的单价
var count = +$(obj).next().find(".count-input").val();//得到对应的数量
$(obj).closest("tr").find(".subtotal").text((price*count).toFixed(2));
}

js2–价格和选择:

/**
 * Created by Administrator on 2016/3/15.
 */
$(document).ready(function(){
//批量删除的事件
$("#deleteAll").click(function(){
$(".check-one").each(function(){
if($(this).is(":checked")){
$(this).closest("tr").remove();
            }
        });
allTotal();
    });
//全选的复选框的点击事件(此处可以用点击click)
$(".check-all").change(function(){
if($(this)[0].checked){
for(var i =0; i<$(".check").length;i++){
$(".check")[i].checked=true;
            }
        }else{
for(var i =0; i<$(".check").length;i++){
$(".check")[i].checked=false;
            }
        }
allTotal();
    });
//复选框被改变时触发合计金额的事件
$(".check-one").change(function(){
allTotal();
    });
//数量发生改变时也要触发合计金额的事件
$(".count-input").change(function(){
allTotal();
    });
//数量按钮被点击的时候也要触发合计金额的事件
$(".reduce,.add").on("click",function(){
allTotal();
    });
/*//在点击已选商品的弹出按钮时触发的事件
    $(".up").click(function(){
        $(this).closest("div").find(".arrow").toggle();
        $(".selected-view").toggle();
        alert($(".selected-view").css("display"))

    });*/
    //在点击已选商品的收回按钮时触发的事件
$("#selected").click(function(){
$(this).closest("div").find(".arrow").toggle();
$(".selected-view").toggle();
if($(".selected-view").css("display")=="block"){
var imgSrc=[];
imgSrc=chooseGoods();//立即获得已经选中图片的地址
if(imgSrc.length==0){
$("#selectedViewList").append("<h4>您没有选择任何商品!</h4>");
return;
            }
for(var i = 0; i<imgSrc.length; i++) {
chosen(imgSrc[i]);
            }
        }else if($(".selected-view").css("display")=="none"){
$("#selectedViewList").children().each(function(){
$(this).remove();
            });
        }
    });
//添加已选商品的图片放入已选商品的div中
function chosen(imgSrc){
$("#selectedViewList").append("<div><img src=\""+imgSrc+"\"><span onclick='cancelChoose(this)'>取消选择</span></div>");
    }
});
//取消选择的点击事件
function cancelChoose(obj){
$(obj).closest("div").remove();
$(".check-one").each(function(){
var c1 = $(this).closest("td").next().find("img").attr("src");
var c2 = $(obj).prev().attr("src");
if(c1==c2){
this.checked=false;//这里让对应的复选框的状态改为不选中
allTotal();
return;
        }
    });
}
//计算所选商品总数和合计价格的方法,可以和下面的得到图片(得到表格的行td)方法合并---可优化!!!!
function allTotal(){
var allTotal= 0,
nums=0;
$(".subtotal").each(function(){
var isChecked=$(this).closest("tr").find(".check-one");
if(isChecked.is(":checked")){
allTotal = +$(this).text()+allTotal;
nums=nums+(+$(this).closest("tr").find(".count-input").val());
        }
    });
$("#priceTotal").text(allTotal.toFixed(2));
$("#selectedTotal").text(nums);
}
//即时得到选择了的图片地址,其实这个方法可以改善为返回对应的td单元格元素,便于其他地方调用
function chooseGoods(){
var imgSrc=[];
$(".check-one").each(function(){
if($(this).is(":checked")){
var src=$(this).closest("tr").find("img").attr("src");//获取选中的元素的图片地址
imgSrc.push(src);
        }
    });
return imgSrc;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript购物车实现效果可以非常丰富和实用。通过使用JavaScript编写的购物车功能,可以构建一个类似于电子商务网站中常见的购物车系统。 首先,在网页的商品列表中,通过JavaScript代码将每个商品的信息进行绑定和处理。当用户点击将商品添加到购物车按钮时,JavaScript将捕获用户的点击事件,并从商品列表中获取相关信息,例如商品的id、名称、价格以及数量等。 接下来,JavaScript会将获取到的商品信息保存到一个购物车对象中,这个购物车对象可以是一个数组或者是一个json对象。购物车对象会在用户点击添加商品按钮时实时更新,以反映当前购物车中的商品情况。 同时,JavaScript还会对购物车进行一些额外的处理。例如,当用户点击购物车图标时,JavaScript会将购物车对象中的商品信息动态显示在购物车弹窗中,包括商品的名称、价格和数量等。用户还可以在购物车中修改商品数量、删除商品或者清空购物车等操作。 此外,JavaScript还可以实现一些购物车的功能增强。例如,当用户修改商品的数量时,JavaScript会自动更新购物车中的商品总价和数量,并将其实时展示给用户。另外,JavaScript还可以对购物车中的商品进行排序、搜索和筛选等操作,以提供更好的用户体验。 总之,通过使用JavaScript实现购物车功能,我们可以实现一个用户友好、交互性强的购物车系统,为用户提供便捷、高效的购物体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值