下面是自己亲自动手编写的代码,和大家一起学习研究。
商品对比调用的JS文件(包含了商品对比框浮动JS):
/*浮动窗口*/ (function(){ var n=10; var obj=document.getElementById("goods-compare"); if(!obj){ return false; } var x=0; window.onscroll=function(){ obj.style.top=(document.body.scrollTop||document.documentElement.scrollTop)+n+'px'; }; window.onresize=function(){ obj.style.top=(document.body.scrollTop||document.documentElement.scrollTop)+n+'px'; }; })(); //添加显示对比框 function addcompare(chk){ $('#goods-compare').fadeIn().show(); var count=$(".compare-box li").length; if (count>2)//这里可以修改对比的数据哦 { alert('产品比较最多选3种哦'); return; } $.ajax({ type: 'post', url: 'ajax.php', data: { 'action':'1', 'gid':chk.gid,//商品ID 'gname':chk.gname,//商品名称 'gtype':chk.gtype//商品类别,类别不同时不能比较 }, cache: false, async: false, success: function(result) { if(result!='') { alert(result); }else{ var url='http://www.lusen.com/product-'+chk.gid+'.html';//设置商品的链接地址 $(".compare-box").append("<li class='division clearfix' id='"+chk.gid+"'><div class='span-3'><a href='"+url+"' target='_blank' title='"+chk.gname+"'>'"+chk.gname+"'</a></div><span onclick=\"removecompare('"+chk.gid+"');\">删除</span></li>") $("#comids").val($(".compare-box li").map(function(){//将对比的所有商品ID,赋值给#comids return $(this).attr('id'); }).get().join(",")); } } }); } //删除对比产品 function removecompare(id) { $.ajax({ type: 'post', url: 'ajax.php', data: { 'action':'2', 'gid':id }, cache: false, success: function(result) { $("#"+id).remove(); $("#comids").val($(".compare-box li").map(function(){ return $(this).attr('id'); }).get().join(",")); } }); } //清空对比产品 function clearcompare() { $.ajax({ type: 'post', url: 'ajax.php', data: { 'action':'3' }, cache: false, success: function(result) { $(".compare-box").html(''); $("#comids").val(''); } }); } //显示对比框 function showcompare() { $.ajax({ type: 'post', url: 'ajax.php', data: { 'action':'4' }, success: function(result) { if(result){ $(".compare-box").append(result); $("#comids").val($(".compare-box li").map(function(){ return $(this).attr('id'); }).get().join(",")); $('#goods-compare').fadeIn().show(); } } }); } //点击关闭对比框 $('.close-gc').click(function(){ $('#goods-compare').fadeOut().hide(); });
商品对比调用Ajax文件
<"商品比较最多选3种"; return; } foreach($arr as $val) { if($val[0]==$_POST['gid']) { echo "该商品已经加入对比框"; return; } } $info=array($_POST['gid'],$_POST['gname'],$_POST['gtype']); $arr[]=$info; $arr_str=serialize($arr); setcookie('gid',$arr_str); }else { $info=array($_POST['gid'],$_POST['gname'],$_POST['gtype']); $arr[]=$info; $arr_str=serialize($arr); setcookie('gid',$arr_str); } }else if($_POST['action']=='2') {//delone $id=$_POST['gid']; $arr_str = $_COOKIE['gid']; $arr=mb_unserialize($arr_str); foreach($arr as $key=>$val) { if($val[0]==$id) { unset ($arr[$key]); } } $arr_str=serialize($arr); setcookie('gid',$arr_str); }else if($_POST['action']=='3') {//delall setcookie('gid',''); setcookie('gtype',''); }else if($_POST['action']=='4') {//showlist if(isset($_COOKIE['gid'])) { $data=''; $arr_str = $_COOKIE['gid']; $arr=mb_unserialize($arr_str); foreach ($arr as $val){ $url="http://www.lusen.com/product-".$val[0].".html"; $data.="<li id='{$val[0]}' class='division clearfix'><div class='span-3'><a href='{$url}' target='_blank' title='{$val[1]}'>{$val[1]}</a></div><span onclick=\"removecompare('{$val[0]}');\">删除</span></li>"; } echo $data; } } ?>
以上就是商品对比功能实现代码,希望大家可以仔细研究,有好的想法大家一起探讨。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 梁汉文2008-EDMONDHITS48[香港]3CD[WAV+CUE]
- 国风大师纯音系列《童丽-[渭城曲]》1CD[WAV分轨][593.5MB]
- 国风大师纯音系列《许菱子《烧筝2》古筝曲集》1CD[WAV分轨][1.3GB]
- 王子异《1996 Part.2》[320K/MP3][18.29MB]
- 群星.1994-宝丽金最倾情3CD【宝丽金】【WAV+CUE】
- 乐俊亨.2024-同泪【CharioticMusic】【FLAC分轨】
- 何耀珊.2006-收获【华纳】【WAV+CUE】
- 梁汉文1994-壹精选[华星][WAV+CUE]
- 洪荣宏1981-歹路不可行·相思雨(飞跃复刻版)[光美][WAV+CUE]
- 旅行团2015-10Day’s[来福胶泥][WAV+CUE]
- 龙千玉.2011-卡将哟【豪记】【WAV+CUE】
- 蔣志光.1993-多情多寂寞(国专)【BMG】【WAV+CUE】
- 陈慧娴.1996-雪映美白96演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 王子异《1996 Part.2》[FLAC/分轨][50.66MB]
- 贵族音乐《古典名曲与轻音乐钢琴 打开宝宝睡眠开关》[320K/MP3][155.91MB]