home.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. function getParams(url) {
  2. var theRequest = new Object();
  3. if (!url)
  4. url = location.href;
  5. if (url.indexOf("?") !== -1)
  6. {
  7. var str = url.substr(url.indexOf("?") + 1) + "&";
  8. var strs = str.split("&");
  9. for (var i = 0; i < strs.length - 1; i++)
  10. {
  11. var key = strs[i].substring(0, strs[i].indexOf("="));
  12. var val = strs[i].substring(strs[i].indexOf("=") + 1);
  13. theRequest[key] = val;
  14. }
  15. }
  16. return theRequest;
  17. }
  18. var objUrl = {}; //获取get参数
  19. var url = window.location.href;
  20. var osi = $(".siwh ul li").eq(0).attr('data-id');
  21. var params = getParams(url);
  22. if(!$.isEmptyObject(params)){
  23. $.each(params,function(key,val){
  24. objUrl[key] = val;
  25. })
  26. }
  27. /*----------------定义页面全局变量-----------------------*/
  28. // 页数
  29. var page = 0;
  30. // 每页展示5个
  31. var size = 10;
  32. //资讯ID
  33. var thisID=0;
  34. /*----------------------------------------------------------------------------------*/
  35. // 封装 ajax 方法
  36. function AjaxFn(){
  37. page = 0;
  38. page++;
  39. $.ajax({
  40. type: 'GET',
  41. url: '/news/homeform?&page='+page+'&limit='+size + '&cid='+thisID,
  42. dataType: 'json',
  43. success: function(data){
  44. if(data.code == 200){
  45. $('.news_list .ns').append(ModuleWay(data.data)) //调用选择模块
  46. }
  47. },
  48. error: function(xhr, type){
  49. }
  50. });
  51. }
  52. function ModuleWay(conditions){
  53. var _html="";
  54. $.each(conditions.data,function (key,val) {
  55. _html +='<div class="plan_one">';
  56. _html +='<a href="/news/details/'+val.id+'/">';
  57. _html +='<div class="plan_one_title">';
  58. _html +='<p class="text">'+val.subject+'</p>';
  59. _html +=' <p> <span class="guis">'+val.source+' </span><i>·</i><span class="data">'+val.open_time+'</span></p>';
  60. _html +='</div>';
  61. _html +='<div class="plan_one_img"><img src="'+conditions.url+val.thumb[0]+'" alt=""></div>';
  62. _html +='</a>';
  63. _html +='</div>';
  64. })
  65. return _html ;
  66. }
  67. $(function(){
  68. thisID=$('#demo06 ul li').eq(0).attr('data-id');
  69. // 判断 objUrl 是否从别到页面跳转到这里
  70. if(typeof objUrl.cid == "string"){
  71. thisID = objUrl.cid;
  72. }
  73. var ali= 0;
  74. // 点击切换导航栏 ajax 切换
  75. $(".siwh ul").on('click','li',function(){
  76. $('.news_list .dropload-down').remove();
  77. var $this = $(this);
  78. thisID = $this.attr('data-id');
  79. $(".siwh ul li").removeClass('cur');
  80. $('.siwh ul li[data-id="'+thisID+'"]').addClass('cur');
  81. osi = thisID;
  82. $('.news_list .ns').html(""); //切换内容之前先清空盒子
  83. AjaxFn(); //调用Ajax方法
  84. fun();
  85. })
  86. function fun(){
  87. // 上滑加载
  88. $('.news_list').dropload({
  89. // 调用加载
  90. scrollArea : window,
  91. threshold:4/10,
  92. loadDownFn : function(me){
  93. page++;
  94. $.ajax({
  95. type: 'GET',
  96. url: '/news/homeform?&page='+page+'&limit='+size + '&cid='+thisID,
  97. dataType: 'json',
  98. success: function(data){
  99. // console.log(data);
  100. if(data.code == 200){
  101. $('.news_list .ns').append(ModuleWay(data.data)) //调用选择模块
  102. //限制字符个数
  103. $(".news_list .plan_one").each(function(){
  104. var maxwidth=35;
  105. var _lenght = $(this).find('.plan_one_title .text').text().length
  106. var that = $(this).find('.plan_one_title .text')
  107. if(_lenght > maxwidth){
  108. that.text(that.text().substring(0,maxwidth));
  109. that.html(that.html()+'…');
  110. }
  111. });
  112. }else{
  113. // 锁定
  114. me.lock();
  115. // 无数据
  116. me.noData();
  117. }
  118. // alert(data);
  119. // 每次数据加载完,必须重置
  120. setTimeout(function(){
  121. // 插入数据到页面,放到最后面
  122. // $('.news_list').append(result);
  123. // 每次数据插入,必须重置
  124. me.resetload();
  125. },1000);
  126. // me.resetload();
  127. // $('.dropload-down').hide();
  128. },
  129. error: function(xhr, type){
  130. // alert('Ajax error!');
  131. // 即使加载出错,也得重置
  132. me.resetload();
  133. }
  134. });
  135. }
  136. });
  137. }
  138. fun();
  139. })
  140. var mySwiper = new Swiper ('.swiper-container2', {
  141. autoplay: true,//可选选项,自动滑动
  142. speed:300,
  143. loop : true,
  144. autoplay:5000,//等同于以下设置
  145. pagination: '.swiper-pagination'
  146. })
  147. //demo示例六 通过id调取
  148. $('#demo06').navbarscroll({
  149. defaultSelect:0,
  150. scrollerWidth:6,
  151. fingerClick:1,
  152. endClickScroll:function(obj){
  153. // $(".siwh ul li").removeClass('cur');
  154. // $('.siwh ul li[data-id="'+osi+'"]').addClass('cur');
  155. }
  156. });
  157. $(window).scroll(function (){
  158. var st = $(this).scrollTop();
  159. // console.log(st)
  160. if(st >300){
  161. $('.find_nav').show();
  162. //demo示例六 通过id调取
  163. $('#demo05').navbarscroll({
  164. defaultSelect:0,
  165. scrollerWidth:6,
  166. fingerClick:1,
  167. endClickScroll:function(obj){
  168. $(".siwh ul li").removeClass('cur');
  169. $('.siwh ul li[data-id="'+osi+'"]').addClass('cur');
  170. }
  171. });
  172. }else{
  173. $('.find_nav').hide();
  174. }
  175. });