猎豆优选官网

home.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. var sex = '';
  2. var bannerData = [];
  3. var pageNum = 1;
  4. var maxPage = 1;
  5. var loadingFlag = false;
  6. var meiriData = [];//每日精选
  7. var asideData = [];//banner左侧列表栏
  8. var navListData = [];
  9. var category_id = 13;
  10. //点击刷新页面
  11. function reload(){
  12. window.location.href = "../home/index.html";
  13. $(window).scrollTop(0);
  14. }
  15. //向下滑动 导航栏固定
  16. $().ready(function () {
  17. $(window).scroll(function(){
  18. if($(window).scrollTop()>400){
  19. $('.head_item_nav').css({
  20. "display": "block",
  21. "position":"fixed",
  22. "top":"46px"
  23. })
  24. }else{
  25. $('.head_item_nav').css('display','none')
  26. }
  27. if($(window).scrollTop()>= $(document).height()-$(window).height() - 318){
  28. //到达底部加载数据
  29. if(loadingFlag){
  30. pageNum ++ ;
  31. selected(pageNum,category_id);
  32. }
  33. }
  34. });
  35. initData();
  36. navlistData();
  37. ieFontColor();
  38. })
  39. function initData () {
  40. flashSale();//限时抢购
  41. gatBannerData();
  42. asideListData();
  43. initHtml();
  44. selected(pageNum,category_id);
  45. }
  46. //ie浏览器
  47. function ieFontColor () {
  48. if(!!window.ActiveXObject || "ActiveXObject" in window){
  49. $(".container .homeList .homeListRight .advertisement ul li .img_right div:nth-of-type(2) span").css({"color":"#FF0000","background":"#ffffff"})
  50. $(".container .homeList .homeListRight .advertisement ul li .img_right div:nth-of-type(2) span").find("em").css({"background":"#ffffff"})
  51. $(".container .homeList .homeListRight .advertisement ul li:nth-of-type(2) .img_right div:nth-of-type(2) span").css({"color":"#CC00FF","background":"#ffffff"})
  52. $(".activity_title .activity_title_left span").css({"color":"#CC1C0E","background":"#ffffff"})
  53. $(".Recommend .activity_title .activity_title_left span").css({"color":"#5A6CFF","background":"#ffffff"})
  54. }
  55. }
  56. function initHtml () {
  57. sex = 0;
  58. $(".flash_sale .activity_title .activity_title_left i").css({
  59. 'background':'url(https://kx-youhuiquan.oss-cn-beijing.aliyuncs.com/lieodu_gw/xianshiqianggou.png) no-repeat',
  60. 'background-size':'100%'
  61. })
  62. }
  63. //轮播操作
  64. function swiperEvent () {
  65. var mySwiper = new Swiper ('.swiper-container', {
  66. loop: false,
  67. centeredSlides: true,
  68. autoplay: {
  69. delay: 3000,
  70. disableOnInteraction: false//用户操作后还会自动轮播
  71. },
  72. // 如果需要分页器
  73. pagination: {
  74. el: '.swiper-pagination',
  75. clickable: true//点击小点生效
  76. },
  77. // 如果需要前进后退按钮
  78. navigation: {
  79. nextEl: '.swiper-button-next',
  80. prevEl: '.swiper-button-prev',
  81. },
  82. })
  83. }
  84. //点击轮播图跳转
  85. function goBanner () {
  86. $(".swiper-slide").click(function () {
  87. var url = $(this).attr("data-url");
  88. var name = $(this).attr("data-name");
  89. var group_id = $(this).attr("data-id")
  90. if(url){
  91. window.open(url);
  92. }else{
  93. var id = $(this).attr("data-ids");
  94. window.location.href="../list/index.html?id=" + group_id + "&path=nav"
  95. }
  96. })
  97. }
  98. //获取轮播图数据
  99. function gatBannerData () {
  100. $.ajax({
  101. type:"get",
  102. url: api.BASEURL + api.bannerList,
  103. headers:{
  104. sex:api.sex,
  105. source:api.source
  106. },
  107. dataType:"json",
  108. success: function (res) {
  109. console.log(res)
  110. if(res.errno == 0 && res.rst){
  111. if(res.rst.data){
  112. bannerData = res.rst.data;
  113. var swiperHtml = '';
  114. bannerData.forEach(function (item,index) {
  115. swiperHtml += `<div class="swiper-slide" data-url="${item.url}" data-name="${item.note}" data-id="${item.group_id}" data-ids="${item.id}" >
  116. <img width="100%" src="${item.photo}" />
  117. </div>`
  118. })
  119. $(".homeListRight .swiper-wrapper").html(swiperHtml);
  120. }
  121. swiperEvent();
  122. goBanner();
  123. }
  124. },
  125. fail: function (err) {
  126. console.log(err)
  127. }
  128. });
  129. }
  130. //轮播图左边列表
  131. function asideListData(){
  132. $.ajax({
  133. type:'get',
  134. url: api.BASEURL + api.asideList,
  135. headers:{
  136. sex:api.sex,
  137. source:api.source
  138. },
  139. dataType:"json",
  140. success: function (res) {
  141. console.log(res)
  142. var html = '';
  143. if(res.errno == 0 && res.rst){
  144. if(res.rst.data){
  145. asideData = res.rst.data;
  146. for(var i = 0 ; i<res.rst.data.length; i++) {
  147. html += "<li data-id=" + res.rst.data[i].id + "><i style='background:url(" +res.rst.data[i].img+ ") no-repeat;background-size: 100%;'" + "></i><span>" + res.rst.data[i].name + "</span></li>"
  148. }
  149. $(".homeList aside ul").html(html)
  150. // var iconLiList = Array.prototype.slice.call($(".homeList aside ul li"));
  151. // for(var i = 0; i<iconList.length; i++){
  152. // $(iconLiList[i]).find("i").css("background","url(" + iconList[i].icon + ")")
  153. // }
  154. asideHover();
  155. }
  156. }else{
  157. alert(res.err)
  158. }
  159. },
  160. fail: function (err) {
  161. console.log(err)
  162. }
  163. })
  164. }
  165. //左边列表栏划过效果
  166. function asideHover () {
  167. $(".homeList aside ul li").hover(function () {
  168. // iconList.forEach(function (item, index) {
  169. // $(".homeList aside ul li").eq(index).find("i").css("background","url(" + iconList[index].icon + ")")
  170. // })
  171. // $(this).find("i").css("background","url(" + iconList[$(this).index()].icon_on + ")")
  172. if(!!window.ActiveXObject || "ActiveXObject" in window){
  173. $(".homeList aside ul li span").css("color","#5B4242")
  174. $(this).find("span").css('color','#CC1C0E');
  175. }else{
  176. $(".homeList aside ul li span").removeClass('asideHoverActive')
  177. $(this).find("span").addClass('asideHoverActive');
  178. }
  179. },function(){
  180. $(".homeList aside ul li span").removeClass('asideHoverActive')
  181. })
  182. //点击效果
  183. $(".homeList aside ul li").click(function () {
  184. var navListHtml = '';
  185. localStorage.setItem("navList",JSON.stringify(asideData[$(this).index()].itemList))
  186. window.location.href="../list/index.html?id=" + asideData[$(this).index()].itemList[0].id
  187. })
  188. }
  189. //每日精选
  190. function selected (pageNum,category_id) {
  191. loadingFlag = false;
  192. if(maxPage < pageNum){
  193. $(".loading").html("没有更多了")
  194. return;
  195. }
  196. $.ajax({
  197. type:'post',
  198. url: api.BASEURL + api.stocklistByCategoryId,
  199. headers:{
  200. sex:api.sex,
  201. source:api.source
  202. },
  203. dataType:"json",
  204. data:{
  205. category_id:category_id,
  206. page:pageNum
  207. },
  208. success: function (res) {
  209. if(res && res.errno == 0 && res.rst){
  210. var html = '';
  211. maxPage = res.rst.pageInfo.maxPage;
  212. if(res.rst.data.length>0){
  213. if(meiriData.length != 0){
  214. meiriData = meiriData.concat(res.rst.data);
  215. html = template_home(meiriData)
  216. // console.log(meiriData)
  217. }else{
  218. meiriData = res.rst.data;
  219. html = template_home(meiriData)
  220. }
  221. $(".Recommend .commodity_list ul").html(html)
  222. loadingFlag = true;
  223. $(".loading").html("努力加载中...")
  224. }else{
  225. alert('暂无数据')
  226. }
  227. }else{
  228. alert(res.err)
  229. }
  230. },
  231. fail: function (err) {
  232. console.log(err)
  233. }
  234. })
  235. }
  236. //限时抢购
  237. function flashSale () {
  238. var html = '';
  239. var nowTime = Date.parse(new Date()).toString().substr(0,10);
  240. var timeIndex = 0;
  241. var start_time = 0;
  242. var end_time = 0;
  243. activityTime.forEach(function (item, index) {
  244. if(nowTime - item.timeStamp <= 151200 && nowTime - item.timeStamp > 0){
  245. timeIndex = index;
  246. }else if(nowTime > activityTime[0].timeStamp && nowTime < activityTime[1].timeStamp){
  247. timeIndex = 0;
  248. }
  249. })
  250. // var times = activityTime[timeIndex+1].timeStamp - nowTime.toString().substr(0,10);
  251. if(activityTime[timeIndex+1] == undefined){
  252. var times = time_24 - Date.parse(new Date()).toString().substr(0,10)
  253. }else{
  254. var times =activityTime[timeIndex+1].timeStamp - Date.parse(new Date()).toString().substr(0,10)
  255. }
  256. console.log(activityTime[timeIndex+1].timeStamp + "one")
  257. console.log(Date.parse(new Date()).toString().substr(0,10))
  258. console.log(times)
  259. count_down(times)
  260. flashSaleData(activityTime[timeIndex].timeStamp,activityTime[timeIndex+1].timeStamp,1)//首次渲染时候调取
  261. }
  262. //限时抢购接口
  263. function flashSaleData (start_time,end_time,page) {
  264. // http://yhq.quyaqu.com/api/v2/goods/taoQiangGouDataList
  265. $.ajax({
  266. type:'post',
  267. url: api.BASEURL + api.advFourList,
  268. headers:{
  269. sex:api.sex,
  270. source:api.source
  271. },
  272. dataType:"json",
  273. data:{
  274. start_time: start_time,
  275. end_time: end_time,
  276. platform: 1,
  277. page: page,
  278. page_size: 20
  279. },
  280. success: function (res) {
  281. var html = '';
  282. if(res && res.errno && res.rst){
  283. var quan = '';
  284. res.rst.list[0].dataList.forEach(function (item, index) {
  285. var id=item.goods_id;
  286. var is_coupon=item.is_coupon;
  287. var coupon_price=item.coupon_price;
  288. var price=item.price;
  289. var discount_price=item.discount_price;
  290. var commission_rate=item.commission_rate;
  291. if(index < 4){
  292. if(item.coupon_id){
  293. quan = "<div class=commodity_quan><span>券</span><span>" + item.coupon_price + "元</span></div>"
  294. }
  295. html += "<li><a href='../details/index.html?goods_id="+id+"&is_coupon="+is_coupon+"&coupon_price="+coupon_price+"&price="+price+"&discount_price="+discount_price+"&commission_rate="+commission_rate+"' class='lp_li_a'><img src=" + item.img + " /><div class=commodity><div class=commodity_name>" + item.title + "</div><div class=commodity_introduce>" + quan + "<div class=commodity_number>付款人数:<span>" + item.sale_num + "</span></div></div><div class=commodity_price><div><span class=nowPrice>¥" + item.discount_price + "</span><span class=oldPrice>¥" + item.price + "</span></div><div>立即抢购</div></div><div class=commodity_jdt><div class=progress><div class=progress_value style=width:" + item.sale_num/item.total_num*100 + "%></div><span>剩余" + Number(item.total_num-item.sale_num) + "件</span></div></div></div></a></li>"
  296. }
  297. })
  298. $(".flash_sale .commodity_list ul").html(html)
  299. }
  300. },
  301. fail: function (err) {
  302. console.log(err)
  303. }
  304. })
  305. }
  306. //倒计时
  307. function count_down (times) {
  308. var timer=null;
  309. timer=setInterval(function(){
  310. var day=0,
  311. hour=0,
  312. minute=0,
  313. second=0;//时间默认值
  314. if(times > 0){
  315. day = Math.floor(times / (60 * 60 * 24));
  316. hour = Math.floor(times / (60 * 60)) - (day * 24);
  317. minute = Math.floor(times / 60) - (day * 24 * 60) - (hour * 60);
  318. second = Math.floor(times) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
  319. if (day <= 9) day = '0' + day;
  320. if (hour <= 9) hour = '0' + hour;
  321. if (minute <= 9) minute = '0' + minute;
  322. if (second <= 9) second = '0' + second;
  323. $(".time_count_down").html(hour + "&ensp;:&ensp;" + minute + "&ensp;:&ensp;" + second)
  324. }
  325. // console.log(day+"天:"+hour+"小时:"+minute+"分钟:"+second+"秒");
  326. if(times<=0){
  327. clearInterval(timer);
  328. flashSale();
  329. }
  330. times--;
  331. },1000);
  332. }
  333. //导航栏目(商品分类)
  334. function navlistData(){
  335. $.ajax({
  336. type:"get",
  337. url: api.BASEURL + api.navList,
  338. headers:{
  339. sex:api.sex,
  340. source:api.source
  341. },
  342. dataType:"json",
  343. success:function(res) {
  344. if(res.errno == 0 && res.rst){
  345. if(res.rst.data){
  346. navListData = res.rst.data;
  347. var navListHtml = '';
  348. navListData.forEach(function (item,index) {
  349. navListHtml += `<li data-id=${item.id}><a href="../list/index.html?id=${item.id}&path=nav">${item.name}</a></li>`
  350. })
  351. $(".head_item_nav ul").html(navListHtml);
  352. }
  353. }
  354. },
  355. fail: function (err) {
  356. console.log(err)
  357. }
  358. });
  359. }