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
    //   }
    // };
  • 相关阅读:
    70.BOM
    69.捕获错误try catch
    68.键盘事件
    523. Continuous Subarray Sum
    901. Online Stock Span
    547. Friend Circles
    162. Find Peak Element
    1008. Construct Binary Search Tree from Preorder Traversal
    889. Construct Binary Tree from Preorder and Postorder Traversal
    106. Construct Binary Tree from Inorder and Postorder Traversal
  • 原文地址:https://www.cnblogs.com/ycherry/p/12106189.html
Copyright © 2011-2022 走看看