专栏名称: 大迁世界
掘金LV8,思否10万+的作者。一个热爱前端的创业者。
今天看啥  ›  专栏  ›  大迁世界

28.滚动进度条 & 带文字叠加的图像

大迁世界  · 公众号  ·  · 2024-08-25 08:35

文章预览

滚动进度条 创建一个指示页面滚动百分比的进度条。 使用 position: fixed 和一个较大的 z-index 值将元素放置在页面顶部,高于任何内容。 使用 EventTarget.addEventListener() 和 Element.scrollTop 确定文档的滚动百分比,并将其应用于元素的 width 。 < div   id = "scroll-progress" > div > body  {    min-height :  200vh ; } #scroll-progress  {    position : fixed;    top :  0 ;    width :  0% ;    height :  4px ;    background :  #7983ff ;    z-index :  10000 ; } const  scrollProgress =  document .getElementById( 'scroll-progress' ); const  height =    document .documentElement.scrollHeight -  document .documentElement.clientHeight; window .addEventListener( 'scroll' , () => {    const  scrollTop =      document .body.scrollTop ||  document .documentElement.scrollTop;   scrollProgress.style.width =  ` ${(scrollTop  / height) * 100}%`; }); 这个技巧创建了一个交 ………………………………

原文地址:访问原文地址
快照地址: 访问文章快照
总结与预览地址:访问总结与预览