搜索
首页web前端css教程CSS实现基于用户滚动应用(代码)

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

Mar 30, 2019 am 11:38 AM
css3javascript

本篇文章给大家带来的内容是关于CSS实现基于用户滚动应用(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

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

这是我们将使用的HTML,

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

<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,它将如下所示。


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

/* 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 });

本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的CSS视频教程栏目!

以上是CSS实现基于用户滚动应用(代码)的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:segmentfault。如有侵权,请联系admin@php.cn删除
有点提醒您伪元素是孩子,有点。有点提醒您伪元素是孩子,有点。Apr 19, 2025 am 11:39 AM

这里有一个带子元素的容器:

菜单具有'动态命中区域”菜单具有'动态命中区域”Apr 19, 2025 am 11:37 AM

飞翔的菜单!您需要在第二个菜单中实现悬停事件以显示更多菜单项的菜单,即您在棘手的领域中。一方面,他们应该

通过Webvtt改善视频可访问性通过Webvtt改善视频可访问性Apr 19, 2025 am 11:27 AM

“网络的力量在于其普遍性。每个人的访问无论残疾是一个重要方面。” - 蒂姆·伯纳斯 - 李

每周平台新闻:CSS :: Marker伪元素,预先渲染的Web组件,向您的网站添加Webmention每周平台新闻:CSS :: Marker伪元素,预先渲染的Web组件,向您的网站添加WebmentionApr 19, 2025 am 11:25 AM

在本周的综述中:datepickers正在让键盘用户头痛,一个新的Web组件编译器,有助于与Fouc进行战斗,我们终于获得了造型列表项目标记,以及在您的网站上获得网络攻击的四个步骤。

使宽度和灵活的物品一起玩得很好使宽度和灵活的物品一起玩得很好Apr 19, 2025 am 11:23 AM

简短的答案:您可能要寻找的是弹性折射和弹性基础。

位置粘性和桌子标头位置粘性和桌子标头Apr 19, 2025 am 11:21 AM

您可以位置:粘性;一个

每周平台新闻:HTML在搜索控制台,全局脚本范围中的HTML检查,Babel Envs添加默认查询查询每周平台新闻:HTML在搜索控制台,全局脚本范围中的HTML检查,Babel Envs添加默认查询查询Apr 19, 2025 am 11:18 AM

在本周的Web平台新闻世界中,Google搜索控制台可以更轻松地查看爬行的标记,我们了解到自定义属性

Indieweb和网络企业Indieweb和网络企业Apr 19, 2025 am 11:16 AM

Indieweb是一回事!他们将举行会议和一切。纽约客甚至在写这件事:

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。