1.html代码
我的购物车
2.css代码
.car-header {
padding: 20px 0;
}
.car-logo img {
vertical-align: middle;
}
.car-logo b {
font-size: 20px;
margin-top: 20px;
margin-left: 10px;
}
.cart-filter-bar {
font-size: 16px;
color: #E2231A;
font-weight: 700;
}
.cart-filter-bar em {
padding: 5px;
border-bottom: 1px solid #E2231A;
}
.cart-thead {
height: 32px;
line-height: 32px;
margin: 5px 0 10px;
padding: 5px 0;
background: #f3f3f3;
border: 1px solid #e9e9e9;
border-top: 0;
position: relative;
}
.cart-thead>div,
.cart-item>div {
float: left;
}
.t-checkbox,
.p-checkbox {
height: 18px;
line-height: 18px;
padding-top: 7px;
width: 122px;
padding-left: 11px;
}
.t-goods {
width: 400px;
}
.t-price {
width: 120px;
padding-right: 40px;
text-align: right;
}
.t-num {
width: 150px;
text-align: center;
}
.t-sum {
width: 100px;
text-align: right;
}
.t-action {
width: 130px;
text-align: right;
}
.cart-item {
height: 160px;
border-style: solid;
border-width: 2px 1px 1px;
border-color: #aaa #f1f1f1 #f1f1f1;
background: #fff;
padding-top: 14px;
margin: 15px 0;
}
.check-cart-item {
background: #fff4e8;
}
.p-checkbox {
width: 50px;
}
.p-goods {
margin-top: 8px;
width: 565px;
}
.p-img {
float: left;
border: 1px solid #ccc;
padding: 5px;
}
.p-msg {
float: left;
width: 210px;
margin: 0 10px;
}
.p-price {
width: 110px;
}
.quantity-form {
width: 80px;
height: 22px;
}
.p-num {
width: 170px;
}
.decrement,
.increment {
float: left;
border: 1px solid #cacbcb;
height: 18px;
line-height: 18px;
padding: 1px 0;
width: 16px;
text-align: center;
color: #666;
margin: 0;
background: #fff;
margin-left: -1px;
}
.itxt {
float: left;
border: 1px solid #cacbcb;
width: 42px;
height: 18px;
line-height: 18px;
text-align: center;
padding: 1px;
margin: 0;
margin-left: -1px;
font-size: 12px;
font-family: verdana;
color: #333;
-webkit-appearance: none;
}
.p-sum {
font-weight: 700;
width: 145px;
}
/* 结算模块 */
.cart-floatbar {
height: 50px;
border: 1px solid #f0f0f0;
background: #fff;
position: relative;
margin-bottom: 50px;
line-height: 50px;
}
.select-all {
float: left;
height: 18px;
line-height: 18px;
padding: 16px 0 16px 9px;
white-space: nowrap;
}
.select-all input {
vertical-align: middle;
display: inline-block;
margin-right: 5px;
}
.operation {
float: left;
width: 200px;
margin-left: 40px;
}
.clear-all {
font-weight: 700;
margin: 0 20px;
}
.toolbar-right {
float: right;
}
.amount-sum {
float: left;
}
.amount-sum em {
font-weight: 700;
color: #E2231A;
padding: 0 3px;
}
.price-sum {
float: left;
margin: 0 15px;
}
.price-sum em {
font-size: 16px;
color: #E2231A;
font-weight: 700;
}
.btn-area {
font-weight: 700;
width: 94px;
height: 52px;
line-height: 52px;
color: #fff;
text-align: center;
font-size: 18px;
font-family: "Microsoft YaHei";
background: #e54346;
overflow: hidden;
}
/*清除元素默认的内外边距 */
* {
margin: 0;
padding: 0
}
/*让所有斜体 不倾斜*/
em,
i {
font-style: normal;
}
/*去掉列表前面的小点*/
li {
list-style: none;
}
/*图片没有边框 去掉图片底侧的空白缝隙*/
img {
border: 0; /*ie6*/
vertical-align: middle;
}
/*让button 按钮 变成小手*/
button {
cursor: pointer;
}
/*取消链接的下划线*/
a {
color: #666;
text-decoration: none;
}
a:hover {
color: #e33333;
}
button,
input {
font-family: 'Microsoft YaHei', 'Heiti SC', tahoma, arial, 'Hiragino Sans GB', \\5B8B\4F53, sans-serif;
/*取消轮廓线 蓝色的*/
outline: none;
}
body {
background-color: #fff;
font: 12px/1.5 'Microsoft YaHei', 'Heiti SC', tahoma, arial, 'Hiragino Sans GB', \\5B8B\4F53, sans-serif;
color: #666
}
.hide,
.none {
display: none;
}
/*清除浮动*/
.clearfix:after {
visibility: hidden;
clear: both;
display: block;
content: ".";
height: 0
}
.clearfix {
*zoom: 1
}
/*公共样式*/
.fl {
float: left;
}
.fr {
float: right;
}
@font-face {
font-family: 'icomoon';
src: url('../fonts/icomoon.eot?7kkyc2');
src: url('../fonts/icomoon.eot?7kkyc2#iefix') format('embedded-opentype'), url('../fonts/icomoon.ttf?7kkyc2') format('truetype'), url('../fonts/icomoon.woff?7kkyc2') format('woff'), url('../fonts/icomoon.svg?7kkyc2#icomoon') format('svg');
font-weight: normal;
font-style: normal;
}
.fr .icomoon {
font-family: 'icomoon';
font-size: 16px;
line-height: 26px;
}
/*版心*/
.w {
width: 1200px;
margin: 0 auto;
}
.style-red {
color: #c81623;
}
.spacer {
width: 1px;
height: 12px;
background-color: #666;
margin: 9px 12px 0;
}
/*顶部快捷导航*/
.shortcut {
height: 31px;
background-color: #f1f1f1;
line-height: 31px;
}
.shortcut li {
float: left;
}
/*header区域*/
.header {
position: relative;
height: 105px;
}
.logo {
position: absolute;
top: 25px;
left: 0;
width: 175px;
height: 56px;
}
.logo a {
display: block;
/*overflow: hidden;*/
width: 175px;
height: 56px;
background: url(../img/logo.png) no-repeat;
/*text-indent: -999px;*/
font-size: 0;
}
.search {
position: absolute;
top: 25px;
left: 348px;
}
.text {
float: left;
width: 445px;
height: 32px;
border: 2px solid #b1191a;
padding-left: 10px;
color: #ccc;
}
.btn {
float: left;
width: 82px;
height: 36px;
background-color: #b1191a;
border: 0;
font-size: 16px;
color: #fff;
}
.hotwrods {
position: absolute;
top: 65px;
left: 348px;
}
.hotwrods a {
margin: 0 10px;
}
.shopcar {
position: absolute;
top: 25px;
right: 64px;
width: 138px;
height: 34px;
border: 1px solid #dfdfdf;
background-color: #f7f7f7;
line-height: 34px;
text-align: center;
}
.car {
font-family: 'icomoon';
color: #da5555;
}
.arrow {
font-family: 'icomoon';
margin-left: 5px;
}
.count {
position: absolute;
top: -5px;
/*应该是左侧对齐 文字才能往右走显示*/
left: 100px;
background-color: #e60012;
height: 14px;
padding: 0 3px;
line-height: 14px;
color: #fff;
/*border-radius: 左上角 右上角 右下角 左下角;*/
border-radius: 7px 7px 7px 0;
}
/*nav start*/
.nav {
height: 45px;
border-bottom: 2px solid #b1191a;
}
.dropdown {
width: 209px;
height: 45px;
}
.dropdown .dt {
height: 100%;
background-color: #b1191a;
font-size: 16px;
color: #fff;
text-align: center;
line-height: 45px;
}
.dropdown .dd {
height: 465px;
background-color: #c81623;
margin-top: 2px;
}
.menu_item:hover {
background-color: #fff;
}
/*鼠标经过li 里面的 a变颜色*/
.menu_item:hover a {
color: #c81623;
}
.menu_item {
height: 31px;
line-height: 31px;
margin-left: 1px;
padding: 0 10px;
transition: all .5s;
}
.menu_item:hover {
padding-left: 20px;
}
.menu_item a {
font-size: 14px;
color: #fff;
}
.menu_item i {
float: right;
font-family: 'icomoon';
font-size: 18px;
color: #fff;
}
.navitems {
margin-left: 10px;
}
.navitems li {
float: left;
}
.navitems li a {
display: block;
height: 45px;
padding: 0 25px;
line-height: 45px;
font-size: 16px;
}
/*footer 部分*/
.footer {
height: 386px;
background-color: #f5f5f5;
padding-top: 30px;
}
.mod_service {
height: 79px;
border-bottom: 1px solid #ccc;
}
.mod_service li {
float: left;
width: 240px;
height: 79px;
}
.mod-service-icon {
/*浮动的盒子 可以直接给大小的 不需要转换*/
float: left;
width: 50px;
height: 50px;
margin-left: 35px;
background: url(../img/icons.png) no-repeat;
}
.mod_service_zheng {
background-position: -253px -3px;
}
.mod_service_tit {
float: left;
margin-left: 5px;
}
.mod_service_tit h5 {
margin: 5px 0;
}
.mod_service_kuai {
background-position: -255px -54px;
}
.mod_service_bao {
background-position: -257px -105px;
}
.mod_help {
height: 187px;
border-bottom: 1px solid #ccc;
}
.mod_help_item {
float: left;
width: 150px;
padding: 20px 0 0 50px;
}
.mod_help_item dt {
height: 25px;
font-size: 16px;
}
.mod_help_item dd {
height: 22px;
}
.mod_help_app dt,
.mod_help_app p {
padding-left: 15px;
}
.mod_help_app img {
margin: 7px 0;
}
.mod_copyright {
text-align: center;
}
.mod_copyright_links {
margin: 20px 0 15px 0;
}
.mod_copyright_info {
line-height: 18px;
}
3.jQuery代码
$(function () {
// 全选全不选
$(".checkall").click(function () {
let a = $(this).prop("checked")
$(".checkall").prop("checked", a)
$(".j-checkbox").prop("checked", a)
if (a == true) {
$(".j-checkbox").parents(".cart-item").addClass("check-cart-item")
}
else {
$(".j-checkbox").parents(".cart-item").removeClass("check-cart-item")
}
getSum()
})
$(".j-checkbox").click(function () {
getSum()
if ($(this).prop("checked") == true) {
$(this).parents(".cart-item").addClass("check-cart-item")
}
else {
$(this).parents(".cart-item").removeClass("check-cart-item")
}
let c = $(".j-checkbox:checked")
if (c.length == 3) {
$(".checkall").prop("checked", true)
}
else {
$(".checkall").prop("checked", false)
}
})
// 2.1 增商品数量
$(".increment").click(function () {
let a = Number($(this).prevAll(".itxt").val()) + 1
$(this).prevAll(".itxt").val(a)
let b = $(this).parents(".p-num").prevAll(".p-price").text().slice(1)
let c = (a * b).toFixed(2)
$(this).parents(".p-num").nextAll(".p-sum").text(`¥${c}`)
getSum()
})
// 2.2减商品数量
$(".decrement").click(function () {
let a = $(this).nextAll(".itxt").val() - 1
if (a <= 0) {
return
}
$(this).nextAll(".itxt").val(a)
let b = $(this).parents(".p-num").prevAll(".p-price").text().slice(1)
let c = (a * b).toFixed(2)
$(this).parents(".p-num").nextAll(".p-sum").text(`¥${c}`)
getSum()
})
// 修改表单
$(".itxt").keyup(function () {
$(this).val($(this).val().replace(/[^\d]/g, ''))
})
$(".itxt").keyup(function () {
if ($(this).val() == "") {
alert("不能为空")
$(this).val("1")
}
})
$(".itxt").blur(function () {
let a = Number($(this).val())
let b = parseFloat($(this).parents(".p-num").siblings(".p-price").text().slice(1))
$(this).parents(".p-num").siblings(".p-sum").text(`¥${(a * b).toFixed(2)}`)
})
// 总价
function getSum() {
let str = 0
let arr = 0
$(".j-checkbox").each(function (i, el) {
if ($(el).prop("checked") == true) {
str += parseFloat($(el).parents(".cart-item").find(".p-sum").text().slice(1))
arr += Number($(el).parents(".cart-item").find(".itxt").val())
}
})
$(".price-sum em").text(`¥${str.toFixed(2)}`)
$(".amount-sum em").text(arr)
}
// 删除
$(".p-action").click(function () {
$(this).parents(".cart-item").remove()
getSum()
})
$(".clear-all").click(function () {
$(this).parents(".cart-floatbar").siblings(".cart-item-list").remove()
getSum()
})
$(".remove-batch").click(function () {
$(".j-checkbox:checked").parents(".cart-item").remove()
getSum()
})
})
4.效果图