码迷,mamicode.com
首页 > Web开发 > 详细

JS 工具函数

时间:2019-12-27 11:27:57      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:com   turn   spl   inner   emc   atm   parent   ret   ++   

// 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 {
        width: document.body.scrollWidth,
        height: document.body.scrollHeight
      };
    } else {
      return {
        width: document.documentElement.scrollWidth,
        height: document.documentElement.scrollHeight
      }
    }
  }

  function getViewportSize() {
    if (window.innerWidth) {
      return {
        width: window.innerWidth,
        height: window.innerHeight
      }
    } else {
      if (document.compatMode === ‘BackCompat‘) {
        return {
          width: document.body.clientWidth,
          height: document.body.clientHeight
        }
      } else if (document.compatMode === ‘CSS1Compat‘) {
        return {
          width: 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 工具函数

标签:com   turn   spl   inner   emc   atm   parent   ret   ++   

原文地址:https://www.cnblogs.com/ycherry/p/12106189.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!