一段監視 DOM 的神奇代碼

做者:Eddie Aich

翻譯:瘋狂的技術宅javascript

原文:https://dev.to/eddieaich/spy-...前端

未經容許嚴禁轉載java

經過使用此模塊,只需將鼠標懸停在瀏覽器中,便可快速查看DOM元素的屬性。基本上它是一個即時檢查器。node

spyon-demo-sm.gif

將鼠標懸停在 DOM 元素上會顯示其屬性!git

本身嘗試一下

複製下面的整個代碼塊,並將其粘貼到瀏覽器 Web 控制檯中。如今將鼠標懸停在你正在瀏覽的任何網頁上。 看到了什麼?程序員

(function SpyOn() {

  const _id = 'spyon-container',
        _posBuffer = 3;

  function init() {
    document.body.addEventListener('mousemove', glide);
    document.body.addEventListener('mouseover', show);
    document.body.addEventListener('mouseleave', hide);
  }

  function hide(e) {
    document.getElementById(_id).style.display = 'none';
  }

  function show(e) {
    const spyContainer = document.getElementById(_id);
    if (!spyContainer) {
      create();
      return;
    }
    if (spyContainer.style.display !== 'block') {
      spyContainer.style.display = 'block';
    }
  }

  function glide(e) {
    const spyContainer = document.getElementById(_id);
    if (!spyContainer) {
      create();
      return;
    }
    const left = e.clientX + getScrollPos().left + _posBuffer;
    const top = e.clientY + getScrollPos().top + _posBuffer;
    spyContainer.innerHTML = showAttributes(e.target);
    if (left + spyContainer.offsetWidth > window.innerWidth) {
      spyContainer.style.left = left - spyContainer.offsetWidth + 'px';
    } else {
      spyContainer.style.left = left + 'px';
    }
    spyContainer.style.top = top + 'px';
  }

  function getScrollPos() {
    const ieEdge = document.all ? false : true;
    if (!ieEdge) {
      return {
        left : document.body.scrollLeft,
        top : document.body.scrollTop
      };
    } else {
      return {
        left : document.documentElement.scrollLeft,
        top : document.documentElement.scrollTop
      };
    }
  }

  function showAttributes(el) {
    const nodeName = `<span style="font-weight:bold;">${el.nodeName.toLowerCase()}</span><br/>`;
    const attrArr = Array.from(el.attributes);
    const attributes = attrArr.reduce((attrs, attr) => {
      attrs += `<span style="color:#ffffcc;">${attr.nodeName}</span>="${attr.nodeValue}"<br/>`;
      return attrs;
    }, '');
    return nodeName + attributes;
  }

  function create() {
    const div = document.createElement('div');
    div.id = _id;
    div.setAttribute('style', `
      position: absolute;
      left: 0;
      top: 0;
      width: auto;
      height: auto;
      padding: 10px;
      box-sizing: border-box;
      color: #fff;
      background-color: #444;
      z-index: 100000;
      font-size: 12px;
      border-radius: 5px;
      line-height: 20px;
      max-width: 45%;
      `
    );
    document.body.appendChild(div);
  }

  init();

})();

它是怎麼運做的

此模塊以 IIFE 的形式實現。這樣只要須要一些 DOM 監視輔助,就能夠將代碼複製並粘貼到 Web 控制檯中。將 div 插入到文檔的正文中,並在正文上啓用鼠標事件偵聽器。從目標元素中檢索屬性,將其簡化爲單個字符串,最後在工具提示中顯示。github

用例

  1. 幫助解決UI錯誤
  2. 確保你所應用的 DOM 元素可以按預期工做(好比點擊得到正確的類,等等)
  3. 瞭解一個 Web 應用的結構

你能夠從這段代碼中學到什麼

  1. 如何使用 Vanilla JS 實現工具提示模塊
  2. 如何解析 DOM 對象的屬性
  3. 如何找到鼠標 X 和 Y 的位置
  4. 如何獲取文檔的滾動位置
  5. 瞭解不一樣瀏覽器的行爲方式 —— Edge vs. Chrome vs. Safari

開源

你能夠在這裏找到源代碼,但願你能作得更好!也許你不但願將其做爲 IIFE 來實現,或者是去顯示其餘數據。面試


本文首發微信公衆號:前端先鋒

歡迎掃描二維碼關注公衆號,天天都給你推送新鮮的前端技術文章

歡迎掃描二維碼關注公衆號,天天都給你推送新鮮的前端技術文章


歡迎繼續閱讀本專欄其它高贊文章:


相關文章
相關標籤/搜索