Skip to content

Commit 8b6eab7

Browse files
authored
[scroll-animations] Add css syntax for element-based offset (#5264)
Add css syntax to for element-based offsets. Fixes #4337. The element-based syntax is simply applied when the value starts with `selector(#id)` with the following characteristics: - `selector( <<id-selector>> )` is required and is expected to be the first value. - both edge and threshold are optional can can be provided in any order. I followed some of the ideas mentioned by @tabatkins in #4348 to to get to a more ergonomic css function syntax. In particular there is no comma and the optional params can be in any order. Note that unlike #4348 we are not adding a function syntax.
1 parent ede56bd commit 8b6eab7

1 file changed

Lines changed: 62 additions & 4 deletions

File tree

scroll-animations-1/Overview.bs

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ if (window.matchMedia('(prefers-reduced-motion: no-preference)').matches) {
666666
});
667667

668668
const slideIn = target.animate({
669-
transform: ['translateX(0)',q 'translateX(50vw)'],
669+
transform: ['translateX(0)', 'translateX(50vw)'],
670670
opacity: [0, 1]
671671
}, {
672672
timeline:timeline,
@@ -676,6 +676,37 @@ if (window.matchMedia('(prefers-reduced-motion: no-preference)').matches) {
676676
}
677677
</pre>
678678

679+
The same logic can be done in CSS markup:
680+
<pre class="lang-css">
681+
@media (prefers-reduced-motion: no-preference) {
682+
683+
@keyframes slide-in {
684+
from {
685+
transform: translateX(0);
686+
opacity: 0;
687+
}
688+
689+
to {
690+
transform: translateX(50vw);
691+
opacity: 1;
692+
}
693+
}
694+
695+
@scroll-timeline image-in-scrollport {
696+
source: selector(#container);
697+
start: selector(#image) end;
698+
end: selector(#image) start;
699+
}
700+
701+
#target {
702+
animation-name: slide-in;
703+
animation-duration: 1s;
704+
animation-timeline: image-in-scrollport;
705+
}
706+
707+
}
708+
</pre>
709+
679710

680711
</div>
681712

@@ -843,7 +874,7 @@ Issue(5167): This will likely change in the future.
843874
<pre class='descdef'>
844875
Name: source
845876
For: @scroll-timeline
846-
Value: selector(<<id-selector>>) | auto | none
877+
Value: selector( <<id-selector>> ) | auto | none
847878
Initial: auto
848879
</pre>
849880

@@ -883,17 +914,44 @@ instead of document's scrolling Element for <code>auto</code>.
883914
<pre class='descdef'>
884915
Name: start
885916
For: @scroll-timeline
886-
Value: auto | <<length-percentage>>
917+
Value: <<scroll-timeline-offset>>
887918
Initial: auto
888919
</pre>
889920

890921
'start' descriptor determines the scroll timeline's {{start}}.
891922

923+
[=Scroll timeline offsets=] in CSS are represented by the
924+
<<scroll-timeline-offset>> type:
925+
926+
<pre>
927+
<dfn>&lt;scroll-timeline-offset></dfn> = auto | <<length-percentage>> | <<element-offset>>
928+
<dfn>&lt;element-offset></dfn> = selector( <<id-selector>> ) [<<element-offset-edge>> || <<number>>]?
929+
<dfn>&lt;element-offset-edge></dfn> = start | end
930+
</pre>
931+
932+
The offset type depends on the value of <<scroll-timeline-offset>> per
933+
following:
934+
<div class="switch">
935+
: If value is "auto" or of type <<length-percentage>>
936+
:: The [=scroll timeline offset=] is a [=container-based offset=] with the same
937+
value.
938+
939+
: If value is of type <<element-offset>>
940+
:: The [=scroll timeline offset=] is an [=element-based offset=] with the
941+
following member values:
942+
* {{ElementBasedOffset/target}} is the element identified by
943+
<<id-selector>>.
944+
* {{ElementBasedOffset/edge}} is the optional value of
945+
<<element-offset-edge>>. If not provided it defaults to "start".
946+
* {{ElementBasedOffset/threshold}} is the optional value <<number>>. If
947+
not provided it defaults to 0.
948+
949+
</div>
892950

893951
<pre class='descdef'>
894952
Name: end
895953
For: @scroll-timeline
896-
Value: auto | <<length-percentage>>
954+
Value: <<scroll-timeline-offset>>
897955
Initial: auto
898956
</pre>
899957

0 commit comments

Comments
 (0)