Skip to content

Commit de38c3c

Browse files
committed
If a child element is navigated to, observe whether the user scrolls to the page top
1 parent 06e7d23 commit de38c3c

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/main/handlebars/journey.handlebars

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,17 @@ parent: feed
9595
9696
mapping.project(document.querySelector('#map'));
9797
98-
// Update statistics for journey after a little while. To determine which entries the
99-
// user has spent time viewing, use intersection observer
98+
// To determine which entries the user has spent time viewing, use intersection observer
10099
{{&use 'statistics'}}
101100
const statistics = new Statistics();
102-
if (!document.location.hash) {
103-
statistics.schedule(statistics.add('{{journey.slug}}', '{{sign journey.slug}}', 10000));
104-
}
105-
106101
const observer = new IntersectionObserver(
107102
(entries) => {
108103
for (const entry of entries) {
104+
const id = '{{journey.slug}}' + (entry.target.id ? '/' + entry.target.id : '');
109105
if (entry.isIntersecting) {
110-
statistics.schedule('{{journey.slug}}/' + entry.target.id);
106+
statistics.schedule(id);
111107
} else {
112-
statistics.withdraw('{{journey.slug}}/' + entry.target.id);
108+
statistics.withdraw(id);
113109
}
114110
}
115111
},
@@ -119,6 +115,14 @@ parent: feed
119115
observer.observe(document.querySelector('#{{scroll slug}}'));
120116
statistics.add('{{slug}}', '{{sign slug}}', Math.min({{size images}} * 1500, 5000));
121117
{{/each}}
118+
119+
// If a child element is navigated to, observe whether the user scrolls to the page top
120+
if (document.location.hash) {
121+
statistics.add('{{journey.slug}}', '{{sign journey.slug}}', 500)
122+
observer.observe(document.querySelector('h1'));
123+
} else {
124+
statistics.schedule(statistics.add('{{journey.slug}}', '{{sign journey.slug}}', 10000));
125+
}
122126
</script>
123127
{{/inline}}
124128
{{/layout}}

0 commit comments

Comments
 (0)