[关闭]
@jeffjade 2018-03-21T14:59:15.000000Z 字数 2274 阅读 1554

JavaScript常用方法轮子

javascript


Array篇

数组乱序:

  1. Array.prototype.shuffle = function(){
  2. for(var j, x, l = i = this.length;
  3. i;
  4. j = parseInt(Math.random() * l), x = this[--i], this[i] = this[j], this[j] = x);
  5. return this;
  6. };
  7. var arr = new Array(1,2,3,4,5,6,7,8,9,0);
  8. arr.shuffle();
  9. console.log(arr);
  10. //[ 5, 7, 1, 3, 9, 2, 0, 4, 6, 8 ]
  11. //[Finished in 0.2s]

String篇

  1. /*Desc:在string指定位置插入subStr*/
  2. function insertStrToFixedPos(ResStr ,subStr , pos){
  3. subStr = subStr || "";
  4. pos = pos || 0;
  5. var newStr = "";
  6. var forntStr = ResStr.substring(0, pos);
  7. var endStr = ResStr.substring(pos , ResStr.length);
  8. newStr = forntStr + subStr + endStr;
  9. return newStr;
  10. }
  11. // insertStrToFixedPos("aaabbbccc" , "fff" , 3); //aaafffbbbccc
  12. // print(insertStrToFixedPos("aaabbbccc" , "fff")); //fffaaabbbccc
  13. // print(insertStrToFixedPos("aaabbbccc")); //aaabbbccc

操作JS String扩展

  1. // 获取当前地址,指定参数的值;
  2. function queryUrlParam (name) {
  3. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
  4. var r = window.location.search.substr(1).match(reg)
  5. if (r != null) {
  6. return unescape(r[2])
  7. }
  8. return null
  9. }

动态加载 gtag

  1. const loadScript = url => {
  2. return new Promise((resolve, reject) => {
  3. const head = document.head || document.getElementsByTagName('head')[0]
  4. const script = document.createElement('script')
  5. script.async = true
  6. script.src = url
  7. script.charset = 'utf8'
  8. head.appendChild(script)
  9. script.onload = resolve
  10. script.onerror = reject
  11. })
  12. }
  13. import Vue from 'vue'
  14. export default () => {
  15. // Apparently is now mandatory to add the ID in the url
  16. const id = 'UA-xxxxxxxx-x'
  17. const scriptURL = `https://www.googletagmanager.com/gtag/js?id=${id}`
  18. loadScript(scriptURL)
  19. .then(() => {
  20. window.dataLayer = window.dataLayer || []
  21. const gtag = () => window.dataLayer.push(arguments)
  22. gtag('js', new Date())
  23. gtag('config', id)
  24. Vue.prototype.$gtag = gtag
  25. })
  26. .catch(error => {
  27. console.log(error.message)
  28. })
  29. }

getBoundingClientRect判断元素是否在可视区域

getBoundingClientRect 是获取可视区域相关位置信息的,使用这个属性来判断更加方便:

  1. function isElementInViewport (el) {
  2. var rect = el.getBoundingClientRect();
  3. return (
  4. rect.top >= 0 &&
  5. rect.left >= 0 &&
  6. rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
  7. rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
  8. );
  9. }
  1. /**
  2. * @desc: 判断浏览器是否处于全屏状态;
  3. * @date: 2018-03-21
  4. */
  5. function checkIsFullScreen() {
  6. var isFullScreen = document.webkitIsFullScreen || window.fullScreen || document.mozFullScreen;
  7. var isPhysicalFullScreen = window.outerHeight === window.screen.height && window.outerWidth === window.screen.width
  8. return isFullScreen || isFullScreen || false;
  9. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注