仿京东淘宝商品详情页属性选择js效果
在网上找了好久发现都不符合要求就自己摸索写了一个,用到了linq.js这个linq to js 扩展,不然用纯JS遍历json查询要死人啊
demo:http://123.207.28.46:8086/
效果图:
代码直接拷贝就可以运行:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<style>
/*sku选择样式*/
li {
list-style: none;
margin-right: 10px;
} li label {
cursor: pointer;
} li {
float: left;
line-height: 30px;
} .clear {
clear: both;
}
.AttributeValue {
border: 1px solid #808080;
padding: 5px 10px;
} .choices {
border-color: #e01313
} .disabled {
border: 1px dashed #b1abab;
background-color: #f1f1f1;
} .disabled label {
cursor: not-allowed;
}
</style>
<script src="http://neue.cc/linq.min.js"></script>
<script> var Combination1 = { Id: 1, ProductId: 21, Attributes: ",9,13,19,21,", StockQuantity: 10, OverriddenPrice: 99 };
var Combination2 = { Id: 2, ProductId: 21, Attributes: ",9,14,20,23,", StockQuantity: 10, OverriddenPrice: 199 };
var Combination3 = { Id: 3, ProductId: 21, Attributes: ",10,16,19,25,", StockQuantity: 10, OverriddenPrice: 299 };
var Combination4 = { Id: 4, ProductId: 21, Attributes: ",10,17,20,24,", StockQuantity: 10, OverriddenPrice: 299 };
var Combination5 = { Id: 5, ProductId: 21, Attributes: ",11,17,20,24,", StockQuantity: 10, OverriddenPrice: 299 };
var Combination6 = { Id: 6, ProductId: 21, Attributes: ",12,14,19,22,", StockQuantity: 10, OverriddenPrice: 299 };
//list:来自数据查询出来的商品组合属性json数据
var list = [];
list.push(Combination1);
list.push(Combination2);
list.push(Combination3);
list.push(Combination4);
list.push(Combination5);
list.push(Combination6);
//SKU_TYPET:来自数据库中商品属性json数据
var SKU_TYPET = [{ AttributeId: 7, Attribute: "颜色", AttributeValues: [{ AttributeValueId: 9, AttributeValue: "金色" }, { AttributeValueId: 10, AttributeValue: "黑色" }, { AttributeValueId: 11, AttributeValue: "银色" }, { AttributeValueId: 12, AttributeValue: "红色" }] }, { AttributeId: 8, Attribute: "版本", AttributeValues: [{ AttributeValueId: 13, AttributeValue: "公开版" }, { AttributeValueId: 14, AttributeValue: "原厂延保版" }, { AttributeValueId: 15, AttributeValue: "双网通版" }, { AttributeValueId: 16, AttributeValue: "无线充套装" }, { AttributeValueId: 17, AttributeValue: "AirPods套装" }, { AttributeValueId: 18, AttributeValue: "分期用版" }] }, { AttributeId: 9, Attribute: "内存", AttributeValues: [{ AttributeValueId: 19, AttributeValue: "64G" }, { AttributeValueId: 20, AttributeValue: "256G" }] }, { AttributeId: 10, Attribute: "套装", AttributeValues: [{ AttributeValueId: 21, AttributeValue: "优惠套装1" }, { AttributeValueId: 22, AttributeValue: "优惠套装2" }, { AttributeValueId: 23, AttributeValue: "优惠套装3" }, { AttributeValueId: 24, AttributeValue: "优惠套装4" }, { AttributeValueId: 25, AttributeValue: "优惠套装5" }] }];
$(function () {
function ishas(AttributeValueIds) {
var newlist = list.concat();
var newAttributeValueIds = AttributeValueIds;
for (var i = 0; i < newAttributeValueIds.length; i++) {
newlist = Enumerable.From(newlist).Where(function (x) {
return x.Attributes.indexOf(newAttributeValueIds[i]) > -1;
}).ToArray();
}
if (newlist.length > 0) {
return true;
} else {
return false;
}
}
init(SKU_TYPET);
//init:绑定商品属性数据
function init(SKU_TYPET) {
var SKU_TYPE = "";
$.each(SKU_TYPET, function (index, item) {
SKU_TYPE += '<ul class="SKU_TYPE"> <li sku-type-name="' + item.Attribute + '">' + item.Attribute + ':</li></ul>';
SKU_TYPE += "<ul>";
$.each(item.AttributeValues, function (i, childitem) {
var AttributeValueIdsArry = [];
AttributeValueIdsArry.push("," + childitem.AttributeValueId + ",");
if (!ishas(AttributeValueIdsArry)) {
SKU_TYPE += '<li class="AttributeValue disabled" data-AttributeId="' + item.AttributeId + '" data-AttributeValueId="' + childitem.AttributeValueId + '"><label>' + childitem.AttributeValue + '</label></li>';
} else {
SKU_TYPE += '<li class="AttributeValue available" data-AttributeId="' + item.AttributeId + '" data-AttributeValueId="' + childitem.AttributeValueId + '"><label>' + childitem.AttributeValue + '</label></li>';
} });
SKU_TYPE += "</ul>";
SKU_TYPE += '<div class="clear"></div>';
}); $("#show").html(SKU_TYPE);
}
//Attribute:已选择的商品属性集合[{ AttributeId: 7, AttributeValueId: 9 }]
var Attribute = [];
//取消已选择属性点击事件
$("body").on("click", ".choices", function (event) {
$(this).removeClass("choices");
$(this).addClass("available");
var AttributeId = $(this).attr("data-AttributeId");
var AttributeValueId = $(this).attr("data-AttributeValueId");
//从Attribute删除已选择属性
var itemIndex = 0;
$.each(Attribute, function (index, item) {
if (item.AttributeId == parseInt(AttributeId)) {
itemIndex = index;
}
});
Attribute.splice(itemIndex, 1);
//重新绑定
$.each(SKU_TYPET, function (index, item) {
$.each(item.AttributeValues, function (i, childitem) {
var newAttributeValueIds = Enumerable.From(Attribute).Select(function (x) { return x.AttributeValueId }).ToArray();
var AttributeValueIdsArry = [];
$.each(Attribute, function (i, it) {
AttributeValueIdsArry.push("," + it.AttributeValueId + ",");
});
AttributeValueIdsArry.push("," + childitem.AttributeValueId + ",");
if (!ishas(AttributeValueIdsArry)) {
$("[data-AttributeValueId='" + childitem.AttributeValueId + "']").addClass("disabled");
$("[data-AttributeValueId='" + childitem.AttributeValueId + "']").removeClass("available"); } else {
$("[data-AttributeValueId='" + childitem.AttributeValueId + "']").removeClass("disabled");
if (!$("[data-AttributeValueId='" + childitem.AttributeValueId + "']").hasClass("choices")) {
$("[data-AttributeValueId='" + childitem.AttributeValueId + "']").addClass("available");
}
}
}); }); });
//选择属性点击事件
$("body").on("click", ".available", function () {
var AttributeId = $(this).attr("data-AttributeId");
var AttributeValueId = $(this).attr("data-AttributeValueId");
//先判断Attribute是否存在该属性,
if (Enumerable.From(Attribute).ToLookup("$.AttributeId").Contains(parseInt(AttributeId))) {
$.each(Attribute, function (index, item) {
//存在更新其值
if (item.AttributeId == parseInt(AttributeId)) {
item.AttributeValueId = parseInt(AttributeValueId);
}
});
}
//不存在则添加
else {
Attribute.push({ AttributeId: parseInt(AttributeId), AttributeValueId: parseInt(AttributeValueId) });
}
//循环每一项属性值并查询
$.each(SKU_TYPET, function (index, item) {
$.each(item.AttributeValues, function (i, childitem) {
var newAttributeValueIds = Enumerable.From(Attribute).Select(function (x) { return x.AttributeValueId }).ToArray();
var AttributeValueIdsArry = [];
$.each(Attribute, function (i, it) {
AttributeValueIdsArry.push("," + it.AttributeValueId + ",");
});
AttributeValueIdsArry.push("," + childitem.AttributeValueId + ",");
if (!ishas(AttributeValueIdsArry)) {
$("[data-AttributeValueId='" + childitem.AttributeValueId + "']").addClass("disabled");
$("[data-AttributeValueId='" + childitem.AttributeValueId + "']").removeClass("available"); } else {
$("[data-AttributeValueId='" + childitem.AttributeValueId + "']").removeClass("disabled");
if (!$("[data-AttributeValueId='" + childitem.AttributeValueId + "']").hasClass("choices")) {
$("[data-AttributeValueId='" + childitem.AttributeValueId + "']").addClass("available");
} }
}); });
$(this).removeClass("available");
$(this).addClass("choices"); });
$("#Button1").click(function () {
if (Attribute.length != SKU_TYPET.length) {
$("#show").css("border", "2px solid #ff0000");
alert("请选择您要的商品信息");
} else {
$("#show").css("border", "0");
alert("你已选择:"+JSON.stringify(Attribute));
}
})
});
</script>
</head>
<body>
<div id="show" style="width:100%;">
</div>
<input id="Button1" type="button" value="购买" style="margin-left:98px;margin-top:20px" /> </body>
</html>
仿京东淘宝商品详情页属性选择js效果的更多相关文章
- vue实现淘宝商品详情页属性选择功能
方法一是自己想出来的,方法二来自忘记哪里看到的了 不知道是不是你要的效果: 方法一:利用input[type="radio"] css代码: input { display: no ...
- 仿淘宝商品详情页上拉弹出新ViewController
新项目就要开始做了,里面有购物那块,就试着先把淘宝商品详情页的效果做了一下. 1.需求 1.第一次上拉时,A视图拉到一定距离将视图B从底部弹出,A视图也向上 2.显示B视图时下拉时,有刷新效果,之后将 ...
- iOS app url scheme跳转到淘宝商品详情页 唤醒app
最近涉及的一个业务,在app内的一个广告,点击打开webView,加载的是一个淘宝商品详情页,效果是打开该webView自动跳转至淘宝对应的页面,同时在自己的app仍然加载页面,点击评论等也同样能跳转 ...
- android仿京东、淘宝商品详情页上拉查看详情
话不多说,直接上干货,基本就是一个scrollview中嵌套两个scrollview或者webview;关键点事处理好子scrollview和父scrollview的触摸.滑动事件已达到想要的效果.大 ...
- 第十二篇、OC_仿淘宝商品详情页的翻页
// // GFBProductViewController.m // elmsc // // Created by MAC on 2016/11/26. // Copyright © 2016年 G ...
- Vue实现仿淘宝商品详情属性选择的功能
Vue实现仿淘宝商品详情属性选择的功能 先看下效果图:(同个属性内部单选,属性与属性之间可以多选) 主要实现过程: 所使用到的数据类型是(一个大数组里面嵌套了另一个数组)具体格式如下: attrA ...
- Android 仿电商app商品详情页按钮浮动效果
1.效果图如下: 这效果用户体验还是很酷炫,今天我们就来讲解如何实现这个效果. 2.分析 为了方便理解,作图分析 如图所示,整个页面分为四个部分: 1.悬浮内容,floatView 2.顶部内容,he ...
- 淘宝商品html--网页结构
淘宝商品html--网页结构 本篇爬虫紧接上一篇关于 泸州老窖 的爬虫随笔: import re import json def get_space_end(level): return ' ' * ...
- Android之仿京东淘宝的自动无限轮播控件
在App的开发中,很多的时候都需要实现类似京东淘宝一样的自动无限轮播的广告栏,所以就自己写了一个,下面是我自定义控件的思路和过程. 一.自定义控件属性 新建自定义控件SliderLayout继承于Re ...
随机推荐
- 2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...
- .net打印
<input type="button" onclick="javascript:printit()"></input>//打印整个ht ...
- Android 进价5_自定义广播 通过广播更新ListView的适配器 下载管理
1.在处理下载管理时,服务在后台运行,下载完成后要更新listview列表的按钮,将“下载”改成“打开”这样一个功能. 在Activity里面写一个静态内部类,继承广播.其中属性text_button ...
- 【Linux资源管理】使用sar进行性能分析
sar可用于监控Linux系统性能,帮助我们分析性能瓶颈.sar工具的使用方式为”sar [选项] intervar [count]”,其中interval为统计信息采样时间,count为采样次数. ...
- Oracle子查询之高级子查询
Oracle 高级子查询 高级子查询相对于简单子查询来说,返回的数据行不再是一列,而是多列数据. 1,多列子查询 主查询与子查询返回的多个列进行比较 查询与141号或174号员工的manager_id ...
- iOS视频倒放
iOS视频倒放 视频的倒放就是视频从后往前播放,这个只适应于视频图像,对声音来说倒放只是噪音,没什么意义,所以倒放的时候声音都是去除的. 倒放实现 一般对H264编码的视频进行解码,都是从头至尾进行的 ...
- 初学pygame
#Author:cljimport pygamepygame.display.set_mode((640,480),0,32)#设置窗口大小 返回的也是一个surface对象,resolution可以 ...
- 将jquery.qqFace.js表情转换成微信的字符码
jquery.qqFace.js使用方法 引用 <script src="~/Content/qqFace/js/jquery.qqFace.js?v=3"></ ...
- 转:Java并发集合
引自:http://ifeve.com/concurrent-collections-1/ 并发集合(一)引言 声明:本文是< Java 7 Concurrency Cookbook>的第 ...
- font(字体)所使用的属性
1.font-weight:normal blod bolder lighter 100-900之间 400=normal p:first-child{ padding-top: 50px; pos ...