resize.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. (function(doc, win) {
  2. var docEl = doc.documentElement,
  3. resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
  4. recalc = function() {
  5. var clientWidth = docEl.clientWidth;
  6. if(!clientWidth) return;
  7. if(clientWidth >= 1080) {
  8. docEl.style.fontSize = '100px';
  9. } else {
  10. docEl.style.fontSize = 100 * (clientWidth / 750) + 'px';
  11. }
  12. };
  13. if(!doc.addEventListener) return;
  14. win.addEventListener(resizeEvt, recalc, false);
  15. doc.addEventListener('DOMContentLoaded', recalc, false);
  16. var docEl = document.documentElement;
  17. var clientWidth = docEl.clientHeight;
  18. })(document, window);
  19. function toThousands (num, decimal) {
  20. if (decimal) {
  21. num = formatDecimal(num, decimal)
  22. }
  23. return num.toString().replace(/\d+/, function (n) { // 先提取整数部分
  24. return n.replace(/(\d)(?=(\d{3})+$)/g, function ($1) {
  25. return $1 + ','
  26. })
  27. })
  28. }
  29. function formatDecimal (num, decimal) {
  30. num = num.toString()
  31. const index = num.indexOf('.')
  32. if (index !== -1) {
  33. num = num.substring(0, decimal + index + 1)
  34. } else {
  35. num = num.substring(0)
  36. }
  37. return parseFloat(num).toFixed(decimal)
  38. }