I have used a scrollTop
function in jQuery for navigating to top, but strangely \'the smooth animated scroll\' stopped working in Safari and Chrome (scrolling w
// if we are not already in top then see if browser needs html or body as selector
var obj = $('html').scrollTop() !== 0 ? 'html' : 'body';
// then proper delegate using on, with following line:
$(obj).animate({ scrollTop: 0 }, "slow");
BUT, best approach is to scroll an id into your viewport using just native api (since you scroll to top anyway this can be just your outer div):
document.getElementById('wrapperIDhere').scrollIntoView(true);
It works in both browsers if you use scrollTop() with 'document':
$(document).scrollTop();
...instead of 'html' or 'body'. Other way it won't work at the same time in both browsers.
If your CSS html
element has the following overflow
markup, scrollTop
will not function.
html {
overflow-x: hidden;
overflow-y: hidden;
}
To allow scrollTop
to scroll, modify your markup remove overflow
markup from the html
element and append to a body
element.
body {
overflow-x: hidden;
overflow-y: hidden;
}
I have used this with success in Chrome, Firefox, and Safari. Haven't been able to test it in IE yet.
if($(document).scrollTop() !=0){
$('html, body').animate({ scrollTop: 0 }, 'fast');
}
The reason for the "if" statement is to check if the user is all ready at the top of the site. If so, don't do the animation. That way we don't have to worry so much about screen resolution.
The reason I use $(document).scrollTop
instead of ie. $('html,body')
is cause Chrome always return 0 for some reason.
So I was having this problem too and I have written this function:
/***Working function for ajax loading Start*****************/
function fuweco_loadMoreContent(elmId/*element ID without #*/,ajaxLink/*php file path*/,postParameterObject/*post parameters list as JS object with properties (new Object())*/,tillElementEnd/*point of scroll when must be started loading from AJAX*/){
var
contener = $("#"+elmId),
contenerJS = document.getElementById(elmId);
if(contener.length !== 0){
var
elmFullHeight =
contener.height()+
parseInt(contener.css("padding-top").slice(0,-2))+
parseInt(contener.css("padding-bottom").slice(0,-2)),
SC_scrollTop = contenerJS.scrollTop,
SC_max_scrollHeight = contenerJS.scrollHeight-elmFullHeight;
if(SC_scrollTop >= SC_max_scrollHeight - tillElementEnd){
$("#"+elmId).unbind("scroll");
$.post(ajaxLink,postParameterObject)
.done(function(data){
if(data.length != 0){
$("#"+elmId).append(data);
$("#"+elmId).scroll(function (){
fuweco_reloaderMore(elmId,ajaxLink,postParameterObject);
});
}
});
}
}
}
/***Working function for ajax loading End*******************/
/***Sample function Start***********************************/
function reloaderMore(elementId) {
var
contener = $("#"+elementId),
contenerJS = document.getElementById(elementId)
;
if(contener.length !== 0){
var
elmFullHeight = contener.height()+(parseInt(contener.css("padding-top").slice(0,-2))+parseInt(contener.css("padding-bottom").slice(0,-2))),
SC_scrollTop = contenerJS.scrollTop,
SC_max_scrollHeight = contenerJS.scrollHeight-elmFullHeight
;
if(SC_scrollTop >= SC_max_scrollHeight - 200){
$("#"+elementId).unbind("scroll");
$("#"+elementId).append('<div id="elm1" style="margin-bottom:15px;"><h1>Was loaded</h1><p>Some text for content. Some text for content. Some text for content. Some text for content. Some text for content. Some text for content. Some text for content. Some text for content. Some text for content. Some text for content. Some text for content. Some text for content.</p></div>');
$("#"+elementId).delay(300).scroll(function (){reloaderMore(elementId);});
}
}
}
/***Sample function End*************************************/
/***Sample function Use Start*******************************/
$(document).ready(function(){
$("#t1").scrollTop(0).scroll(function (){
reloaderMore("t1");
});
});
/***Sample function Use End*********************************/
.reloader {
border: solid 1px red;
overflow: auto;
overflow-x: hidden;
min-height: 400px;
max-height: 400px;
min-width: 400px;
max-width: 400px;
height: 400px;
width: 400px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div class="reloader" id="t1">
<div id="elm1" style="margin-bottom:15px;">
<h1>Some text for header.</h1>
<p>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
</p>
</div>
<div id="elm2" style="margin-bottom:15px;">
<h1>Some text for header.</h1>
<p>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
</p>
</div>
<div id="elm3" style="margin-bottom:15px;">
<h1>Some text for header.</h1>
<p>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
Some text for content.<br>
</p>
</div>
</div>
I hope it will be helpfully for other programmers.
I use:
var $scrollEl = $.browser.mozilla ? $('html') : $('body');
because read jQuery scrollTop not working in Chrome but working in Firefox