util.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. @Name:layui.util 工具集
  3. @Author:贤心
  4. @License:MIT
  5. */
  6. layui.define('jquery', function(exports){
  7. "use strict";
  8. var $ = layui.$
  9. //外部接口
  10. ,util = {
  11. //固定块
  12. fixbar: function(options){
  13. var ELEM = 'layui-fixbar', TOP_BAR = 'layui-fixbar-top'
  14. ,dom = $(document), body = $('body')
  15. ,is, timer;
  16. options = $.extend({
  17. showHeight: 200 //出现TOP的滚动条高度临界值
  18. }, options);
  19. options.bar1 = options.bar1 === true ? '' : options.bar1;
  20. options.bar2 = options.bar2 === true ? '' : options.bar2;
  21. options.bgcolor = options.bgcolor ? ('background-color:' + options.bgcolor) : '';
  22. var icon = [options.bar1, options.bar2, ''] //图标:信息、问号、TOP
  23. ,elem = $(['<ul class="'+ ELEM +'">'
  24. ,options.bar1 ? '<li class="layui-icon" lay-type="bar1" style="'+ options.bgcolor +'">'+ icon[0] +'</li>' : ''
  25. ,options.bar2 ? '<li class="layui-icon" lay-type="bar2" style="'+ options.bgcolor +'">'+ icon[1] +'</li>' : ''
  26. ,'<li class="layui-icon '+ TOP_BAR +'" lay-type="top" style="'+ options.bgcolor +'">'+ icon[2] +'</li>'
  27. ,'</ul>'].join(''))
  28. ,topBar = elem.find('.'+TOP_BAR)
  29. ,scroll = function(){
  30. var stop = dom.scrollTop();
  31. if(stop >= (options.showHeight)){
  32. is || (topBar.show(), is = 1);
  33. } else {
  34. is && (topBar.hide(), is = 0);
  35. }
  36. };
  37. if($('.'+ ELEM)[0]) return;
  38. typeof options.css === 'object' && elem.css(options.css);
  39. body.append(elem), scroll();
  40. //bar点击事件
  41. elem.find('li').on('click', function(){
  42. var othis = $(this), type = othis.attr('lay-type');
  43. if(type === 'top'){
  44. $('html,body').animate({
  45. scrollTop : 0
  46. }, 200);
  47. }
  48. options.click && options.click.call(this, type);
  49. });
  50. //Top显示控制
  51. dom.on('scroll', function(){
  52. clearTimeout(timer);
  53. timer = setTimeout(function(){
  54. scroll();
  55. }, 100);
  56. });
  57. }
  58. //倒计时
  59. ,countdown: function(endTime, serverTime, callback){
  60. var that = this
  61. ,type = typeof serverTime === 'function'
  62. ,end = new Date(endTime).getTime()
  63. ,now = new Date((!serverTime || type) ? new Date().getTime() : serverTime).getTime()
  64. ,count = end - now
  65. ,time = [
  66. Math.floor(count/(1000*60*60*24)) //天
  67. ,Math.floor(count/(1000*60*60)) % 24 //时
  68. ,Math.floor(count/(1000*60)) % 60 //分
  69. ,Math.floor(count/1000) % 60 //秒
  70. ];
  71. if(type) callback = serverTime;
  72. var timer = setTimeout(function(){
  73. that.countdown(endTime, now + 1000, callback);
  74. }, 1000);
  75. callback && callback(count > 0 ? time : [0,0,0,0], serverTime, timer);
  76. if(count <= 0) clearTimeout(timer);
  77. return timer;
  78. }
  79. //某个时间在当前时间的多久前
  80. ,timeAgo: function(time, onlyDate){
  81. var stamp = new Date().getTime() - new Date(time).getTime();
  82. //超过30天,返回具体日期
  83. if(stamp > 1000*60*60*24*30){
  84. stamp = new Date(time).toLocaleString();
  85. onlyDate && (stamp = stamp.replace(/\s[\S]+$/g, ''));
  86. return stamp;
  87. }
  88. //30天以内,返回“多久前”
  89. if(stamp >= 1000*60*60*24){
  90. return ((stamp/1000/60/60/24)|0) + '天前';
  91. } else if(stamp >= 1000*60*60){
  92. return ((stamp/1000/60/60)|0) + '小时前';
  93. } else if(stamp >= 1000*60*3){ //3分钟以内为:刚刚
  94. return ((stamp/1000/60)|0) + '分钟前';
  95. } else if(stamp < 0){
  96. return '未来';
  97. } else {
  98. return '刚刚';
  99. }
  100. }
  101. };
  102. exports('util', util);
  103. });