common.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. $(function () {
  2. //header
  3. $('#menu-btn').on('click', function () {
  4. $('.app-header').toggleClass('open');
  5. });
  6. // nav click
  7. $('.child-link').on('click', function () {
  8. $(this).parent('.menu-item').toggleClass('on');
  9. $(this).siblings('.child').slideToggle();
  10. return false;
  11. });
  12. // footer
  13. $('.app-footer .link-name').on('click', function () {
  14. $(this).parent().siblings().find('.link-list').slideUp();
  15. $(this).siblings('.link-list').stop().slideToggle();
  16. $(this).parent().toggleClass('on').siblings().removeClass('on');
  17. });
  18. /**
  19. * 返回顶部
  20. */
  21. $('#go-top').on('click', function () {
  22. $('html,body').animate({
  23. 'scrollTop': 0
  24. });
  25. });
  26. $(window).on('scroll', function () {
  27. var winH = $(window).height();
  28. var top = $(document).scrollTop();
  29. if (top >= winH * 1.5) {
  30. $('#go-top').stop().fadeIn(200);
  31. } else {
  32. $('#go-top').stop().fadeOut(200);
  33. }
  34. })
  35. /*****************/
  36. $('.weixin-share-box').on('click', function () {
  37. var $that = $(this);
  38. $(this).toggleClass('on');
  39. if ($(this).hasClass('on')) {
  40. setTimeout(function () {
  41. $(document).one('click', function () {
  42. $that.removeClass('on');
  43. });
  44. })
  45. }
  46. });
  47. });
  48. //分享到新浪微博
  49. function shareToSinaWB(event) {
  50. event.preventDefault();
  51. var _shareUrl = 'http://v.t.sina.com.cn/share/share.php?'; //真实的appkey,必选参数
  52. _shareUrl += '&url=' + encodeURIComponent(shareConfig._url || document.location); //参数url设置分享的内容链接|默认当前页location,可选参数
  53. _shareUrl += '&title=' + encodeURIComponent(shareConfig._title || document.title); //参数title设置分享的标题|默认当前页标题,可选参数
  54. _shareUrl += '&source=' + encodeURIComponent(shareConfig._source || '');
  55. _shareUrl += '&sourceUrl=' + encodeURIComponent(shareConfig._sourceUrl || '');
  56. _shareUrl += '&content=' + 'utf-8'; //参数content设置页面编码gb2312|utf-8,可选参数
  57. _shareUrl += '&pic=' + encodeURIComponent(shareConfig._pic || ''); //参数pic设置图片链接|默认为空,可选参数
  58. window.open(_shareUrl, '_blank');
  59. }
  60. //分享到QQ空间
  61. function shareToQzone(event) {
  62. event.preventDefault();
  63. var _shareUrl = 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?';
  64. _shareUrl += 'url=' + encodeURIComponent(shareConfig._url || document.location); //参数url设置分享的内容链接|默认当前页location
  65. _shareUrl += '&desc=' + encodeURIComponent(shareConfig._desc || '分享的描述'); //参数desc设置分享的描述,可选参数
  66. _shareUrl += '&summary=' + encodeURIComponent(shareConfig._summary || '分享摘要'); //参数summary设置分享摘要,可选参数
  67. _shareUrl += '&title=' + encodeURIComponent(shareConfig._title || document.title); //参数title设置分享标题,可选参数
  68. _shareUrl += '&site=' + encodeURIComponent(shareConfig._site || ''); //参数site设置分享来源,可选参数
  69. _shareUrl += '&pics=' + encodeURIComponent(shareConfig._pic || ''); //参数pics设置分享图片的路径,多张图片以"|"隔开,可选参数
  70. window.open(_shareUrl, '_blank');
  71. }
  72. //请求数据
  73. function loadMore(config, closeList) {
  74. var sendData = {
  75. pageIndex: config.pageIndex,
  76. pageSize: config.pageSize
  77. };
  78. $.extend(sendData, config.qry);
  79. $.get(config.url, sendData, function result(res) {
  80. if (!res) {
  81. alert('请求出错' + res.message);
  82. return;
  83. }
  84. var data = res;
  85. if (typeof config.pipe === 'function') {
  86. config.pipe(data);
  87. }
  88. var html = '';
  89. data.forEach(function (v, i) {
  90. html += $('#template').html().replace(/{{([^{}]+?)}}/igm,
  91. function (match, key) {
  92. return v[key];
  93. });
  94. });
  95. if (closeList === true) {
  96. config.list.html('');
  97. }
  98. config.list.append($(html));
  99. if (typeof config.afterFun === 'function') {
  100. config.pipe(data);
  101. }
  102. //判断是否还有数据
  103. if (data.length < config.pageSize) {
  104. $('.empty-text').html('');
  105. $(document).off('scroll.more');
  106. } else {
  107. $('.empty-text').html('↑上划加载更多...');
  108. }
  109. config.onOff = true;
  110. });
  111. }
  112. //上滑刷新
  113. function scrollData(config) {
  114. $(document).off('scroll.more');
  115. if ($('.empty-text').attr('empty') !== 'true') {
  116. $(document).on('scroll.more', function () {
  117. var vTop = $(document).scrollTop();
  118. var bottom = $('.empty-text').offset().top - 100; //多减100px 提前加载
  119. if (vTop >= bottom - innerHeight && config.onOff) {
  120. config.onOff = false;
  121. $('.empty-text').html('加载中. . .');
  122. //准备请求
  123. config.pageIndex++;
  124. loadMore(config);
  125. }
  126. })
  127. }
  128. }