zoukankan      html  css  js  c++  java
  • JS 工具函数

    // var tools = function() {
      function addEvent(el, type, fn) {
        if (el.addEventListener) { // 
          el.addEventListener(type, fn, false);
        } else if (el.attachEvent) {
          el.attachEvent('on' + type, fn);
        } else {
          el['on' + type] = fn;
        }
      }
    
      function getStyles(elem, prop) {
        if (window.getComputedStyle) {
          if (prop) {
            return parseInt(window.getComputedStyle(elem)[prop]);
          } else {
            return window.getComputedStyle;
          }
        } else if (elem.currentStyle) {
          if (prop) {
            return elem.currentStyle[prop]
          } else {
            return elem.currentStyle;
          }
    
        }
      }
    
      function getEleDocPosition(ele) {
        var parent = ele.offsetParent,
          offsetLeft = ele.offsetLeft,
          offsetTop = ele.offsetTop;
        while (parent) {
          offsetLeft += parent.offsetLeft;
          offsetTop += parent.offsetTop;
          parent = parent.offsetParent;
        }
    
        return {
          left: offsetLeft,
          top: offsetTop
        }
      }
    
      function getScrollSize() {
        if (document.body.scrollHeight) {
          return {
             document.body.scrollWidth,
            height: document.body.scrollHeight
          };
        } else {
          return {
             document.documentElement.scrollWidth,
            height: document.documentElement.scrollHeight
          }
        }
      }
    
      function getViewportSize() {
        if (window.innerWidth) {
          return {
             window.innerWidth,
            height: window.innerHeight
          }
        } else {
          if (document.compatMode === 'BackCompat') {
            return {
               document.body.clientWidth,
              height: document.body.clientHeight
            }
          } else if (document.compatMode === 'CSS1Compat') {
            return {
               document.documentElement.clientWidth,
              height: document.documentElement.clientHeight
            }
          }
        }
      }
    
      function getScrollOffset() {
        if (window.pageXOffset) { // w3c 规范
          return {
            left: window.pageXOffset,
            top: window.pageYOffset
          }
        } else {
          return {
            left: document.body.scrollLeft + document.documentElement.scrollLeft, //document.body.scrollLeft 和 document.documentElement.scrollLeft都是数字类型,不存在即为0
            top: document.body.scrollTop + document.documentElement.scrollTop,
          }
        }
      }
    
      function elemChildren(node) {
        var temp = {
          'length': 0,
          'push': Array.prototype.push,
          'splice': Array.prototype.splice
        },
            children = node.childNodes;
        for (var i = 0; i < children.length; i++) {
          var childItem = children[i];
    
          if (childItem.nodeType === 1) {
            // temp[temp['length']] = childItem;
            // temp['length']++;
            temp.push(childItem);
          }
        }
        return temp;
      }
    
      function cancelBuble(e) {
        if (e.stopPropagation) {
          e.stopPropagation();
        } else {
          e.cancelBuble = true;
        }
      }
    
    
    //   return {
    //     addEvent: addEvent,
    //     getStyles: getStyles,
    //     getEleDocPosition: getEleDocPosition,
    //     getScrollWidth: getScrollWidth,
    //     getViewportSize: getViewportSize,
    //     getScrollOffset: getScrollOffset,
    //     elemChildren: elemChildren,
    //     cancelBuble: cancelBuble
    //   }
    // };
  • 相关阅读:
    js多个基本类型计算
    移动端弹窗滚动穿透问题
    length-of-longest-substring 无重复字符的最长子串 javascript解答
    addTwoNumbers两数之和 javascript解答
    two-sum两数之和 javascript解答
    js防抖和节流
    React / Vue 项目时为什么要在列表组件中写 Key,其作用是什么?
    二进制文件流处理笔记
    ES6 class 类的理解(一)
    django之js模板插件artTemplate的使用
  • 原文地址:https://www.cnblogs.com/ycherry/p/12106189.html
Copyright © 2011-2022 走看看