亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
What is @scroll-timeline scrolling timeline?
Suggestion DEMO
@scroll-timeline syntax introduction
使用 @scroll-timeline 實現(xiàn)滾動進度指示器
使用 scroll-offsets 精確控制動畫觸發(fā)時機
使用 @scroll-timeline 實現(xiàn)各類效果
@scroll-timeline 的實驗室特性與特性檢測
特性檢測
最后
Home Web Front-end CSS Tutorial Learn more about the new features of CSS animation: @scroll-timeline

Learn more about the new features of CSS animation: @scroll-timeline

Mar 18, 2022 am 10:55 AM
css animation new features

In the previous article "Several new CSS features you deserve to know in 2022 (Collection and Learning)", I briefly introduced several new CSS features. Today I will take you to understand one of them in depth. New feature (animation killer): @scroll-timeline, I hope it will be helpful to everyone!

Learn more about the new features of CSS animation: @scroll-timeline

In the CSS specification Scroll-linked Animations, an epoch-making CSS feature is introduced. That is -- The @scroll-timeline at-rule, which literally translates to Scrolling Timeline.

This article will take you through it all, from getting started to learning to use CSS @scroll-timeline. (Recommended learning: css video tutorial)

What is @scroll-timeline scrolling timeline?

What is @scroll-timeline What is the scrolling timeline?

@scroll-timeline You can set the start and end of an animation to be determined by the scrolling progress within the scroll container, not by time.

Means, We can define an animation effect, and the start and end of the animation can be controlled by scrolling the container.

Suggestion DEMO

Before systematically learning grammar, we use a DEMO to briefly understand its usage:

We first implement a simple font F Rotation animation :

<div id="g-box">F</div>
#g-box {
    animation-name: rotate;
    animation-duration: 3s;
    animation-direction: alternate;
    animation-easing-function: linear;
}
@keyframes rotate {
    0% {
        transform: rotate(0);
    }
    100% {
        transform: rotate(360deg);
    }
}

Normally, it is such a simple animation:

Learn more about the new features of CSS animation: @scroll-timeline

Next, we combine this animation with @scroll-timeline Combined, it needs to be placed in a scrollable container:

<div id="g-box">F</div>
#g-content {
    width: 300px;
    height: 170vh;
    background: #999;
}
#g-box {
    font-size: 150px;
    margin: 70vh auto 0;
    animation-name: rotate;
    animation-duration: 3s;
    animation-direction: alternate;
    animation-easing-function: linear;
    animation-timeline: box-rotate;
}
@keyframes rotate {
    0% {
        transform: rotate(0);
    }
    100% {
        transform: rotate(360deg);
    }
}
@scroll-timeline box-rotate {
    source: selector("#g-content");
}

Here, we have implemented a scrollable container #g-content, Its height is 170vh, which is 1.7 times the height of the visual interface, and the #g-box container is placed at a height of 70vh from the top :

Learn more about the new features of CSS animation: @scroll-timeline

The interesting thing is, the rotation animation we set will not start automatically. The animation will only start when we scroll down. The actual effect Gif:

Learn more about the new features of CSS animation: @scroll-timeline

CodePen Demo -- @scroll-timeline Demo

https://codepen.io/Chokcoco/pen/JjOZMaQ

Seeing this, everyone should be able to understand the role and meaning of @scroll-timeline. It gives CSS the ability to control the progress of animation based on the scrolling of the scroll bar! Amazing! !

@scroll-timeline syntax introduction

Next, let’s take a moment and take a brief look at the syntax of @scroll-timeline.

To use @scroll-timeline, the most important thing is to define a @scroll-timeline rule:

@scroll-timeline moveTimeline {
  source: selector("#g-content");
  orientation: vertical;
  scroll-offsets: 0px, 500px;
}

Among them:

  • source:綁定觸發(fā)滾動動畫的滾動容器
    • source: auto:綁定到 Document,也就是全局 Windows 對象
    • source: selector("id-selector"),通過 selector(),內(nèi)置一個 #id 選擇器,選取一個可滾動容器
    • source: none:不指的滾動容器
  • orientation:設(shè)定滾動時間線的方向
    • orientation: auto:默認為 vertical,也就是豎直方向的滾動
    • orientation: vertical:豎直方向的滾動
    • orientation: horizontal:水平方向的滾動
    • orientation: block:不太常用,使用沿塊軸的滾動位置,符合書寫模式和方向性
    • orientation: inline:不太常用,使用沿內(nèi)聯(lián)軸的滾動位置,符合書寫模式和方向性
  • scroll-offsets:滾動時間線的核心,設(shè)定在滾動的什么階段,觸發(fā)動畫,可通過三種方式之一進行設(shè)置:
    • scroll-offsets: none 這意味著沒有 scroll-offset 指定。
    • 由逗號分隔的 length-percentage 值列表確定。每個值都映射到animation-duration。例如,如果 ananimation-duration 設(shè)置為 2s 且滾動偏移量為 0px, 30px, 100px,則在 1s 時,滾動偏移量將為 30px。
    • 第三種確定滾動偏移量的方法是使用元素偏移量。這意味著可以指定頁面內(nèi)的元素,其位置決定了滾動時間線以及要使用這些元素的哪個邊緣。指定元素是使用 selector() 函數(shù)完成的,該函數(shù)接收元素的 id。邊緣由關(guān)鍵字 start 或確定 end。可選的閾值的 0–1 可用于表示元素滾動中預(yù)期可見的百分比。

