Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

I've recently discovered that on Mobile Safari the $(window).scrollTop() would not update during the scroll event animation. It appeared to only update after the scroll (the elastic scroll) had finished its animation. This was a rather annoying 'feature' for me because I needed to update my UI during the animation, so I went looking for a workaround and here's what I've come up with.

Below are two videos showing the before and after the workaround. The first video shows the default behaviour where scrollTop() is only updated after the scroll event completes, the second video shows a workaround that has continuous updates during scrolling.



This was my 'before' code:
 Before Code
<!DOCTYPE html>
<html lang="en">
<head>
<title>ScrollTop test</title>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<style>
div#message {
position: fixed;
right: 0;
top: 0;
color: red;
font-size: 300%;
}
</style>
</head>
<body>
<div id="body">
<div id="message"></div>
</div>
<div id="content" style="display: none; margin: 1em;"></div>
<script>
$(document).ready(function() {
/* generate some content */
for (var i = 0; i < 100; i++) {
$('#content').clone().text('Content item ' + i).show().appendTo($('#body'));
}
$(document).scroll(function(e) {
var scrollTop = $(window).scrollTop();
$('#message').text('Scroll Top: ' + scrollTop);
});
});
</script>
</body>
</html>




The main part of the code was the $(document).scroll() event handler. There the $(window).scrollTop() is looked up and set as text for display. Nothing fancy. That was the code for the first video.

My workaround to make continuous scrolling work added a couple of modifications...

First I set my div#body style so that it stretched to the size of the viewport, like so:
 CSS
div#body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
}


The second modification was to change the $(document).scroll() to this code:
 JavaScript
$('#body').scroll(function(e) {
var scrollTop = $(e.target).scrollTop();
$('#message').text('Scroll Top: ' + scrollTop);
});


The workaround responds to scroll events on the div#body instead and looks up scrollTop() on that DIV element instead of on the window object. This way continuous scroll events can be detected. The modified code was used in the second video.

You can get the source code for the before and after code on my GitHub - before code and after code.

-i

Please leave your comments or feedback below!
comments powered by Disqus
Other posts you may like...
Hi! You can search my blog here ⤵
Or browse the recent top tags...

Recent Blog Posts

WebLogic Maven Plugin - How to fix the MojoExecutionException: The artifact location was not specified

jPhotoFrame version 0.4 released with a whole new layout engine

Upcycling a couple of old broken lamps to create something amazing

A custom exception mapper and writer for a RESTful JAX-RS Jersey service

How to fix Plex error - Sorry there was a problem playing this item

Jersey JAX-RS filters and interceptors execution order for a POST request

Fix your Mac - users not showing on the macOS login screen when FileVault is enabled

BMB-012 Nanoblock T-Rex Skeleton Model assembly

Writing a custom MessageBodyReader to process POST body data with Jersey

How to make Skype for Business responsive again on macOS

Recent Galleries

BMB-012 Nanoblock T-Rex Skeleton Model assembly

Tiny Arcade revision 6 kit assembly and decal application

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 1 - introduction and case disassembly

Building a custom Atari Lynx game box storage shelf unit in a day

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

Monument Valley 2 is released and does not disappoint

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity