Reputation: 25
Having problem offset().top not working in Safari. Works fine on all other browsers but seems to break in Safari. Any suggestions?
$(window).scroll(function(){
collapseNav();
});
function collapseNav() {
if ($(".navbar").offset().top > 50 ) {
// code here
} else {
// code here
}
}
Upvotes: 1
Views: 4152
Reputation: 2321
You can fix this issue like this
// as of 1.4.2 the mobile safari reports wrong values on offset()
// http://dev.jquery.com/ticket/6446
// remove once it's fixed
if (/webkit.*mobile/i.test(navigator.userAgent)) {
(function($) {
$.fn.offsetOld = $.fn.offset;
$.fn.offset = function() {
var result = this.offsetOld();
result.top -= window.scrollY;
result.left -= window.scrollX;
return result;
};
})(jQuery);
}
Upvotes: 1
Reputation: 25
Found a work around using $(window).scrollTop(). So it looks like this:
$(window).scroll(function(){
collapseNav();
});
function collapseNav() {
if ($(window).scrollTop() > 50 ) {
// code here
} else {
// code here
}
}
Upvotes: 0