Skip to content

Commit 92cb67c

Browse files
author
Matej Lednicky
committed
fix(DIST-370): Change scroll top calculation
Scroll top offset calculation was failing for HTML pages without proper DOCTYPE definition.
1 parent 78afa84 commit 92cb67c

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

demo/behavioral-code/scroll-code.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!-- intentionally leaving out the <!DOCTYPE html> -->
22
<html lang="en">
33
<head>
44
<title>Open: scroll (via embed code)</title>
@@ -59,7 +59,7 @@ <h2>Customize the percentage</h2>
5959
const offsetTop = window.pageYOffset || document.documentElement.scrollTop
6060
const clientTop = document.documentElement.clientTop || 0
6161
const scrollTopPixels = offsetTop - clientTop
62-
const scrollTopPercentage = scrollTopPixels / document.body.clientHeight * 100
62+
const scrollTopPercentage = scrollTopPixels / document.documentElement.clientHeight * 100
6363
document.getElementById('scroll-pixels').innerHTML = scrollTopPixels;
6464
document.getElementById('scroll-percentage').innerHTML = (Math.round(scrollTopPercentage * 100) / 100).toFixed(2);
6565
})

src/core/utils/popup-auto-open.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ const openOnScroll = (popup, scrollThreshold) => {
1818
const offsetTop = window.pageYOffset || document.documentElement.scrollTop
1919
const clientTop = document.documentElement.clientTop || 0
2020
const scrollTopPixels = offsetTop - clientTop
21-
const scrollTopPercentage = scrollTopPixels / document.body.clientHeight * 100
22-
const viewportHeight = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)
23-
if (scrollTopPercentage >= scrollThreshold || scrollTopPixels + viewportHeight >= document.body.clientHeight) {
21+
const scrollTopPercentage = scrollTopPixels / document.documentElement.clientHeight * 100
22+
const scrolledToTheBottom = scrollTopPixels + window.innerHeight >= document.documentElement.clientHeight
23+
24+
if (scrollTopPercentage >= scrollThreshold || scrolledToTheBottom) {
2425
popup.open()
2526
document.removeEventListener('scroll', handleScroll)
2627
}

src/core/utils/popup-auto-open.spec.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,17 @@ describe('handleAutoOpen', () => {
8888

8989
beforeAll(() => {
9090
global.document = {
91-
body: {
92-
clientHeight: 1000
93-
},
9491
documentElement: {
95-
clientHeight: 500
92+
clientHeight: 1000
9693
},
9794
addEventListener: (_event, fn) => {
9895
handler = fn
9996
},
10097
removeEventListener
10198
}
10299
global.window = {
103-
pageYOffset: 0
100+
pageYOffset: 0,
101+
innerHeight: 500
104102
}
105103
})
106104

0 commit comments

Comments
 (0)