Igor's Blog

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

, , , ,
About — I'm an enthusiastic software engineer and consultant interested in many fields including J2EE, programming, electronics, 3D printing, video games, wood working and gardening.
See my Resume for more information.
The views expressed in this blog are my own and not those of my employer.
comments powered by Disqus
My other posts you may like...
Programming, DIY, Games, Hacks, Tech and more.
Follow me on...
Current and Past Projects
See my Resume

Subscribe


RSS Feed

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer

Recent Blog Posts

Using Jersey 2.x as a shared library on WebLogic 12.1.2

Google DFP with AdSense fallback is causing infinite scroll pages to go haywire

Workaround for Mobile Safari scrollTop() not updating during scroll

Skipping execution of Maven plugins that do not have a native skip option

Unmarshalling an XML fragment representing a JAXB object without XmlRootElement

Injecting a ContainerRequestContext into a Jersey entity provider class

Obfuscating PHP source code with Maven and YAK Pro PO

Making use of corner shelves with easy to make slide out draws

Octotree breaks GitLab Issue Boards

Compiling the FHIR schema with XJC

Recent Galleries

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

Good Friday Electronics fun Easter Bunny LED PCB Kit IBEABU-01.0

Top Categories

Blogs I follow

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth

Friends

RAWS Parts Online
Alpha Dimensions Hosting
Kristensen Photography
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Follow me on... 
     
...or subscribe for updates!

Don't show this again