common.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // commons在DOM操作完毕后插入
  2. // 获取可视窗宽高
  3. function getViewPortWH() {
  4. return {
  5. clientW: document.documentElement.clientWidth || document.body.clientWidth,
  6. clientH: document.documentElement.clientHeight || document.body.clientHeight
  7. }
  8. }
  9. function isMobile() {
  10. const regex_match = /(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|jigs browser|hiptop|^benq|haier|^lct|operas*mobi|opera*mini|320x320|240x320|176x220)/i;
  11. const u = navigator.userAgent;
  12. if (null == u) {
  13. return true;
  14. }
  15. let result = regex_match.exec(u);
  16. return null == result ? false : true;
  17. }
  18. /**
  19. * css属性的动画设置
  20. * @param {object} settings 动画效果配置参数
  21. */
  22. function cssAnimate(settings) {
  23. // 使用解构赋值 提取参数值到变量中
  24. let {
  25. ele,
  26. transform,
  27. duration,
  28. whendone = null
  29. } = settings
  30. if (ele.timer) return // 如果已有定时器 则点击无效
  31. let frames = 0,
  32. numFrames = duration / 100
  33. const beginAt = {},
  34. increment = {}
  35. /*
  36. * 1| 计算初始样式和动画增量
  37. */
  38. const cssProps = getComputedStyle(ele)
  39. for (let item in transform) {
  40. beginAt[item] = parseInt(cssProps[item])
  41. increment[item] = (transform[item] - beginAt[item]) / numFrames
  42. }
  43. /*
  44. * 2| 设置定时任务执行动画
  45. */
  46. ele.timer = setInterval(function () {
  47. frames++;
  48. // 判断临界条件
  49. if (frames > numFrames) {
  50. // 取消定时器
  51. clearInterval(ele.timer)
  52. delete ele.timer;
  53. if (whendone instanceof Function) whendone.call(ele);
  54. return false;
  55. }
  56. for (let item in transform) {
  57. ele.style[item] = (beginAt[item] + increment[item] * frames) + 'px';
  58. }
  59. }, 100);
  60. }
  61. function timeTD(resolve, reject, seconds = null) {
  62. let total = seconds || 60;
  63. resolve(total);
  64. let timer = setInterval(function () {
  65. if (--total < 1) {
  66. clearInterval(timer);
  67. reject();
  68. } else {
  69. resolve(total);
  70. }
  71. }, 1000);
  72. }
  73. function curryTimeTD(resolve, seconds = null) {
  74. return function (reject) {
  75. let total = seconds || 60;
  76. resolve(total);
  77. let timer = setInterval(function () {
  78. if (--total < 1) {
  79. clearInterval(timer);
  80. reject();
  81. } else {
  82. resolve(total);
  83. }
  84. }, 1000);
  85. return timer;
  86. }
  87. }
  88. export default {
  89. getViewPortWH,
  90. cssAnimate,
  91. timeTD,
  92. curryTimeTD,
  93. isMobile
  94. }