JQuery的滚动到锚上点击? [英] JQuery scroll to anchor on click?

查看:111
本文介绍了JQuery的滚动到锚上点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有这个功能是创建分页类。我想以某种方式使用平滑滚动移动页面回评div容器的顶部,但我不确定在哪里,有什么功能,我需要做的。

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);
});

我想获得做一些事情的成功getComments功能将它移动到容器的ID。有没有好办法?

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?

推荐答案

如果您的评论div有评论-DIV 的ID,那么你可以这样做:

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.

这篇关于JQuery的滚动到锚上点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