scroll-offsets 的理解會比較困難,我們稍后詳述。

在設(shè)定了一個 @scroll-timeline 之后,我們只需要將它和動畫綁定起來即可,通過 animation-timeline

@scroll-timeline moveTimeline {
  source: selector("#g-content");
  orientation: vertical;
  scroll-offsets: 0px, 500px;
}
div {
    animation-name: move;
    animation-duration: 3s;
    animation-timeline: moveTimeline;
}
@keyframes move{
    0% {
        transform: translate(0, 0);
    }
    100% {
        transform: translate(100%, 0);
    }
}

使用 @scroll-timeline 實現(xiàn)滾動進度指示器

之前在 不可思議的純 CSS 滾動進度條效果 一文中,我們介紹了一種使用漸變實現(xiàn)的純 CSS 滾動進度指示器效果:

Learn more about the new features of CSS animation: @scroll-timeline

該方法有些小小的瑕疵。其中一個就是當滾動距離太短的時候,進度條右側(cè)會有明顯的斜邊效果。

而有了 @scroll-timeline 之后,我們終于可以將滾動和動畫這兩個元素綁定起來,再實現(xiàn)滾動進度指示器,就已經(jīng)非常輕松了:

<div id="g-container">
    <p>...文本內(nèi)容...</p>
</div>
#g-container {
    width: 100vw;
}
#g-container::before {
    content: "";
    position: fixed;
    height: 5px;
    left: 0;
    top: 0;
    right: 0;
    background: #ffc107;
    animation-name: scale;
    animation-duration: 1s;
    animation-fill-mode: forwards;
    animation-timeline: box-rotate;
    transform-origin: 0 50%;
}

@keyframes scale {
    0% {
        transform: scaleX(0);
    }
    100% {
        transform: scaleX(1);
    }
}
@scroll-timeline box-rotate {
    source: auto;
    orientation: vertical;
}

1、我們在頁面最上方,通過一個偽元素,實現(xiàn)一個占滿屏幕 100%5px 高的進度條。正常而言是這樣:

Learn more about the new features of CSS animation: @scroll-timeline

2、通過設(shè)定一個 transform: scaleX(0)transform: scaleX(1) 的動畫,并且將它與 body 的滾動相綁定,即可得到滾動指示器,效果如下:

Learn more about the new features of CSS animation: @scroll-timeline

完整的代碼,你可以戳這里:CodePen Demo - 使用 @scroll-timeline 實現(xiàn)滾動進度條

https://codepen.io/Chokcoco/pen/eYeKLMj

使用 scroll-offsets 精確控制動畫觸發(fā)時機

大家可以再看看上面的 Gif 圖,都有一個問題,就是動畫的開始時間都是從滾動一開始就開始了,剛好在滾動結(jié)束時結(jié)束。那么如果我希望動畫在滾動的特定階段觸發(fā),那該怎么辦呢?

這里,就需要借助 scroll-offsets,去更加精確的控制我們的動畫。

在滾動過程中,我們可以將一個元素,劃分為 3 個區(qū)域:

  • 滾動過程中,從上方視野盲區(qū),進入視野
  • 滾動過程中,處于視野中
  • 滾動過程中,從視野中,進入下方視野盲區(qū)

在這里,我們就可以得到兩個邊界,上方邊界,下方邊界:

Learn more about the new features of CSS animation: @scroll-timeline

而對于上下兩個邊界,又會有兩種狀態(tài)。以上邊界為例子,會有:

  • 元素剛剛開始進入可視區(qū)
  • 元素完全進入可視區(qū)

對于這兩種狀態(tài),我們用 start 0start 1 表示,同理,下方的邊界也可以用 end 0end 1 表示:

Learn more about the new features of CSS animation: @scroll-timeline

這里的 0 和 1 實際表示的是,元素滾動中預(yù)期可見的百分比。

有了這些狀態(tài)值,配合 scroll-offsets,我們就可以精確控制滾動動畫的觸發(fā)時間。

我們設(shè)定一個從左向右并且伴隨透明度變化的動畫,的看看下面幾種情況:

