温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用css实现基于用户滚动应用

发布时间:2022-02-28 15:29:36 来源:亿速云 阅读:126 作者:小新 栏目:web开发

这篇文章主要介绍怎么用css实现基于用户滚动应用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

  通过将当前滚动偏移映射到html元素上的属性,我们可以根据当前滚动位置设置页面上的元素样式。我们可以使用它来构建一个浮动导航组件。

  这是我们将使用的HTML,<header>当我们向下滚动时,我们希望在内容之上浮动的一个很好的组件。

  <header>I'mthepageheader</header>

  <p>Lot'sofcontenthere...</p>

  <p>Morebeautifulcontent...</p>

  <p>Content...</p>

  首先,我们将监听该'scroll'事件,document并且scrollY每次用户滚动时我们都会请求当前位置。

  document.addEventListener('scroll',()=>{

  document.documentElement.dataset.scroll=window.scrollY;

  });

  我们将滚动位置存储在html元素的数据属性中。如果您使用开发工具查看DOM,它将如下所示。

  <htmldata-scroll="0">

  现在我们可以使用此属性来设置页面上的元素样式。

  /*Makesuretheheaderisalwaysatleast3emhigh*/

  header{

  min-height:3em;

  width:100%;

  background-color:#fff;

  }

  /*Reservethesameheightatthetopofthepageastheheadermin-height*/

  html:not([data-scroll='0'])body{

  padding-top:3em;

  }

  /*Switchtofixedpositioning,andsticktheheadertothetopofthepage*/

  html:not([data-scroll='0'])header{

  position:fixed;

  top:0;

  z-index:1;

  /*Thisbox-shadowwillhelpsellthefloatingeffect*/

  box-shadow:00.5emrgba(0,0,0,.5);

  }

  基本上就是这样,当向下滚动时,标题现在将自动从页面中分离并浮动在内容之上。JavaScript代码并不关心这一点,它的任务就是将滚动偏移量放在数据属性中。这很好,因为JavaScript和CSS之间没有紧密耦合。

  仍有一些改进,主要是在性能领域。

  但首先,我们必须修复脚本,以适应页面加载时滚动位置不在顶部的情况。在这些情况下,标题将呈现错误。

  页面加载时,我们必须快速获取当前滚动偏移量。这确保了我们始终与当前的事态同步。

  //Readsoutthescrollpositionandstoresitinthedataattribute

  //sowecanuseitinourstylesheets

  conststoreScroll=()=>{

  document.documentElement.dataset.scroll=window.scrollY;

  }

  //Listenfornewscrollevents

  document.addEventListener('scroll',storeScroll);

  //Updatescrollpositionforfirsttime

  storeScroll();

  接下来我们将看一些性能改进。如果我们请求该scrollY位置,浏览器将必须计算页面上每个元素的位置,以确保它返回正确的位置。如果我们不强迫它每次滚动互动都这样做是最好的。

  要做到这一点,我们需要一个debounce方法,这个方法会将我们的请求排队,直到浏览器准备好绘制下一帧,此时它已经计算了页面上所有元素的位置,所以它不会再来一遍。

  //Thedebouncefunctionreceivesourfunctionasaparameter

  constdebounce=(fn)=>{

  //ThisholdstherequestAnimationFramereference,sowecancancelitifwewish

  letframe;

  //Thedebouncefunctionreturnsanewfunctionthatcanreceiveavariablenumberofarguments

  return(...params)=>{

  //Iftheframevariablehasbeendefined,clearitnow,andqueuefornextframe

  if(frame){

  cancelAnimationFrame(frame);

  }

  //Queueourfunctioncallforthenextframe

  frame=requestAnimationFrame(()=>{

  //Callourfunctionandpassanyparamswereceived

  fn(...params);

  });

  }

  };

  //Readsoutthescrollpositionandstoresitinthedataattribute

  //sowecanuseitinourstylesheets

  conststoreScroll=()=>{

  document.documentElement.dataset.scroll=window.scrollY;

  }

  //Listenfornewscrollevents,herewedebounceour`storeScroll`function

  document.addEventListener('scroll',debounce(storeScroll));

  //Updatescrollpositionforfirsttime

  storeScroll();

  通过标记事件,passive我们可以告诉浏览器我们的滚动事件不会被触摸交互取消(例如与谷歌地图等插件交互时)。这允许浏览器立即滚动页面,因为它现在知道该事件不会被取消。

  document.addEventListener('scroll',debounce(storeScroll),{passive:true});

以上是“怎么用css实现基于用户滚动应用”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css
AI