window.scrollTo not working in phonegap - alternative solution or workaround?

10,373

Solution 1

After doing some research, I realized window.scrollTo() does actually work in iOS6 with phonegap 2.1, there was something else that failed; for some reason, document.height didn't yield a property of equal proportion within UIwebView so I had to write a small workaround. I'll post the solution and the entire code below for future reference.

function setKeyboardPos(tarId) {

//programmatically: set scroll pos so keyboard aligns perfectly underneath textfield
var elVerticalDistance = $("#"+tarId).offset()["top"];
var keyboardHeight = 157;

if(isNativeApp()) { keyboardHeight = 261; } //I choose to change the keyboard height for the sake of simplicity. Obviously, this value does not correnspond to the actual height of the keyboard but it does the trick
var keyboardTextfieldPadding = 2;
var heightOfView = document.height;
var inputHeight = $("#"+tarId).outerHeight();

var viewPortSpace = heightOfView-keyboardHeight-keyboardTextfieldPadding; //180
var verticalNewSroll = (elVerticalDistance+inputHeight)-viewPortSpace;
if(verticalNewSroll<0) { verticalNewSroll = 0; }
////

//OK, all done lets go ahead with some actions
$("#footer").hide(); //hide footer so that the keyboard doesn't push it on top of textfield
$("#containingDiv").css("bottom","0px"); //remove bottom space for footer
window.scrollTo(0,verticalNewSroll); // perform scroll!

}

function isNativeApp() {

var app = (document.URL.indexOf('http://') === -1) && (document.URL.indexOf('https://') === -1);
if (app) {
    return true; // PhoneGap native application
} else {
    return false; // Web app / safari
}

}

Solution 2

you can try and use the animate and scrollTop property to scroll It looks something like this:

$("html, body").animate({ scrollTop: "The value to scroll to" });

Hope this helps.

Share:
10,373

Related videos on Youtube

Jonathan
Author by

Jonathan

Updated on July 03, 2022

Comments

  • Jonathan
    Jonathan almost 2 years

    I've written a rather basic js function that programatically and automatically aligns the iPhone keyboard perfectly underneath each and every input field that gets focused (feel free to use it if you like it!). The alignment's primarily handled by window.scroll - a standard method that works in any browser view, except in UIWebView hence phonegap/cordova (2.1). So I need a workaround.

    My working code:

    function setKeyboardPos(tarId) {
    
    //programmatically: set scroll pos so keyboard aligns perfectly underneath textfield
    var elVerticalDistance = $("#"+tarId).offset()["top"]; //i.e. 287
    var keyboardHeight = 158;
    var heightOfView = document.height; // i.e. 444
    var inputHeight = $("#"+tarId).outerHeight();
    
    var viewPortSpace = heightOfView-keyboardHeight; //i.e. 180
    var verticalNewSroll = (elVerticalDistance+inputHeight)-viewPortSpace;
    if(verticalNewSroll<0) { verticalNewSroll = 0; }
    ////
    
        //OK, all done lets go ahead with some actions
        $("#footer").hide(); //hide footer so that the keyboard doesn't push it on top of textfield
        $("#containingDiv").css("bottom","0px"); //remove bottom space for footer
        window.scrollTo(0,verticalNewSroll); //scroll! where the problem starts
    
    
    }
    

    Working in everything but UIWebView, that is. As I mentioned above, everything works except the window.scrollTo (N.B. some minor changes have been made for the sake of clarity). So does anyone know of an alternative solution or even a good workaround?

    Similar questions

    window.scrollTo doesn't work in phonegap for IOS

    PhoneGap / Cordova scrollTo Ignored

    How to add vertical scroll in Phonegap

    Above are furthermore three similar questions that somewhat points one in the right direction. One of the answerers mentions the use of css to accomplish this. Can anyone come up with a more concrete example? Another guy suggests anchors but that's not a very pretty solution and doesn't go very well with the rest of my code.

  • Jonathan
    Jonathan over 11 years
    Thanks but I just realized window.scrollTo() did actually work here (in iOS 6!) so this was actually not the culprit.