Skip to content

Commit 5bd1f25

Browse files
committed
Fixing scroll
1 parent e40a261 commit 5bd1f25

File tree

5 files changed

+87
-6
lines changed

5 files changed

+87
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<html>
2+
<head>
3+
<style>
4+
body {
5+
margin: 0;
6+
font-family: sans-serif;
7+
}
8+
9+
.container {
10+
display: flex;
11+
gap: 10px;
12+
flex-direction: column;
13+
height: 200vh;
14+
}
15+
16+
.box {
17+
width: 100px;
18+
height: 100px;
19+
background-color: #0077ff;
20+
display: flex;
21+
align-items: center;
22+
justify-content: center;
23+
text-align: center;
24+
position: fixed;
25+
}
26+
</style>
27+
</head>
28+
<body>
29+
<div class="container">
30+
<div class="box" id="mini" style="top: 0px">mini</div>
31+
<div class="box" id="js" style="top: 100px">js</div>
32+
<div class="box" id="waapi" style="top: 200px">waapi</div>
33+
</div>
34+
<script type="module" src="/src/inc.js"></script>
35+
<script type="module">
36+
const { animateMini, animate, scroll, delay } = window.Motion
37+
38+
const options = {
39+
duration: 0.2,
40+
}
41+
42+
const miniElement = document.querySelector("#mini")
43+
const jsElement = document.querySelector("#js")
44+
const waapiElement = document.querySelector("#waapi")
45+
46+
const mini = animateMini(
47+
miniElement,
48+
{ transform: "translateX(100px)" },
49+
options
50+
)
51+
const js = animate(jsElement, { x: 100 }, options)
52+
const waapi = animate(
53+
waapiElement,
54+
{ transform: "translateX(100px)" },
55+
options
56+
)
57+
58+
const scrollOptions = {
59+
offset: ["start start", "end end"],
60+
}
61+
62+
scroll(mini, scrollOptions)
63+
scroll(js, scrollOptions)
64+
scroll(waapi, scrollOptions)
65+
</script>
66+
</body>
67+
</html>

packages/framer-motion/src/render/dom/scroll/index.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import { OnScroll, ScrollOptions } from "./types"
55

66
export function scroll(
77
onScroll: OnScroll | AnimationPlaybackControls,
8-
{
9-
axis = "y",
10-
container = document.documentElement,
11-
...options
12-
}: ScrollOptions = {}
8+
{ axis = "y", container = document.body, ...options }: ScrollOptions = {}
139
): VoidFunction {
1410
const optionsWithDefaults = { axis, container, ...options }
1511

packages/framer-motion/src/render/dom/scroll/on-scroll-handler.ts

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ function measure(
2727
}
2828
}
2929

30+
console.log(target.scrollHeight, target.clientHeight)
31+
3032
info.x.targetLength =
3133
target === container ? target.scrollWidth : target.clientWidth
3234
info.y.targetLength =

packages/framer-motion/src/render/dom/scroll/utils/get-timeline.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ export function getTimeline({
5555
: scrollTimelineFallback({ container, ...options })
5656
}
5757

58-
return targetCache[axis]!
58+
return targetCache[axisKey]!
5959
}

tests/animate/animate.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,22 @@ test.describe("animate() methods", () => {
376376
expect(style).toContain("transform: ")
377377
})
378378
})
379+
380+
test("scroll", async ({ page }) => {
381+
test.use({ viewport: { width: 500, height: 500 } })
382+
383+
await waitForAnimation("animate/animate-scroll.html", page)
384+
385+
// Scroll 250px
386+
await page.evaluate(() => {
387+
window.scrollTo(0, 250)
388+
})
389+
390+
await eachBox(page, async (box) => {
391+
const boundingBox = await box.boundingBox()
392+
expect(boundingBox?.x).toBeCloseTo(50)
393+
})
394+
})
379395
})
380396

381397
test.describe("animate() properties", () => {

0 commit comments

Comments
 (0)