Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On mobile phone, detect when user scrolls "past" top of screen

Imagine a mobile webpage with a navigation bar at the very top of the page.

Using javascript/jQuery I'd like to be able to detect when a user scrolls "past" the top of the screen.

Let me try to explain: Imagine that the webpage just loaded and you see the navigation bar at the very top of your screen. Now, you put your finger on the screen and drag down. On an iPhone, this will look something like:

enter image description here

I'm looking for something similar to the following:

$(document).ready(function () {
  $(window).scroll(function (event) {

    var y = $(this).scrollTop();
    if(y < -20)) {
        //do something
    }

Unfortunately, this won't work on Android phones because they don't have the same elastic behavior as iPhones:

like image 354
Nosrettap Avatar asked May 27 '14 16:05

Nosrettap


Video Answer


1 Answers

The bad news

So, let's start with the bad news: there is no way to get the information you wish for natively. Why? Because once you get into the 'elastic' zone you're dragging the entire webview component down rather than just the content of the document like in your pseudo code.

What does this practically mean? Emulation or Native wrapping

Emulation, the choices

So you will have to run your own solution and you will have to make a couple of choices. First of all if you wish to use elastic scrolling you should note that this is patented by Apple Inc. in patent US7469381 B2. Please go through this patent carefully to ensure you won't be infringing (even if you're building an iOS only app this does not change anything).

Secondly the question is whether you really want to emulate a native experience. There is a big lobby against native experience emulation due to 1) a believe that it can't ever be perfect enough and 2) as operating systems and browser change your code will stay out of date and thus look terrible/weird or possible can even cause entirely unexpected behaviour/combinations.

Secondly you will have to decide whether you wish for the same elastic behaviour on android or whether you wish to give a more native like experice on android. Personally I believe it makes for some excellent UX, so I wouldn't explicitedly disadvice you from using an elastic effect, however it is something you should consider carefully.

Emulation, the scripts

Most scripts that provide similar emulation to what you want are "pull to refresh" scripts. Depending on your specific wishes you should simply use one of those scripts and either alter them or use some CSS to hide the message inside them.

  • Hook.js - Not perfect emulation of the native experience and uses the old iOS background, but a pretty good option none the less, to hide the spinner just use

    .hook-spinner{
         display:none;
    }
    
  • iScroll.js - Very well developed code and behaves excellently. Disadvantage is that it provides a lot more than what you're looking for which might be either a good thing or a bad thing. You can find a sample implementation of pull to refresh behaviour here, but do note it's for an older version of iScroll, in the last version the example seems not to have been implemented, but it should be quite similar. Just look at the code to see how to remove the pull to refresh message.

  • jQuery scrollz - Just one more option. Seems to be comparable to hook.js, but does have some iScroll like features as well. You can 'remove' the message by specifying the pullHeaderHTML with empty HTML strings.

Native wrapping

The alternative, which I am convinced you do not want to do, but I do want to add for the sake of completeness, is to distribute your app as a native app for example bundled up in phonegap. However to get this working would require a fair number of changes to the phonegap code itself and would not be an advisable approach (I have once developed a phonegap app using native components and interactions 'around' it triggering various javascript events, although doable and presenting certain advantages it's not something I would advice).

like image 198
David Mulder Avatar answered Oct 04 '22 02:10

David Mulder