tv.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. var pageSize=3;
  2. var flag=false;
  3. /**
  4. *初始化、添加监听
  5. */
  6. $(document).ready(function(){
  7. document.onkeydown = function(event) {
  8. var evt = event ? event : (window.event ? window.event : null);
  9. return jumpPage(Event.mapping(evt));
  10. };
  11. document.onirkeypress = function(event) {
  12. var evt = event ? event : (window.event ? window.event : null);
  13. return jumpPage(Event.mapping(evt));
  14. };
  15. document.onsystemevent = function(event) {
  16. var evt = event ? event : (window.event ? window.event : null);
  17. return jumpPage(Event.mapping(evt));
  18. };
  19. getMerchant();
  20. });
  21. function getMerchant(){
  22. mobile.ajaxPost(base+'/merchant/getAll',{},function(json){
  23. if(json.data&&json.data.length>0){
  24. var html=[];
  25. $.each(json.data,function(id,item){
  26. if(id==0){
  27. html.push('<div class="col-xs-4" data-id="'+item.id+'">');
  28. }else{
  29. html.push('<div class="col-xs-4" data-id="'+item.id+'">');
  30. }
  31. html.push('<div class="mui-card">');
  32. html.push('<div class="mui-card-header">');
  33. html.push('<img class="merchant-lazy" data-original="'+imgUrl+item.merchant_img1+'" style="width:100%;min-height:100%;">')
  34. html.push('</div>');
  35. html.push('<div class="mui-card-content">');
  36. html.push('<p class="product-name mui-ellipsis-2">'+item.merchant_name+'</p>');
  37. html.push('<p class="product-price items mt10">');
  38. html.push('<em class="item">'+item.visit_num+'<span class="fs24">人关注</span></em>');
  39. html.push('<span class="mui-btn mui-btn-primary">'+item.merchant_category_name+'</span>');
  40. html.push('</p>');
  41. html.push('</div>');
  42. html.push('</div>');
  43. html.push('</div>');
  44. });
  45. $("#tab0").html(html.join(""));
  46. $("img.merchant-lazy").lazyload({
  47. threshold : 400,
  48. effect: "fadeIn",
  49. placeholder: base+'/static/mobile/image/500.gif',
  50. skip_invisible : true
  51. });
  52. getProduct();
  53. }
  54. });
  55. }
  56. function getProduct(){
  57. mobile.ajaxPost(base+'/product/getAll',{},function(json){
  58. if(json.data&&json.data.length>0){
  59. var html=[];
  60. $.each(json.data,function(id,item){
  61. if(id==0){
  62. html.push('<div class="col-xs-4" data-id="'+item.id+'">');
  63. }else{
  64. html.push('<div class="col-xs-4" data-id="'+item.id+'">');
  65. }
  66. html.push('<div class="mui-card">');
  67. html.push('<div class="mui-card-header">');
  68. html.push('<img class="product-lazy" data-original="'+imgUrl+item.product_img_1+'" style="width:100%;min-height:100%;">')
  69. html.push('</div>');
  70. html.push('<div class="mui-card-content">');
  71. html.push('<p class="product-name mui-ellipsis-2">'+item.product_name+'</p>');
  72. html.push('<p class="product-price items mt10">');
  73. html.push('<em class="item"><b>¥</b>'+item.product_price+'<span class="del-line color3 ml5 fs18">'+item.product_old_price+'元</span></em>');
  74. if(item.time_status==0){
  75. html.push('<span class="mui-btn mui-btn-primary">马上抢</span>');
  76. }else if(item.time_status==1){
  77. html.push('<span class="mui-btn mui-btn-grey">已结束</span>');
  78. }else if(item.time_status==2){
  79. html.push('<span class="mui-btn mui-btn-grey">已抢完</span>');
  80. }else if(item.time_status==3){
  81. html.push('<span class="mui-btn mui-btn-grey">已过期</span>');
  82. }
  83. html.push('</p>');
  84. html.push('</div>');
  85. html.push('</div>');
  86. html.push('</div>');
  87. });
  88. $("#tab1").html(html.join(""));
  89. if(sessionStorage.getItem("tvindex")){
  90. var tabindex = sessionStorage.getItem("tabindex")
  91. $(".tab-item.active").removeClass("active");
  92. $(".tab .tab-item").eq(tabindex).addClass("active");
  93. $("#tab"+tabindex).siblings(".row").hide();
  94. $("#tab"+tabindex).show();
  95. var i = parseInt(sessionStorage.getItem("tvindex"));
  96. var page = parseInt(i/pageSize)+1;
  97. $("#tab"+tabindex+" .col-xs-4.active").removeClass("active");
  98. $("#tab"+tabindex+" .col-xs-4").eq(i).addClass("active");
  99. $("#tab"+tabindex).css("top",-(page-1)*356+10);
  100. showImg(page);
  101. flag=true;
  102. }
  103. $("img.product-lazy").lazyload({
  104. threshold : 400,
  105. effect: "fadeIn",
  106. placeholder: base+'/static/mobile/image/500.gif',
  107. skip_invisible : true
  108. });
  109. }
  110. });
  111. }
  112. function jumpPage(event) {
  113. if (event.code==37 || event.code=="KEY_LEFT")//左
  114. fun_left();
  115. if (event.code==38 || event.code=="KEY_UP")//上
  116. fun_up();
  117. if (event.code==39 || event.code=="KEY_RIGHT" )//右
  118. fun_right();
  119. if (event.code==40 || event.code=="KEY_DOWN" )//下
  120. fun_down();
  121. if (event.code==13 || event.code=="KEY_SELECT")//enter
  122. fun_enter();
  123. if (event.code==640 || event.code=="KEY_BACK")//enter
  124. fun_back();
  125. }
  126. function fun_left(){
  127. var tabindex=$(".tab-item.active").index();
  128. if(flag){
  129. if($("#tab"+tabindex+" .col-xs-4.active").index()==0){
  130. $("#tab"+tabindex+" .col-xs-4.active").removeClass("active");
  131. flag=false;
  132. }
  133. var i = $("#tab"+tabindex+" .col-xs-4.active").index()-1;
  134. var page = parseInt(i/pageSize)+1;
  135. $("#tab"+tabindex+" .col-xs-4.active").removeClass("active");
  136. $("#tab"+tabindex+" .col-xs-4").eq(i).addClass("active");
  137. $("#tab"+tabindex).css("top",-(page-1)*356+10);
  138. }
  139. }
  140. function fun_right(){
  141. var tabindex=$(".tab-item.active").index();
  142. if(flag){
  143. if($("#tab"+tabindex+" .col-xs-4.active").index()==$("#tab"+tabindex+" .col-xs-4").length-1){
  144. return;
  145. }
  146. var i = $("#tab"+tabindex+" .col-xs-4.active").index()+1;
  147. var page = parseInt(i/pageSize)+1;
  148. $("#tab"+tabindex+" .col-xs-4.active").removeClass("active");
  149. $("#tab"+tabindex+" .col-xs-4").eq(i).addClass("active");
  150. $("#tab"+tabindex).css("top",-(page-1)*356+10);
  151. showImg(page);
  152. }else{
  153. if($("#tab"+tabindex+" .col-xs-4").length>0){
  154. flag=true;
  155. $("#tab"+tabindex+" .col-xs-4").eq(0).addClass("active");
  156. }
  157. }
  158. }
  159. function fun_up(){
  160. var tabindex=$(".tab-item.active").index();
  161. if(flag){
  162. if(parseInt($("#tab"+tabindex+" .col-xs-4.active").index()/pageSize)==0){
  163. return;
  164. }
  165. var i = $("#tab"+tabindex+" .col-xs-4.active").index()-3;
  166. var page = parseInt(i/pageSize)+1;
  167. $("#tab"+tabindex+" .col-xs-4.active").removeClass("active");
  168. $("#tab"+tabindex+" .col-xs-4").eq(i).addClass("active");
  169. $("#tab"+tabindex).css("top",-(page-1)*356+10);
  170. }else{
  171. var index = tabindex-1;
  172. if(tabindex!=0){
  173. $(".tab-item.active").removeClass("active");
  174. $(".tab .tab-item").eq(index).addClass("active");
  175. $("#tab"+index).siblings(".row").hide();
  176. $("#tab"+index).show();
  177. }
  178. }
  179. }
  180. function fun_down(){
  181. var tabindex=$(".tab-item.active").index();
  182. if(flag){
  183. var totalPage = parseInt(($("#tab"+tabindex+" .col-xs-4").length-1)/pageSize)+1;
  184. if(parseInt($("#tab"+tabindex+" .col-xs-4.active").index()/pageSize)+1==totalPage){
  185. return;
  186. }
  187. var i = $("#tab"+tabindex+" .col-xs-4.active").index()+pageSize;
  188. var page = parseInt(i/pageSize)+1;
  189. $("#tab"+tabindex+" .col-xs-4.active").removeClass("active");
  190. if(i>$("#tab"+tabindex+" .col-xs-4").length){
  191. $("#tab"+tabindex+" .col-xs-4").eq($("#tab"+tabindex+" .col-xs-4").length-1).addClass("active");
  192. }else{
  193. $("#tab"+tabindex+" .col-xs-4").eq(i).addClass("active");
  194. }
  195. $("#tab"+tabindex).css("top",-(page-1)*356+10);
  196. showImg(page);
  197. }else{
  198. var index = tabindex+1;
  199. if(index!=$(".tab .tab-item").length){
  200. $(".tab-item.active").removeClass("active");
  201. $(".tab .tab-item").eq(index).addClass("active");
  202. $("#tab"+index).siblings(".row").hide();
  203. $("#tab"+index).show();
  204. showImg(1);
  205. }
  206. }
  207. }
  208. function fun_enter(){
  209. if($(".col-xs-4.active").index()!=-1){
  210. var tabindex=$(".tab-item.active").index();
  211. sessionStorage.setItem("tvindex",$(".col-xs-4.active").index());
  212. sessionStorage.setItem("tabindex",tabindex);
  213. if(tabindex==0){
  214. location.href=base+"/tv/merchantView.jsp?id="+$("#tab"+tabindex+" .col-xs-4.active").attr("data-id");
  215. }else{
  216. location.href=base+"/tv/productView.jsp?id="+$("#tab"+tabindex+" .col-xs-4.active").attr("data-id");
  217. }
  218. }
  219. }
  220. function fun_back(){
  221. history.back();
  222. }
  223. function showImg(page){
  224. var tabindex=$(".tab-item.active").index();
  225. if(tabindex==0){
  226. for(var i=0;i<pageSize;i++){
  227. var img = $("#tab0 .col-xs-4").eq(pageSize*page+i).find(".merchant-lazy");
  228. if($(img).attr("src")&&$(img).attr("src").indexOf("500.gif")!=-1){
  229. $(img).attr("src",$(img).attr("data-original"));
  230. $(img).css("display","inline");
  231. }
  232. }
  233. }else{
  234. for(var i=0;i<pageSize;i++){
  235. var img = $("#tab1 .col-xs-4").eq(pageSize*page+i).find(".product-lazy");
  236. if($(img).attr("src")&&$(img).attr("src").indexOf("500.gif")!=-1){
  237. $(img).attr("src",$(img).attr("data-original"));
  238. $(img).css("display","inline");
  239. }
  240. var img = $("#tab1 .col-xs-4").eq(pageSize*(page-1)+i).find(".product-lazy");
  241. if($(img).attr("src")&&$(img).attr("src").indexOf("500.gif")!=-1){
  242. $(img).attr("src",$(img).attr("data-original"));
  243. $(img).css("display","inline");
  244. }
  245. }
  246. }
  247. }