jquery - scroll to div ajaxjavascript - Stack Overflow

Ok, for the life of me, I can't get this to work. I've searched for examples and I found tons

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
Add a ment  | 

4 Answers 4

Reset to default 3

Try 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

相关推荐

  • jquery - scroll to div ajaxjavascript - Stack Overflow

    Ok, for the life of me, I can't get this to work. I've searched for examples and I found tons

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信