<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>新鲜水果 | 小毛虫生鲜</title>
<link rel="stylesheet" type="text/css" href="./css/goods_details.css">
<link rel="stylesheet" type="text/css" href="./css/basic.css">
<!-- <link rel="stylesheet" type="text/css" href="./css/main.css"> -->
</head>
<body>
<!-- 顶部 -->
<div class="outside header_con">
<div class="header">
<div class="welcome fl">欢迎来到小毛虫生鲜!
<a href="http://127.0.0.1:3001/adminlogin.html">[管理员登录]</a>
</div>
<div class="fr">
<div class="login_btn fl">
<a href="/login.html">登录</a>
<span>|</span>
<a href="/register.html">注册</a>
</div>
<div class="user_link fl">
<span>|</span>
<a href="index.html">首页</a>
<span>|</span>
<a href="user_info.html">用户中心</a>
<span>|</span>
<a href="mycart.html">我的购物车</a>
</div>
</div>
</div>
</div>
<div class="outside">
<div class="search_bar clearfix">
<a href="/" class="logo fl"><img src="./images/base-img/logo.jpg"></a>
<div class="search_con fl">
<input type="text" class="input_text fl" name="q" placeholder="搜索商品">
<input type="button" class="input_btn fr" name="" value="搜索">
</div>
<div class="guest_cart fr">
<a href="mycart.html" class="cart_name fl">我的购物车</a>
<div class="goods_count fl" id="show_count">0</div>
</div>
</div>
</div>
<!-- 中间样式 -->
<div class="outside navbar_con">
<div class="navbar clearfix">
<div class="subnav_con fl">
<h1>全部商品分类</h1>
<span></span>
<ul class="subnav">
<li><a href="goods_list.html?lid=1" class="fruit">新鲜水果</a></li>
<li><a href="goods_list.html?lid=2" class="seafood">海鲜水产</a></li>
<li><a href="goods_list.html?lid=3" class="meet">猪牛羊肉</a></li>
<li><a href="goods_list.html?lid=4" class="egg">禽类蛋品</a></li>
<li><a href="goods_list.html?lid=5" class="vegetables">新鲜蔬菜</a></li>
<li><a href="goods_list.html?lid=6" class="ice">速冻食品</a></li>
</ul>
</div>
<ul class="navlist fl">
<li><a href="/">返回首页 >></a></li>
</ul>
</div>
</div>
<!--二级页面面包屑导航-->
<div class="outside">
<div class="breadcrumb">
全部分类 > 商品详情
</div>
</div>
<div class="outside">
<div class="goods_detail_con clearfix">
<!--动态数据存放处-->
</div>
</div>
<div class="outside">
<div class="main_wrap clearfix">
<div class="r_wrap fr clearfix">
<ul class="detail_tab clearfix">
<li class="active">商品介绍</li>
<!-- <li>评论</li> -->
</ul>
<div class="tab_content">
<!-- <dl>
<dt>商品详情:</dt>
<dd><p>阿斯蒂芬阿三的说法</p></dd>
</dl> -->
</div>
</div>
</div>
</div>
<div class="add_jump"></div>
<!-- 底部 -->
<div class="footer no-mp">
<div class="foot_link">
<a href="https://github.com/weilanhanf/daily_fresh_demo">关于我们</a>
<span>|</span>
<a href="https://www.cnblogs.com/welan/">联系我们</a>
<span>|</span>
<a href="#">招聘人才</a>
<span>|</span>
<a href="#">友情链接</a>
</div>
<p>CopyRight © 2020 大佬说的都队 All Rights Reserved</p>
<p>电话:17329410120 京ICP备123456789号</p>
</div>
<script type="text/javascript" src="./js/jquery.min.js"></script>
<script src="./js/details.js"></script>
<script src="./js/addcart.js"></script>
<script src="./js/out_login.js"></script>
<script src="./js/searchbar.js"></script>
<script type="text/javascript">
// 用户点击 + 时,商品总价格的改变
function localAdd() {
var num = parseFloat($('#num_show').val());
var kucun = parseFloat($('.goods_kucun').text());
// 如果商品数量<库存,则数量+1
if (num <= kucun) {
num += 1;
// console.log(num);
$('#num_show').val(num);
// $.number();
// 获取当前售价
price = parseInt($('#gprice').text());
// console.log(price);
// 数量*当前售价
total = num * price;
$('#num_show').val(num);
$('#gtotal').text(total.toFixed(2) + '元');
} else {
alter("不能购买数量大于库存");
}
}
// 用户点击 - 时的总价格
function localReduce() {
var num = parseFloat($('#num_show').val());
if (num > 1) {
num -= 1;//数量减一
// console.log(num);
$('#num_show').val(num);
// $.number();
//原先的价格
price = parseInt($('#gprice').text());
//获取点击增加按钮时的价格
price_now = parseInt($('#gtotal').text());
total = price_now - price;
$('#num_show').val(num);
$('#gtotal').text(total.toFixed(2) + '元');
}
else {
alert("购买数量不能少于1");
}
};
// 用户点击立即购买按钮时,携带者当前商品的id和数量,跳转到结算页面
function buy_account(lid){
if($('.login_btn').text().indexOf('登录') >= 0){
alert('请登陆后购买');
// location.href = "/login.html";
location.href = '/login.html';
return;
}
// 商品的数量
var num =$('#num_show').val();
// 跳转
window.location.href=`buy_account.html?lid=${lid}&num=${num}`;
}
</script>
</body>
</html>
js代码:(out_login.js \ searchbar.js两个js代码在首页博客里面有)
details.js(商品详情的js代码)
// 获取从首页获得的商品id
//http://127.0.0.1:3000/goods_tails.html?lid= id
var lid=location.search.split("=")[1];
console.log(lid);
$.ajax({
url:"http://127.0.0.1:3000/details",
type:"get",
data:{lid},
dataType:"json"
})
.then(function(result){
console.log(result[0]);
// 商品详情
var html=`<div class="goods_detail_pic fl">
<img src="${result[0].img_url}">
</div>
<div class="goods_detail_list fr">
<h3 class="title">${result[0].title}</h3>
<p class="chandi">产地:<a href="${result[0].herf}" class="herf">内蒙古</a></p>
<div class="prize_bar">
<span class="show_pirze">¥<em id="gprice">${result[0].price.toFixed(2)} </em></span>
<span class="show_unit" id="unit">单 位:${result[0].unit}</span>
<span class="show_unit">库 存:<span class="goods_kucun">${result[0].num}</span></span>
</div>
<div class="goods_num clearfix">
<div class="num_name fl">数 量:</div>
<div class="num_add fl">
<input type="text" id="num_show" class=" fl" value="1">
<a href="javascript:localAdd();" class="add fr">+</a>
<a href="javascript:localReduce();" class="reduce fr">-</a>
</div>
</div>
<div class="total">总价:<em id="gtotal">${result[0].price.toFixed(2)}元</em></div>
<div class="operate_btn">
<a href="javascript:buy_account(lid);" class="buy_btn">立即购买</a>
<a href="javascript:add_cart(lid);" class="add_cart" id="add_cart">加入购物车</a>
</div>`;
var div1=$(".goods_detail_con");
// console.log(div1);
div1.html(html);
// 商品介绍
var html1=`
<dl>
<dt>商品详情:</dt>
<dd><p>${result[0].intro}</p></dd>
</dl>`;
var div2=$(".tab_content");
div2.html(html1);
})
// })
addcart.js(添加购物车的js代码)
// 用户点击 加入购物车 按钮时
function add_cart(lid) {
// console.log(lid);
if($('.login_btn').text().indexOf('登录') >= 0){
alert('请登陆后购买');
// location.href = "/login.html";
location.href = '/login.html';
return;
}
// 购物车按钮的位置
// var $add_x = $('#add_cart').offset().top;
// console.log($add_x);
// var $add_y = $('#add_cart').offset().left;
// console.log($add_y);
// var $to_x = $('#show_count').offset().top;
// console.log($to_x);
// var $to_y = $('#show_count').offset().left;
// console.log($to_y);
count = $('#num_show').val();
$('#show_count').text(count);
//一次添加购物车的数量
count = parseInt($('#num_show').val());
// console.log(num);
//商品id
// id=sessionStorage.getItem("details_id");
// id=$('#buy_btn').prop("href").split("=")[1];
// console.log(id);
//商品图片地址
img_url=$('.goods_detail_pic>img').prop("src");
// console.log(img_url);
title=$('.title').text();
// console.log(title);
num=$('.goods_kucun').text();
// console.log(num);
unit=$('#unit').text().split(":")[1];
// console.log(unit);
//截取字符串http://127.0.0.1: 3000/goods_tails.html?lid=7
herf=$('.herf').prop("href").split(":")[2].substr(5,23);
// console.log($('.herf').prop("href"));
// console.log(herf);
price=$('#gprice').text();
sessionStorage.setItem("price",price);
userid=sessionStorage.getItem("userID");
console.log(userid);
$.ajax({
url:"http://127.0.0.1:3000/addcart",
type:"post",
data:{
lid:lid,
userid:userid,
img_url:($('.goods_detail_pic>img').prop("src")),
title:$('.title').text(),
num:$('.goods_kucun').text(),
unit:$('#unit').text().split(":")[1],
price:$('#gprice').text(),
herf:$('.herf').prop("href").split(":")[2].substr(5,23),
count:parseInt($('#num_show').val())
},
dataType:"json"
})
.then(function(result){
// if(result.code==1)
// console.log(result);
$('#show_count').text(result.code);
alert(result.msg);
})
}
css代码:(goods_details.css商品详情页样式)
/* 商品详情样式 */
.goods_detail_con{
width:80%;
height:75%;
border:1px solid #ededed;
margin:0 auto 20px;
}
.goods_detail_pic{
width:30%;
height:350px;
margin:24px 0 0 24px;
}
.goods_detail_pic img{
width: 80%;
height: 40%;
}
.goods_detail_list{
width:45%px;
height:350px;
margin:24px 24px 0 0;
}
.goods_detail_list h3{
font-size:18px;
line-height:24px;
color:#666;
font-weight:normal;
}
.goods_detail_list p{
color:#666;
font-size: 10px;
line-height:40px;
}
.goods_detail_list .prize_bar{
height:65px;
background-color:#fff5f5;
line-height:65px;
}
.goods_detail_list .prize_bar .show_pirze{
font-size:20px;
color:#ff3e3e;
padding-left:20px;
}
.goods_detail_list .prize_bar .show_pirze em{
font-style:normal;
font-size:24px;
padding-left:10px;
}
.goods_detail_list .prize_bar .show_unit{
padding:0 70px 0 70px;
font-size: 10px;
}
.goods_num{
height:52px;
margin-top:19px;
}
.goods_num .num_name{
width:70px;
height:52px;
font-size: 10px;
line-height:52px;
}
.goods_num .num_add{
width:75px;
height:50px;
border:1px solid #dddddd
}
.goods_num .num_add input{
width:49px;
height:50px;
text-align:center;
line-height:50px;
border:0px;
outline:none;
font-size:14px;
color:#666
}
.goods_num .num_add .add,.goods_num .num_add .reduce{
width:25px;
line-height:25px;
text-align:center;
border-left:1px solid #ddd;
border-bottom:1px solid #ddd;
color:#666;
font-size:14px
}
.goods_num .num_add .reduce{
border-bottom:0px
}
.total{
height:35px;
line-height:35px;
margin-top:25px;
font-size: 10px;
}
.total em{
font-style:normal;
color:#ff3e3e;
font-size:18px
}
.operate_btn{
height:40px;
margin-top:35px;
font-size:0;
position:relative;
}
.operate_btn .buy_btn,.operate_btn .add_cart{
display:inline-block;
width:25%;
height:75%;
border:1px solid #c40000;
font-size:11px;
color:#c40000;
line-height:28px;
text-align:center;
background-color:#ffeded;
}
.operate_btn .add_cart{
background-color:#c40000;
color:#fff;
margin-left:10px;
position:relative;
z-index:10;
}
.add_jump{
width:20px;
height:20px;
background-color:#c40000;
position:absolute;
left:268px;
top:10px;
border-radius:50%;
z-index:9;
display:none;
}
.detail_tab{
height:35px;
border-bottom:1px solid #37ab40
}
.detail_tab li{
height:34px;
line-height:34px;
padding:0 30px;
font-size:12px;
color:#333333;
float:left;
border:1px solid #e8e8e8;
border-bottom:0px;
cursor:pointer;
background-color:#faf8f8
}
.detail_tab li.active{
border-top:2px solid #37ab40;
position:relative;
background-color:#fff;
border-left:1px solid #37ab40;
border-right:1px solid #37ab40;
top:-1px;
height:35px;
}
.tab_content dt{
margin:10px 0 0 5px;
font-size:12px;
color:#044d39
}
.tab_content dd{
line-height:24px;
/* font-size: 12px; */
margin-top:5px;
text-indent: 30px;
}