首页 > 其他分享 >html前端页面多规格商品sku选择

html前端页面多规格商品sku选择

时间:2023-09-18 12:01:13浏览次数:38  
标签:sku function attr text ids html var 页面


<style> body {   background-color: palegoldenrod;   position: relative; } footer {   border: 1px solid red;   height: 50px;   position: fixed;   bottom: 0;   left: 0;   width: 100%; } .btn {   padding: 0 15px;   height: 35px;   line-height: 35px;   border: 1px solid #057778;   font-size: 16px;   -webkit-border-radius: 5px;   -moz-border-radius: 5px;   border-radius: 5px;   background-color: #057778;   color: #FFFFFF; } .big_btn{   width: 98%;   margin: 20px 1% 0 ; } .clearfix:after {content: "";display: block;visibility: hidden;height: 0;clear: both;} .clearfix {zoom: 1;} em,i{font-style:normal;} /*强制换行*/ .noline {   word-wrap: break-word;   word-break: break-all; } /*不换行,以省略号结束*/ .line {   overflow: hidden;   text-overflow: ellipsis;   white-space: nowrap;   word-break: keep-all; } /*flex盒子*/ .flex_box {   display: -webkit-box;   display: -webkit-flex;   display: -ms-flexbox;   display: flex;   align-items: center;   -webkit-align-items: center; } .flex1 {   -webkit-box-flex: 1;   -webkit-flex: 1;   -ms-flex: 1;   flex: 1; } /*------------------------------------*/ .sku_box{   background-color: #FFFFFF;   width: 100%;   position: fixed;   bottom: 0;   left: 0;   padding: 10px 15px 15px 15px;   font-size: 16px;   z-index: 100;   /*display: none*/ } .sku_close{   position: absolute;   right: 10px;   top: 10px;   width: 30px;   height: 30px;   font-size: 32px;   color: #CCCCCC; } .sku_hd{   border-bottom: 1px solid #ddd;   padding: 10px 0;   font-size: 14px;   color: #898989; } .img_box{   width: 80px;   height: 80px;   margin-right: 10px; } .img_box img{   width: 100%;   height: 100%; } .sku_hd li{   line-height: 1.8; } .sku_hd li span{   margin-right: 10px; } .sku_hd li label{   margin-right: 10px; } .sku_bd label,.sku_ft label{   font-size: 14px;   color: #898989;   line-height: 30px; } .sku_item{   border-bottom: 1px solid #ddd;   padding: 10px 0; } .sku_list li{   height: 35px;   line-height: 35px;   color: #333333;   background-color: #EaEaEa;   font-size: 16px;   display: inline-block;   padding: 0 15px;   margin-top: 10px;   margin-right: 10px;   -webkit-border-radius: 5px;   -moz-border-radius: 5px;   border-radius: 5px;   font-size: 14px; } body .active{   background-color: #057778;   color: #FFFFFF; } body .sku_none{   color: #ccc; } .sku_ft{   padding: 10px 0; } .count{   float: right; } .count li{   float: right;   text-align: center; } .minus,.plus{   width: 20px;   border: 1px solid #000;   color: #000000;   -webkit-border-radius: 3px;   -moz-border-radius: 3px;   border-radius: 3px;   height: 20px ;   margin-top: 5px;   font-size: 20px;   line-height: 18px; } /*减号*/ .minus_not,.plus_not{   border: 1px solid #CCCCCC;   color: #CCCCCC; } .count li:nth-of-type(2){   width: 60px;   padding: 0 5px;   line-height: 30px; } </style>   <body>     <div class="sku_box"> <div class="sku_close">×</div> <div class="sku_hd flex_box"> <div class="img_box"> <img src="" alt="这是商品图片"/> </div> <ul class="flex1"> <li style="color: #057778;">¥<em id="proPrice">113</em></li> <li> <label>库存:</label> <span id="proStock">7</span></li> <li> <label>规格:</label> <span id="sku_select"> <!--<span>超纤革白红</span><span>36</span>--> </span> </li> </ul> </div> <div class="sku_bd"> <!--<div class="sku_item"> <label>颜色</label> <ul class="sku_list" id="sku_list1"> <li class="active">超纤革白红</li> <li>网布白红</li> <li>超纤革鸳鸯色</li> <li>超纤革白红</li> <li>网布白红</li> </ul> </div>--> </div> <div class="sku_ft "> <div class="clearfix"> <label>数量</label> <ul class="count clearfix"> <!--加号--> <li class="plus" id="plus">+</li> <li id="shop_num">1</li> <!--减号--> <li class="minus minus_not" id="minus">-</li> </ul> </div> <button type="button" class="btn big_btn" id="inputShopCart">加入购物车</button> </div> </div> <footer> <button type="button" class="btn" id="btn1">加入购物车</button> </footer>   </body> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script> var skuList = [{ SkuId: 51, SkuName: "重量", ValueList: [ { ValueId: 110, ValueName: "15g" }, { ValueId: 109, ValueName: "25g" }, ] }, { SkuId: 10243628, SkuName: "大小", ValueList: [ { ValueId: 10243731, ValueName: "x" }, { ValueId: 10243732, ValueName: "m" }, { ValueId: 10243733, ValueName: "l" }, ] }, { SkuId: 1001, SkuName: "颜色", ValueList: [ { ValueId: 100101, ValueName: "黄色" }, { ValueId: 100102, ValueName: "绿色" }, { ValueId: 100103, ValueName: "蓝色" }, ] } ]; var skuSelectList = [ { Sku: "61_110_10243731_100101", Price: 1212.00, "Stock": 12 }, { Sku: "61_110_10243732_100102", Price: 21.00, "Stock": 9 }, { Sku: "61_110_10243733_100103", Price: 121.00, "Stock": 1 }, { Sku: "61_109_10243731_100101", Price: 212.00, "Stock": 5 }, { Sku: "61_109_10243732_100102", Price: 1.00, "Stock": 8 }, { Sku: "61_109_10243733_100103", Price: 12.00, "Stock": 7 } ]; //商品ID var pid = 61; $(function() { skuInit(); $("#btn1").on("click", function() { $(".sku_box").show(); }) $(".sku_close").on("click",function(){ $(".sku_box").hide() }) priceAndStock(0) $(".sku_list").on("click", "li", function() { if($(this).hasClass('sku_none')) { return; //被锁定了 } $(this).toggleClass("active"); if($(this).hasClass('active')) { $(this).siblings("li").removeClass("active"); } //将选择的选项的valueID组成一个新的数组 var select_ids = _getSelAttrId(); //获取选择了规格的父元素,这是一个集合 var $_sel_goods_attr = $('li.active').parents('.sku_list'); // step 1 var all_ids = filterAttrs(select_ids); //获取未选择的 var $other_notsel_attr = $('.sku_list').not($_sel_goods_attr); // //设置为选择属性中的不可选节点 $other_notsel_attr.each(function() { set_block($(this), all_ids); }); //设置已选节点的同级节点是否可选 $_sel_goods_attr.each(function() { update_2($(this)); }); showSelectSku(); if($(".sku_item").length == $("#sku_select .shop_sku").length) { var selectSku = getSelectSku(); priceAndStock(selectSku) }else{ priceAndStock(0) } checkShopNum() }) //减号 var num = 1 $("#minus").on("click", function() { num-- $("#shop_num").text(num--) checkShopNum() }) //加号 $("#plus").on("click", function() { num++ $("#shop_num").text(num); checkShopNum() }) //加入购物车 $("#inputShopCart").on("click", function() { if($(".sku_item").length != $("#sku_select .shop_sku").length) { alert("请选择商品规格") return false; } //skuID var getSelectSkus = getSelectSku(); var num = parseInt($("#shop_num").text()); alert("你选择的skuId为:"+getSelectSkus +",数量为:"+num) }) }) //获取最后选择的规格 function getSelectSku(){ var result; result = pid; $("#sku_select .shop_sku").each(function(k, v) { result += "_" + $(v).attr("valueid"); }) return result; } //判断库存量与价格 function priceAndStock(n) { var maxStock = 0; var minprice = skuSelectList[0].Price; for(var i = 0; i < skuSelectList.length; i++) { maxStock += skuSelectList[i].Stock; minprice = minprice < skuSelectList[i].Price ? minprice : skuSelectList[i].Price //console.log(n) if(skuSelectList[i].Sku == n) { $("#proStock").text(skuSelectList[i].Stock); $("#proPrice").text(skuSelectList[i].Price) return false; } else { $("#proStock").text(maxStock); $("#proPrice").text(minprice); } } } function skuInit() { if(skuList.length == 0) { return false; } //生成规格选择列 $(".sku_bd").empty(); for(var i = 0; i < skuList.length; i++) { var skuItem = $('<div class="sku_item"></div>') var label = $('<label skuId="' + skuList[i].SkuId + '">' + skuList[i].SkuName + '</label>') var ulList = $('<ul class="sku_list" id="sku_list' + i + 1 + '">') var d = skuList[i].ValueList for(var j = 0; j < d.length; j++) { var lis = $('<li valueId="' + d[j].ValueId + '">' + d[j].ValueName + '</li>') ulList.append(lis); } skuItem.append(label); skuItem.append(ulList); $(".sku_bd").append(skuItem); } } //获取所有包含指定节点的路线 function filterProduct(ids) { var result = []; $(skuSelectList).each(function(k, v) { _attr = '_' + v.Sku + '_'; _all_ids_in = true; for(k in ids) { if(_attr.indexOf('_' + ids[k] + '_') == -1) { _all_ids_in = false; break; } } if(_all_ids_in) { result.push(v); } }); return result; } //获取 经过已选节点 所有线路上的全部节点 // 根据已经选择得属性值,得到余下还能选择的属性值 function filterAttrs(ids) { var products = filterProduct(ids); var result = []; $(products).each(function(k, v) { result = result.concat(v['Sku'].split('_')); }); return result; } //已选择的节点数组 function _getSelAttrId() { var list = []; $('.sku_list li.active').each(function() { list.push($(this).attr('valueId')); }); return list; } function update_2($goods_attr) { // 若该属性值 $li 是未选中状态的话,设置同级的其他属性是否可选 var select_ids = _getSelAttrId(); var $li = $goods_attr.find('li.active'); var select_ids2 = del_array_val(select_ids, $li.attr('valueId')); var all_ids = filterAttrs(select_ids2); set_block($goods_attr, all_ids); } function set_block($goods_attr, all_ids) { //根据 $goods_attr下的所有节点是否在可选节点中(all_ids) 来设置可选状态 $goods_attr.find('li').each(function(k2, li2) { if($.inArray($(li2).attr('valueId'), all_ids) == -1) { $(li2).addClass('sku_none'); } else { $(li2).removeClass('sku_none'); } }); } function del_array_val(arr, val) { //去除 数组 arr中的 val ,返回一个新数组 var a = []; for(k in arr) { if(arr[k] != val) { a.push(arr[k]); } } return a; } //生成显示的规格数 function showSelectSku() { $("#sku_select").empty(); $(".sku_list").each(function(i, item) { var m = $(item).find(".active") var sp = $('<span></span>'); sp.attr("valueId", m.attr("valueId")) sp.text(m.text()) if(m.text().length != 0) { sp.addClass("shop_sku") } $("#sku_select").append(sp) }) } //判断选择库存量的值 function checkShopNum() { //判断选择数量是否大于一 if(parseInt($("#shop_num").text()) <= 1) { $("#shop_num").text("1") $("#minus").addClass("minus_not") } else { $("#minus").removeClass("minus_not") } //判断选择数量是否大于库存量 if(parseInt($("#shop_num").text()) >= parseInt($("#proStock").text())) { $("#shop_num").text($("#proStock").text()); $("#plus").addClass("plus_not"); } else { $("#plus").removeClass("plus_not"); } } </script>

 

原文地址:https://www.cnblogs.com/qqing/p/7458625.html

演示地址:http://jsrun.net/NKiKp

标签:sku,function,attr,text,ids,html,var,页面
From: https://www.cnblogs.com/shemmor/p/17711516.html

相关文章

  • 当页面中文本不允许选择时,使文本框中文本可以选择的js代码
    <bodyonselectstart="returnoSelect(event.srcElement);"><scriptlanguage="javascript">functionoSelect(obj){if(obj.type!='text')returnfalse;}</script><inputtype="text"name=&quo......
  • 按下按钮后页面的滚动条向下(或向上)滑动,松开之后便停留在当前位置的效果如何实现?
    网友问题?客户要求做一个框架,按下小框架页面中的“上翻”或者“下翻”按钮后,大框架页面便随之向上(或向下)滚动,松开按钮滚动停止,并停留在当前位置,如何解决?解决方案如下:-----------------mm.htm:-----------------<framesetrows="*,20%"><framename="main"src='liu.htm'target="fo......
  • 页面输出太多会严重影响web程序的性能
    我有这样一个小程序:asp+sqlserver2000。数据量增加的很快,最近发现它的性能非常差,每次打开都需要十几秒,甚至几十秒,因为我的程序分页用的是我自己的分页程序:难道这个分页程序有问题,但是其他地方用到它没有感觉到慢呀,我没事就琢磨他,到网上查资料,结果没有具体查到说到我......
  • html5 的 webScoket 和 C# 建立Socket连接
    html5的webScoket和C#建立Socket连接最近使用的web项目中,需要服务器直接触发前端显示效果。所以研究了一下websocket:名词解释:WebSocketWebSocket协议是一种双向通信协议,它建立在TCP之上,同http一样通过TCP来传输数据,但是它和http最大的不同有两点:1.WebSocket是一种双向......
  • TienChin 渠道管理-渠道页面完善
    最后附上渠道管理的数据installSQL语句:INSERTINTOTienChin.tienchin_channel(channel_id,channel_name,status,remark,type,create_by,update_by,create_time,update_time,del_flag)VALUES(3,'小红书渠道',1,'小红书渠道',1,'qudao','qu......
  • JAVA 实现登录页面生成验证码
    1importjavax.swing.*;2importjava.awt.Color;3importjava.awt.Font;4importjava.awt.Graphics;5importjava.awt.event.ActionEvent;6importjava.awt.event.ActionListener;7importjava.util.Random;89publicclassLoginFrameext......
  • Java swing 实现QQ登录注册页面
    代码如下1packagecom.lty;2importjavax.swing.*;3importjava.awt.*;4importjava.awt.event.ActionEvent;5importjava.awt.event.ActionListener;6importjava.util.Random;78publicclassQQLoginPage{9privateJFrameframe;10......
  • Vue js 框下制作登录页面的新方法
    ......
  • 19 HTML总结
    ......
  • HTML+CSS学习笔记
    HTML骨架:(VSCode可以快速生成骨架:在HTML文件(.html)中,!(英文)配合Enter/Tab键)html:整个网页head:网页头部,用来存放给浏览器看的信息,例如CSStitle:网页标题body:网页主体,用来存放给用户看的信息,例如图片、文字 常用标签:1、标题标签:(文字加粗、字号逐渐......