import Vue from "vue";
// function getElementToPageTop(el) {
//   if (el.parentElement) {
//     return getElementToPageTop(el.parentElement) + el.offsetTop;
//   }
//   return el.offsetTop;
// }

Vue.directive("domObserver", {
  bind(el, binding) {
    let top = "";
    let height = "";
    function isResize() {
      // let toPageTop = getElementToPageTop(el);
      let toPageTop = el.getBoundingClientRect().top;
      let elHeight = el.getBoundingClientRect().height;
      if (toPageTop != top || elHeight != height) {
        binding.value({ top: toPageTop, height: elHeight });
      }
      top = toPageTop;
      height = elHeight;
    }
    el.__vueSetInterval__ = setInterval(isResize, 300);
  },
  unbind(el) {
    // console.log(el, "解绑");
    clearInterval(el.__vueSetInterval__);
  },
});
