页面滚动元素显示,不滚动几秒后消失

需求

在页面上有"返回顶部"的按钮,要实现在上下滚动页面时,该按钮显示,当页面停止滚动几秒后,按钮消失。

代码

function iconShow() {
  var scrollTimer = null;
  function debounce(fn, wait) {
    return function () {
      if (scrollTimer !== null) {
        clearTimeout(scrollTimer);
      }
      $(".top").fadeIn(); //元素出现
      scrollTimer = setTimeout(fn, wait);
    }
  }
  function handle() {
    scrollTimer = null;
    $(".top").fadeOut(); //元素消失
  }
  window.addEventListener('scroll', debounce(handle, 2500));
}
iconShow();
posted @ 2020-09-23 14:21  ZerlinM  阅读(444)  评论(0编辑  收藏  举报