@@ -36,7 +36,7 @@ const swup = new Swup({
3636## Markup
3737
3838In this example, we want to slide in the new ` main ` element while sliding out the previous ` main `
39- element:
39+ element. The markup for parallel animations isn't any different from normal animations.
4040
4141``` html
4242<section >
@@ -49,25 +49,51 @@ element:
4949</section >
5050```
5151
52+ ### State during the animation
53+
54+ During the animation, both containers will be in the DOM at the same time.
55+ Swup has inserted the next container, will wait for any animations to finish, and
56+ then remove the previous container.
57+
58+ Note: the next container is always inserted ** before** the previous one, which
59+ is marked as hidden from screen readers.
60+
61+ ``` html
62+ <section >
63+ <header >
64+ My Website
65+ </header >
66+ <!-- Next container -->
67+ <main id =" swup" class =" transition-slide is-next-container" >
68+ <h1 >Next page</h1 >
69+ </main >
70+ <!-- Previous container -->
71+ <main id =" swup" class =" transition-slide is-previous-container" aria-hidden =" true" >
72+ <h1 >Previous page</h1 >
73+ </main >
74+ </section >
75+ ```
76+
5277## Styling
5378
79+ ### Basic styling for parallel animations
80+
5481Showing both the previous and current container during a page transition requires slightly more
5582complex styling than usual. The containers need to be layered on top of each other while they're
56- both in the DOM. The details depend on the specific layout, however the easiest way in most cases
57- is a CSS grid with both containers claiming the same row and column.
83+ both in the DOM.
84+
85+ The details depend on the specific layout, however the easiest way in most cases
86+ is a CSS ` grid ` with both containers claiming the same row and column.
87+ This type of layout avoids messy absolute positioning and scroll offsets.
5888
5989``` css
6090/* Layout */
6191
6292section {
6393 display : grid ;
94+ overflow : hidden ;
6495 grid-template-areas : " header"
6596 " main" ;
66- overflow : hidden ;
67- }
68-
69- section > * {
70- min-width : 0 ;
7197}
7298
7399section > header {
@@ -77,27 +103,61 @@ section > header {
77103section > main {
78104 grid-area : main;
79105}
106+ ```
80107
81- /* Slide transition */
108+ ### Defining the animations
82109
110+ Instead of using swup's default classes for timing
111+ the animation, parallel animations can be controlled using the classes ` is-previous-container ` and ` is-next-container ` on the containers themselves.
112+
113+ ``` css
114+ /* Parallel animation timing */
83115.is-changing .transition-slide {
84116 transition : transform 0.3s ease-in-out , opacity 0.3s ease-in-out ;
85117}
86118
119+ /* Style of next container when done */
87120.transition-slide.is-next-container {
88121 transform : translateX (100% );
89122 opacity : 0 ;
90123}
91124
125+ /* Style of previous container when done */
92126.transition-slide.is-previous-container {
93127 transform : translateX (-100% );
94128 opacity : 0 ;
95129}
96130```
97131
132+ ## Timing
133+
134+ Technically, the plugin will skip the out-animation, add the next container, wait for animations to
135+ finish, then remove the previous container. All animations now happen in the in-phase of the
136+ lifecycle, after the ` content:replace ` hook that normally marks the middle of the animation
137+ process. Any containers that are not animated or not animated in parallel (e.g. a static header)
138+ will be replaced at the start of the parallel animation.
139+
98140## Options
99141
100142### containers
101143
102144The containers that are visible at the same time. Usually only the main content container. Must be
103- a container normally replaced by swup.
145+ a container normally replaced by swup. If not specified, defaults to running all
146+ animations in parallel.
147+
148+ ## API
149+
150+ ### Opting out of parallel animations
151+
152+ The plugin will set a flag on the global context, indicating the current visit
153+ as a parallel animation: ` context.animation.parallel ` . You can unset this flag
154+ to fall back to a normal animation with leave and enter in series.
155+
156+ ``` js
157+ // Disable parallel animations for this visit
158+ swup .hooks .on (' visit:start' , (context ) => {
159+ if (someCondition) {
160+ context .animation .parallel = false ;
161+ }
162+ });
163+ ```
0 commit comments