I have a rather intense (yet wonderfully simple) UI in JQ where anchors are required for the UI to position.
The problem is in iOS/Chrome (webkit) and various other browsers. When an anchor is clicked about 50% of the time the a
tag will trigger the Chrome URL address bar to slide down, press again and it goes away. The offset is serious no bueno.
As you would expect this messes up the UI positioning and the address bar offsetting is rather hard to predict as it is a device+screen size reaction.
I think this is a bug when the first fragment is used where there was previously none OR the change of a fragment state (new or changed). These actions, or similar, are telling the browser "a tag clicked, activate url bar".
I like this solution (CSS) but this does not address the problem as the address bar is not an element to predict against reliably (unless I want to write a foolish amount of JS for a small problem):
https://stackoverflow.com/a/13184714/860715
I am skipping the long-winded full screen API as not all mobile have that feature.
My current solution, in testing now, is to add onclick="return;"
to the a
tags anchoring the UI. This method seems to capture the anchor activity and skip the a
tag triggering the address bar.
Thoughts on a better solution and/or possible issues in the UI for various mobile browsers.
Thx to Louys Patrice Bessette for the solution, a minor edit was required:
$(document).ready(function(){
$("a").on("click", function(e){
if( $(this).attr("href").substr(0,1) == "#" && $(this).attr("href").length > 1 ){
var targetPos = $(document).find("a[name='"+$(this).attr("href").substr(1)+"']").offset().top;
$("html, body").scrollTop(targetPos);
e.preventDefault(); // this was moved -<-<
}
});});