【问题标题】:Hide scrollbar when not scrolling (Mac like behaviour)不滚动时隐藏滚动条(类似 Mac 的行为)
【发布时间】:2021-07-16 22:45:36
【问题描述】:

我在 Mac 浏览器上的 Vue Web 应用程序显示了非常优雅的滚动条。但是 Windows 上的同一个应用程序会因为宽度太大而破坏 UI,并且滚动始终可见。

为了解决这个问题,我从 CSS 中创建了滚动条,并添加了事件监听器以仅在滚动条开始滚动时才显示。

div * {
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none; /* IE 10+ */
    ::-webkit-scrollbar-track {
        -webkit-box-shadow: none !important;
        background-color: transparent;
    }
    ::-webkit-scrollbar {
        width: 3px !important;
        background-color: transparent;
    }
    ::-webkit-scrollbar-thumb {
        background-color: transparent;
    }
}
.on-scrollbar{
    scrollbar-width: thin; /* Firefox */
    -ms-overflow-style: none; /* IE 10+ */

  }
  
  .on-scrollbar::-webkit-scrollbar-track {
    -webkit-box-shadow: none !important;
    background-color: transparent !important;
  }
  
  .on-scrollbar::-webkit-scrollbar {
    width: 6px !important;
    background-color: transparent;
  }
  
  .on-scrollbar::-webkit-scrollbar-thumb {
    background-color: #acacac;
  }

JS:

window.addEventListener('scroll', (e) => {
    if (e.target.classList.contains("on-scrollbar") === false) {
        e.target.classList.add("on-scrollbar");
    }
}, true);

但问题是,一旦滚动条可见。不滚动时,我无法删除/隐藏滚动条。基本上我正在尝试实现我们在 Mac 上的滚动条的默认行为。 谁能帮我解决这个问题?

【问题讨论】:

    标签: javascript html css scroll scrollbar


    【解决方案1】:

    希望这会有所帮助。滚动条的一些 debounce 和 css。如果你想改变改变宽度的样式/动画,有更好的资源。祝你好运!

    function debounce(func, timeout = 300){
      let timer;
      return (...args) => {
        clearTimeout(timer);
        timer = setTimeout(() => { func.apply(this, args); }, timeout);
      };
    }
    
    window.addEventListener("mousewheel", e => {
      document.querySelector("div").classList.remove("hidden");
    });
    
    window.addEventListener("mousewheel", debounce(e => {
      document.querySelector("div").classList.add("hidden");
    }));
    div {
      background: gray;
      overflow: auto;
      height: 300px;
      width: 100%;
    }
    
    div > div {
      background: lighblue;
      height: 800px;
    }
    
    /* width */
    .shown::-webkit-scrollbar {
      width: 10px;
    }
    
    /* width */
    .hidden::-webkit-scrollbar {
      width: 0px;
    }
    
    /* Track */
    ::-webkit-scrollbar-track {
      background: black;
    }
    
    /* Handle */
    ::-webkit-scrollbar-thumb {
      background: white;
    }
    
    /* Handle on hover */
    ::-webkit-scrollbar-thumb:hover {
      background: lightblue;
    }
    <div class="shown">
      <div></div>
    </div>

    【讨论】:

      【解决方案2】:
      window.addEventListener('scroll', (e) => {
          if (e.target.classList.contains("on-scrollbar") === false) {
              e.target.classList.add("on-scrollbar");
          }else{
              setTimeout(function(){
                e.target.classList.remove("on-scrollbar");
           },2000)
          }
       }, true);
      

      【讨论】:

      • 虽然此代码可以解决问题,但including an explanation 确实有助于提高您的回答质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性 cmets 挤满你的代码,因为这会降低代码和解释的可读性!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-05
      • 1970-01-01
      • 2020-03-04
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多