JQuery scroll to anchor on click?

吃可爱长大的小学妹 提交于 2019-12-12 09:56:27

问题


Basically I have this function is a class that creates pagination. I want to somehow use a smooth scroll to move the page back to the top of the comment container div but am unsure where or what function I would need to do that.

var Comments = function(options) {
    this.options = {
            id: 0,
            page: 0,
            object: null,
            name: null,
            parentid: 0,
            folder: './'
        };

    this.options = $.extend(this.options, options || {});  

    this.getComments =  function(page) {
        this.options.page = page;
        var object = this.options.object;
        var data = 'objid=' + this.options.name;
        $.ajax({
           type: "GET",
           url: this.options.folder + 'backend.php',
           data: data,
           success: function(msg){
             object.html(msg);
           }
         });
    };  

    this.getComments(this.options.page);
});

I'd like to get do something in the success getComments function that moves it up to the ID of the container. Is there a good way?


回答1:


If your comment div has an ID of comment-div, then you can do this:

$('html,body').animate({
    scrollTop: '+=' + $('#comment-div').offset().top + 'px'
}, 'fast');

You can adjust the speed as easing as needed, just check the animate documentation for details.



来源:https://stackoverflow.com/questions/5508819/jquery-scroll-to-anchor-on-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!