您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

CSS实现基于用户滚动应用(代码)

2024/4/19 23:53:08发布5次查看
本篇文章给大家带来的内容是关于css实现基于用户滚动应用(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
通过将当前滚动偏移映射到html元素上的属性,我们可以根据当前滚动位置设置页面上的元素样式。我们可以使用它来构建一个浮动导航组件。
这是我们将使用的html,<header>当我们向下滚动时,我们希望在内容之上浮动的一个很好的组件。
<header>i'm the page header</header><p>lot's of content here...</p><p>more beautiful content...</p><p>content...</p>
首先,我们将监听该'scroll'事件,document并且scrolly每次用户滚动时我们都会请求当前位置。
document.addeventlistener('scroll', () => {  document.documentelement.dataset.scroll = window.scrolly;});
我们将滚动位置存储在html元素的数据属性中。如果您使用开发工具查看dom,它将如下所示。
<html data-scroll="0">
现在我们可以使用此属性来设置页面上的元素样式。
/* make sure the header is always at least 3em high */header {  min-height: 3em;  width: 100%;  background-color: #fff;}/* reserve the same height at the top of the page as the header min-height */html:not([data-scroll='0']) body {  padding-top: 3em;}/* switch to fixed positioning, and stick the header to the top of the page */html:not([data-scroll='0']) header {  position: fixed;  top: 0;  z-index: 1;  /* this box-shadow will help sell the floating effect */  box-shadow: 0 0 .5em rgba(0, 0, 0, .5);}
基本上就是这样,当向下滚动时,标题现在将自动从页面中分离并浮动在内容之上。javascript代码并不关心这一点,它的任务就是将滚动偏移量放在数据属性中。这很好,因为javascript和css之间没有紧密耦合。
仍有一些改进,主要是在性能领域。
但首先,我们必须修复脚本,以适应页面加载时滚动位置不在顶部的情况。在这些情况下,标题将呈现错误。
页面加载时,我们必须快速获取当前滚动偏移量。这确保了我们始终与当前的事态同步。
// reads out the scroll position and stores it in the data attribute// so we can use it in our stylesheetsconst storescroll = () => {  document.documentelement.dataset.scroll = window.scrolly;}// listen for new scroll eventsdocument.addeventlistener('scroll', storescroll);// update scroll position for first timestorescroll();
接下来我们将看一些性能改进。如果我们请求该scrolly位置,浏览器将必须计算页面上每个元素的位置,以确保它返回正确的位置。如果我们不强迫它每次滚动互动都这样做是最好的。
要做到这一点,我们需要一个debounce方法,这个方法会将我们的请求排队,直到浏览器准备好绘制下一帧,此时它已经计算了页面上所有元素的位置,所以它不会再来一遍。
// the debounce function receives our function as a parameterconst debounce = (fn) => {  // this holds the requestanimationframe reference, so we can cancel it if we wish  let frame;  // the debounce function returns a new function that can receive a variable number of arguments  return (...params) => {        // if the frame variable has been defined, clear it now, and queue for next frame    if (frame) {       cancelanimationframe(frame);    }    // queue our function call for the next frame    frame = requestanimationframe(() => {            // call our function and pass any params we received      fn(...params);    });  } };// reads out the scroll position and stores it in the data attribute// so we can use it in our stylesheetsconst storescroll = () => {  document.documentelement.dataset.scroll = window.scrolly;}// listen for new scroll events, here we debounce our `storescroll` functiondocument.addeventlistener('scroll', debounce(storescroll));// update scroll position for first timestorescroll();
通过标记事件,passive我们可以告诉浏览器我们的滚动事件不会被触摸交互取消(例如与谷歌地图等插件交互时)。这允许浏览器立即滚动页面,因为它现在知道该事件不会被取消。
document.addeventlistener('scroll', debounce(storescroll), { passive: true });
本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注的css视频教程栏目!
以上就是css实现基于用户滚动应用(代码)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录