Ok, for the life of me, I can't get this to work. I've searched for examples and I found tons. But none of them seem to work for me.
Question is simple, I have a div
that loads content from the database.
I need to scroll to the bottom of the div when the page is loaded.
here's an example.
<body>
<div id="container">
<div id="messagewindow">
<?php
foreach($results as $r){
//content loop..
}
?>
<div id="scrolltome"></div>
</div>
</div>
</body>
I added the div
id scrolltome
in hopes it would work.
Thank you in advance!
Ok, for the life of me, I can't get this to work. I've searched for examples and I found tons. But none of them seem to work for me.
Question is simple, I have a div
that loads content from the database.
I need to scroll to the bottom of the div when the page is loaded.
here's an example.
<body>
<div id="container">
<div id="messagewindow">
<?php
foreach($results as $r){
//content loop..
}
?>
<div id="scrolltome"></div>
</div>
</div>
</body>
I added the div
id scrolltome
in hopes it would work.
Thank you in advance!
Share Improve this question edited May 19, 2015 at 7:18 Lucky 17.4k19 gold badges120 silver badges156 bronze badges asked May 19, 2015 at 4:29 YadigitYadigit 512 silver badges7 bronze badges 1- Can you provide a fiddle to explain the problem?? – Guruprasad J Rao Commented May 19, 2015 at 4:33
4 Answers
Reset to default 3Try using window.scrollTo(0, $(element).offset().top)
For example: https://jsfiddle/88uhpkx0/
Try below function
function scrollToElement(selector, time, verticalOffset) {
time = typeof(time) != 'undefined' ? time : 500;
verticalOffset = typeof(verticalOffset) != 'undefined' ? verticalOffset : 0;
element = $(selector);
offset = element.offset();
offsetTop = offset.top + verticalOffset;
$('html, body').animate({
scrollTop: offsetTop
}, time);}
Call this function by
scrollToElement('#scrolltome`, 750, -50);
. Here 750 time set for animate and -50 is elements offset value. You can change these values according to your needs
Simply pass the top offset distance of #scrolltome div to body scrollTop on window load like this:-
$(window).load(function(e) {
var distance = $('#scrolltome').offset().top
$('html,body').animate({scrollTop:distance},1500);
});
First of all, your scrolltome
div is missing >
.
And you could try the following to make the scroll.
$('#messagewindow').animate({
scrollTop: divHeight
}, 1000);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745362387a4624418.html
评论列表(0条)