网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

CSS实现基于用户滚动应用的方法

小编给大家分享一下CSS实现基于用户滚动应用的方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

专注于为中小企业提供成都做网站、成都网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业大兴免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了数千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

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

这是我们将使用的HTML,

当我们向下滚动时,我们希望在内容之上浮动的一个很好的组件。

I'm the page header

Lot's of content here...

More beautiful content...

Content...

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

document.addEventListener('scroll', () => {
  document.documentElement.dataset.scroll = window.scrollY;
});

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

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

/* 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 stylesheets
const storeScroll = () => {
  document.documentElement.dataset.scroll = window.scrollY;
}

// Listen for new scroll events
document.addEventListener('scroll', storeScroll);

// Update scroll position for first time
storeScroll();

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

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

// The debounce function receives our function as a parameter
const 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 stylesheets
const storeScroll = () => {
  document.documentElement.dataset.scroll = window.scrollY;
}

// Listen for new scroll events, here we debounce our `storeScroll` function
document.addEventListener('scroll', debounce(storeScroll));

// Update scroll position for first time
storeScroll();

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

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

以上是CSS实现基于用户滚动应用的方法的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


当前标题:CSS实现基于用户滚动应用的方法
文章起源:http://bjjierui.cn/article/ghohcg.html

其他资讯