1、滾動動畫在元素從下方開始出現(xiàn)時開始,完全出現(xiàn)后截止。

動畫運行范圍:end 0 --> end 1

@keyframes move {
    0% {
        transform: translate(-100%, 0);
        opacity: 0;
    }
    100% {
        transform: translate(0, 0);
        opacity: 1;
    }
}
@scroll-timeline box-move {
    source: auto;
    orientation: "vertical";
    scroll-offsets: 
        selector(#g-box) end 0, 
        selector(#g-box) end 1;

    /* Legacy Descriptors Below: */
    start: selector(#g-box) end 0;
    end: selector(#g-box) end 1;
    time-range: 1s;
}
#g-box {
    animation-name: move;
    animation-duration: 3s;
    animation-fill-mode: both;
    animation-timeline: box-move;
}

效果如下:

Learn more about the new features of CSS animation: @scroll-timeline

2、滾動動畫在元素從下方完全出現(xiàn)時開始,在滾動到上方即將離開屏幕后截止:

動畫運行范圍:end 1 --> start 1

// ...
@scroll-timeline box-move {
    source: auto;
    orientation: "vertical";
    scroll-offsets: 
        selector(#g-box) end 1, 
        selector(#g-box) start 1;

    /* Legacy Descriptors Below: */
    start: selector(#g-box) end 1;
    end: selector(#g-box) start 1;
    time-range: 1s;
}
// ...

效果如下:

Learn more about the new features of CSS animation: @scroll-timeline

3、滾動動畫在元素滾動到上方即將離開屏幕后開始,完全離開屏幕后截止:

動畫運行范圍:start 1 --> start 0

// ...
@scroll-timeline box-move {
    source: auto;
    orientation: "vertical";
    scroll-offsets: 
        selector(#g-box) start 1, 
        selector(#g-box) start 0;

    /* Legacy Descriptors Below: */
    start: selector(#g-box) start 1;
    end: selector(#g-box) start 0;
    time-range: 1s;
}
// ...

效果如下:

1Learn more about the new features of CSS animation: @scroll-timeline

掌握 scroll-offsets 的用法是靈活運用滾動時間線的關(guān)鍵,當然,在上面你還會看到 start: selector(#g-box) start 1end: selector(#g-box) start 0 這種寫法,這是規(guī)范歷史遺留問題,最新的規(guī)范已經(jīng)使用了 scroll-offsets 去替代 start: end: 的寫法。

把上述 3 種情況放在一起,再比較比較:

1Learn more about the new features of CSS animation: @scroll-timeline

完整的代碼,你可以戳這里:CodePen Demo - @scroll-timeline Demo | element-based offset

使用 @scroll-timeline 實現(xiàn)各類效果

在能夠掌握 @scroll-timeline 的各個語法之后,我們就可以開始使用它創(chuàng)造各種動畫效果了。

譬如如下的,滾動內(nèi)容不斷劃入:

1Learn more about the new features of CSS animation: @scroll-timeline

代碼較長,可以戳這里,來自 bramus 的 Codepen CodePen Demo -- Fly-in Contact List (CSS @scroll-timeline version)

https://codepen.io/bramus/pen/bGwJVzg

甚至可以結(jié)合 scroll-snap-type 制作一些全屏滾動的大屏特效動畫:

1Learn more about the new features of CSS animation: @scroll-timeline

要知道,這在以前,是完全不可能利用純 CSS 實現(xiàn)的。完整的代碼你可以戳這里:CodePen Demo -- CSS Scroll-Timeline Split Screen Carousel

https://codepen.io/Chokcoco/pen/QWOrPdM

簡而言之,任何動畫效果,如今,都可以和滾動相結(jié)合起來,甚至乎是配合 SVG 元素也不例外,這里我還簡單改造了一下之前的一個 SVG 線條動畫:

1Learn more about the new features of CSS animation: @scroll-timeline

完整的代碼你可以戳這里:CodePen Demo -- SVG Text Line Effect | Scroll Timeline

https://codepen.io/Chokcoco/pen/wvPxbRm

@scroll-timeline 的實驗室特性與特性檢測

@scroll-timeline 雖好,目前仍處于實驗室特性時間,Chrome 從 85 版本開始支持,但是默認是關(guān)閉的。

兼容性如下(2022-03-07):

1Learn more about the new features of CSS animation: @scroll-timeline

在最新的 chrome、Edge、Opera 可以通過瀏覽器配置開啟該特性,Chrome 下開啟該特性需要:

  • 瀏覽器 URL 框輸入 chrome://flags

  • 開啟 #enable-experimental-web-platform-features

美酒雖好,但是離完全能用,瀏覽器大規(guī)模支持還需要等待一會,給時間一點時間吧!

特性檢測

基于目前的兼容性問題,我們可以通過瀏覽器的特性檢測 @supports 語法,來漸進增強使用該功能。

特性檢測的語法也非常簡單:

@supports (animation-timeline: works) {
    @scroll-timeline list-item-1 {
	source: selector(#list-view);
	start: selector(#list-item-1) end 0;
	end: selector(#list-item-1) end 1;
        scroll-offsets:
            selector(#list-item-1) end 0,
            selector(#list-item-1) end 1
        ;
	time-range: 1s;
    }
    // ...
}

通過 @supports (animation-timeline: works) {} 可以判斷瀏覽器是否支持 @scroll-timeline。

最后

目前關(guān)于 @scroll-timeline 的相關(guān)介紹還非常少,但是它確是能夠改變 CSS 動畫的一個非常大的革新。隨著兼容性的逐漸普及,未來勢必會在 CSS 中占據(jù)一席之地。

本文到此結(jié)束,希望對你有幫助 :)

(學(xué)習(xí)視頻分享:web前端

The above is the detailed content of Learn more about the new features of CSS animation: @scroll-timeline. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP to build social sharing functions PHP sharing interface integration practice How to use PHP to build social sharing functions PHP sharing interface integration practice Jul 25, 2025 pm 08:51 PM

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy Jul 25, 2025 pm 08:27 PM

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

What are common CSS browser inconsistencies? What are common CSS browser inconsistencies? Jul 26, 2025 am 07:04 AM

Different browsers have differences in CSS parsing, resulting in inconsistent display effects, mainly including the default style difference, box model calculation method, Flexbox and Grid layout support level, and inconsistent behavior of certain CSS attributes. 1. The default style processing is inconsistent. The solution is to use CSSReset or Normalize.css to unify the initial style; 2. The box model calculation method of the old version of IE is different. It is recommended to use box-sizing:border-box in a unified manner; 3. Flexbox and Grid perform differently in edge cases or in old versions. More tests and use Autoprefixer; 4. Some CSS attribute behaviors are inconsistent. CanIuse must be consulted and downgraded.

How to build a PHP Nginx environment with MacOS to configure the combination of Nginx and PHP services How to build a PHP Nginx environment with MacOS to configure the combination of Nginx and PHP services Jul 25, 2025 pm 08:24 PM

The core role of Homebrew in the construction of Mac environment is to simplify software installation and management. 1. Homebrew automatically handles dependencies and encapsulates complex compilation and installation processes into simple commands; 2. Provides a unified software package ecosystem to ensure the standardization of software installation location and configuration; 3. Integrates service management functions, and can easily start and stop services through brewservices; 4. Convenient software upgrade and maintenance, and improves system security and functionality.

Describe the `vertical-align` property and its typical use cases Describe the `vertical-align` property and its typical use cases Jul 26, 2025 am 07:35 AM

Thevertical-alignpropertyinCSSalignsinlineortable-cellelementsvertically.1.Itadjustselementslikeimagesorforminputswithintextlinesusingvalueslikebaseline,middle,super,andsub.2.Intablecells,itcontrolscontentalignmentwithtop,middle,orbottomvalues,oftenu

What is the accent-color property? What is the accent-color property? Jul 26, 2025 am 09:25 AM

accent-color is an attribute used in CSS to customize the highlight colors of form elements such as checkboxes, radio buttons and sliders; 1. It directly changes the default color of the selected state of the form control, such as changing the blue check mark of the checkbox to red; 2. Supported elements include input boxes of type="checkbox", type="radio" and type="range"; 3. Using accent-color can avoid complex custom styles and extra DOM structures, and maintain native accessibility; 4. It is generally supported by modern browsers, and old browsers need to be downgraded; 5. Set accent-col

How to compile SCSS to CSS? How to compile SCSS to CSS? Jul 27, 2025 am 01:58 AM

InstallDartSassvianpmafterinstallingNode.jsusingnpminstall-gsass.2.CompileSCSStoCSSusingthecommandsassinput.scssoutput.css.3.Usesass--watchinput.scssoutput.csstoauto-compileonsave.4.Watchentirefolderswithsass--watchscss:css.5.Usepartialswith_prefixfo

How to change text color in CSS? How to change text color in CSS? Jul 27, 2025 am 04:25 AM

To change the text color in CSS, you need to use the color attribute; 1. Use the color attribute to set the text foreground color, supporting color names (such as red), hexadecimal codes (such as #ff0000), RGB values (such as rgb(255,0,0)), HSL values (such as hsl(0,100%,50%)), and RGBA or HSLA with transparency (such as rgba(255,0,0,0.5)); 2. You can apply colors to any element containing text, such as h1 to h6 titles, paragraph p, link a (note the color settings of different states of a:link, a:visited, a:hover, a:active), buttons, div, span, etc.; 3. Most

See all articles