comparison.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. Array.prototype.in_array = function(e){
  2. for(i=0;i<this.length && this[i]!=e;i++);
  3. return !(i==this.length);
  4. }
  5. function remove_all(){
  6. $("#checkbox :checkbox").attr("checked",false);
  7. $("#relation_text").html("");
  8. $.get(ajax_url, {m:'yp', c:'index', a:'pk', action:'remove', catid:catid, random:Math.random()});
  9. $("#relation").val("");
  10. c_sum();
  11. };
  12. $("#checkbox :checkbox").click(function (){
  13. var q = $(this),
  14. num = 4,
  15. id = q.val(),
  16. title = q.attr('title'),
  17. img = q.attr('img'),
  18. sid = 'v1'+id,
  19. relation_ids = $('#relation').val(),
  20. relation_ids = relation_ids.replace(/(^_*)|(_*$)/g, ""),
  21. r_arr = relation_ids.split('-');
  22. if($("#comparison").css("display")=="none"){
  23. $("#comparison").fadeIn("slow");
  24. }
  25. if(q.attr("checked")==false){
  26. $('#'+sid).remove();
  27. $.get(ajax_url, {m:'yp', c:'index', a:'pk', action:'reduce', id:id, catid:catid, random:Math.random()});
  28. if(relation_ids !='' ) {
  29. var newrelation_ids = '';
  30. $.each(r_arr, function(i, n){
  31. if(n!=id) {
  32. if(i==0) {
  33. newrelation_ids = n;
  34. } else {
  35. newrelation_ids = newrelation_ids+'-'+n;
  36. }
  37. }
  38. });
  39. $('#relation').val(newrelation_ids);
  40. }
  41. c_sum();
  42. }else{
  43. if(r_arr.in_array(id)){
  44. q.checked=false;
  45. alert("抱歉,已经选择了该信息");
  46. return false;
  47. }
  48. if(r_arr.length>=num){
  49. q.checked=false;
  50. alert("抱歉,您只能选择"+r_arr.length+"款商品对比");
  51. return false;
  52. }else{
  53. $.get(ajax_url, {m:'yp', c:'index', a:'pk', action:'add', id:id, title:title, thumb:img, catid:catid, random:Math.random()});
  54. var str = "<li style='display:none' id='"+sid+"'><img src="+img+" height='45'/><p>"+title+"</p><a href='javascript:;' class='close' onclick=\"remove_relation('"+sid+"',"+id+")\">X</a></li>";
  55. $('#relation_text').append(str);
  56. $("#"+sid).fadeIn("slow");
  57. if(relation_ids =='' ){
  58. $('#relation').val(id);
  59. }else{
  60. relation_ids = relation_ids+'-'+id;
  61. $('#relation').val(relation_ids);
  62. }
  63. c_sum();
  64. $('#'+sid+' img').LoadImage(true, 120, 60,'statics/images/s_nopic.gif');
  65. }
  66. }
  67. });
  68. function remove_relation(sid,id){
  69. $('#'+sid).remove();
  70. $.get(ajax_url, {m:'yp', c:'index', a:'pk', action:'reduce', id:id, catid:catid, random:Math.random()});
  71. $("#c_"+id).attr("checked",false);
  72. $("#c_h_"+id).attr("checked",false);
  73. var relation_ids = $('#relation').val(),
  74. r_arr = relation_ids.split('-'),
  75. newrelation_ids = '';
  76. if(relation_ids!=''){
  77. $.each(r_arr, function(i, n){
  78. if(n!=id) {
  79. if(i==0) {
  80. newrelation_ids = n;
  81. } else {
  82. newrelation_ids = newrelation_ids+'-'+n;
  83. }
  84. }
  85. });
  86. $('#relation').val(newrelation_ids);
  87. }
  88. c_sum();
  89. return false;
  90. }
  91. function comp_btn() {
  92. var relation_ids = $('#relation').val(),
  93. _rel = relation_ids.replace(/(^_*)|(_*$)/g, "");
  94. if($("#comp_num").text()>1){
  95. //alert(_rel);
  96. window.open(ajax_url+"?m=yp&c=index&a=pk&catid="+catid+"&modelid="+modelid+"&pk="+_rel);
  97. }else{
  98. alert("请选择2~4个需要对比的商品!");
  99. }
  100. };
  101. function c_sum(){
  102. var relation_ids = $('#relation').val(),
  103. relation_ids = relation_ids.replace(/(^_*)|(_*$)/g, ""),
  104. r_arr = relation_ids.split('-');
  105. if(relation_ids){
  106. $("#comp_num").text(r_arr.length);
  107. }else{
  108. $("#comp_num").text('0');
  109. }
  110. }
  111. //浮动
  112. (function($) {
  113. $.fn.extend({
  114. "followDiv": function(str) {
  115. var _self = this,
  116. _h = _self.height() ;
  117. var pos;
  118. switch (str) {
  119. case "right":
  120. pos = { "right": "0px", "top": "27px" };
  121. break;
  122. }
  123. topIE6 = parseInt(pos.top) + $(window).scrollTop();
  124. _self.animate({'top':topIE6},500);
  125. /*FF和IE7可以通过position:fixed来定位,*/
  126. //_self.css({ "position": "fixed", "z-index": "9999" }).css(pos);
  127. /*ie6需要动态设置距顶端高度top.*/
  128. // if ($.browser.msie && $.browser.version == 6) {
  129. //_self.css('position', 'absolute');
  130. $(window).scroll(function() {
  131. topIE6 = parseInt(pos.top) + $(window).scrollTop();
  132. //_self.css('top', topIE6);
  133. $(_self).stop().animate({top:topIE6},300)
  134. });
  135. // }
  136. return _self; //返回this,使方法可链。
  137. }
  138. });
  139. })(jQuery);
  140. $("body").append('<div id="comparison"><div class="title"><span id="comp_num">1</span>/4对比栏<a href="javascript:;" onclick="$(this).parent().parent().hide()" class="colse">X</a></div><input type="hidden" id="relation" value="" /><ul id="relation_text"></ul><center><a href="javascript:void(0);" onclick="comp_btn()" id="comp_btn">开始对比</a><br><a href="javascript:;" class="remove_all" onclick="remove_all();">清空对比栏</a></center></div>');
  141. $("#comparison").followDiv("right");