jquery兑现产品比较器
jquery实现产品比较器
这两天在搞一个产品比较的选择器功能,就像很多电商页面上对两个或者两个以上的产品的一些属性进行比较,从中挑选一个更适合我们的产品的这样的一个玩意,效果图如下:
当我们点击红叉或者清除所有品种的时候会将比较栏中的产品清除掉,当点击对比所选品种时就会向后台发送请求然后返回一些数据(一般都是所选产品共有的一些属性)给我们,当点击收起时这个比较栏就会缩小成如下形式:
这时候点击展开的时候就会回到原来的样子。
这个比较器使用了jquery的cookie插件是为了能在浏览器浏览切换过程中不会将我们已选产品删除。代码中的cookie过期时间设置为浏览器浏览期间,当关闭了浏览器之后我们的cookie就消失了。对于代码中很多封杀掉的功能你们可以自己取消注释进行测试。完整的可以运行的代码在附件里,包括css和图片,其中有美工的功劳。下载下来解压双击compartor.html即可。
关键代码如下:
/*添加品种到比较栏*/ function AddProduct(productID, productName, imgSrc, link) { var html = ""; html += '<li id="' + productID + '">'; html += '<a value="' + productID + '" class="removeProduct" ></a >'; //html += '<img src="' + imgSrc + '" class="thumbnail"/>'; html += '<a href="' + link.replace(".do",".do?") + '" title="查看品种详情">' + productName + '</a>'; html += '</li>'; jQuery("#CompareProductList ul").prepend(html); jQuery("a.removeProduct").each(function() { jQuery(this).click(function() { productCount--; if (productCount < 0) { productCount = 0; } jQuery("#ShowProductCount").text("品种总数:" + productCount); var $product = jQuery("#" + jQuery(this).attr("value"), "#CompareProductList ul"); $product.slideUp("fast", function() { $product.remove()}); if (productCount == 0) { jQuery("#CompareProductList").css("height", ""); jQuery("#CompareProductList ul").css("margin-top", "0px"); } var oldProductID = $product.attr("id"); var oldProductName = jQuery("a", $product).not(".removeProduct").text(); var oldImgSrc = jQuery("img", $product).attr("src"); var oldLink = jQuery("a", $product).not(".removeProduct").attr("href"); RemoveProductFromCookie(oldProductID, oldProductName, oldImgSrc, oldLink); SetControlButton(); if (productCount <= pageSize) { jQuery("#CompareProductList ul").animate({ marginTop: 0 }, 'normal'); } }); return false; }); productCount++; jQuery("#ShowProductCount").text("品种总数:" + productCount); SetControlButton(); jQuery("#CompareBox").show("normal"); jQuery("#CompareHideBox").hide("fast"); checkHeight(); };附件下载地址为:http://download.csdn.net/detail/uohzoaix/4483246