require("https://cdn.jsdelivr.net/npm/juxtaposejs@1.1.6/build/js/juxtapose.min.js")
.catch(() => null)
Before-and-after image demo
Driven by juxtapose.js
Step 1: add the CSS to the frontmatter
Your YAML frontmatter should include the library’s CSS (note how the examples on its GitHub README include a <link rel="stylesheet">
tag!):
css:
- https://cdn.knightlab.com/libs/juxtapose/latest/css/juxtapose.css
Step 2: Importing the module
Let’s try it out. JuxtaposeJS is on jsDelivr, so we can just import it! It’s a little stubborn, but requiring the individual file, rather than just the package name, seems to work (thanks, Observable Module Require Debugger!). It produces an error but seems to work, so I just catch
the error and ignore it:
Step 3: Write the Juxtapose!
And here’s the Juxtapose code! Once the library’s loaded, you can just write it with HTML, as per the README:
::: {.juxtapose data-startingposition="35%" style="margin-bottom:2em"}
{data-credit="Fabian Mardi on Unsplash" data-label="Image A by Fabian Mardi on Unsplash"}
{data-credit="Weyne Yew on Unsplash" data-label="Image B by Weyne Yew on Unsplash"}
:::
Nice! I’ve added a bit of extra margin-bottom
to it so that this text clears it properly too.