let scrollTop = 0; // 滚动离开顶部的距离
/**
 * @Description: 吸顶指令
 * @author LiuBo
 * @date 2021/1/26
 * @time 17:17
 */
const SuctionTop = {
  bind: (el: any, binding: any) => {
    el.addEventListener("scroll", () => {
      SuctionTop.onScroll(el, binding.value);
    });
  },
  update: () => {},
  // 滚动时调用
  onScroll: (element: any, callBack: any) => {
    const scrollT = element.scrollTop;
    SuctionTop.canSuction(element, element.scrollHeight - element.clientHeight);
    const isScrollUp = scrollT - scrollTop > 0; // 页面是否向上滚动
    callBack({ scrollTop: scrollT, isScrollUp: isScrollUp });
    scrollTop = scrollT;
  },
  // 计算
  canSuction: (element: any, height: number) => {
    if (height > 20 && height < 130) {
      const div = element.childNodes[0].tagName === "DIV" ? element.childNodes[0] : element.childNodes[0].childNodes[0];
      div.style.minHeight = `${element.clientHeight + 130}px`;
    }
  }
};
export { SuctionTop };
