基于用户转动运用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 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 });

CodePen

    原文作者:Arthur
    原文地址: https://segmentfault.com/a/1190000018160245
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