简体   繁体   English

jQuery滚动到由ajax请求加载的内容中的id

[英]jQuery Scroll to id in content loaded by ajax request

I need to do scroll to the item loaded by ajax, but after loading the contents, nothing happens, the page is always at the top. 我需要滚动到由ajax加载的项目,但是加载内容之后,什么也没有发生,因此页面始终位于顶部。 When I click refresh button (the page is reloaded dynamically with content caching), the page scrolls to the designated target. 当我单击“刷新”按钮(使用内容缓存动态重新加载页面)时,页面滚动到指定的目标。

var startPageLoader = function( pid, sid, url ) {
        loader( 'show' );

        $.ajax( {
            url         : cCore.Settings[ 'url' ] + '/page/',
            method      : 'POST',
            data        : { _t : 'page', _pid : pid, _sid : sid },
            dataType    : 'json',
            success     : function( ret ) {
                if( !ret[ 'status' ] ) {
                    loader( 'hide' );
                    return;
                }
                loader( 'hide' );

                $( '#mainWrapper > .content article.subpage' ).hide( ).html( ret[ 'data' ] ).fadeIn( );

                if( url != null ) {
                    if( sid.length )
                        url += '#' + sid;

                    history.pushState( {}, '', url );
                }

                aProduct[ 0 ] = pid;

                cCore.Plugins[ "modalTooltip" ].rebuild( );

                prepareGallery( );
                scrollToSection( sid );
            }
        } );    
    };

    var scrollToSection = function( sid ) {
        if( !sid.length )
            return;

        aProduct[ 1 ] = sid; // sid is an name of target

        var offset = $( "#" + sid ).offset( ).top;
        $( 'html, body' ).animate( {
            scrollTop: offset
        }, 1000 );


    };

I think that could be issue with the document height, but i'm not sure. 我认为这可能与文档高度有关,但我不确定。

Thanks. 谢谢。

You should be able to use anchors to achieve this with some javascript also. 您还应该可以使用锚来通过一些JavaScript来实现此目的。

Here's a link describing anchors: http://www.htmlgoodies.com/tutorials/getting_started/article.php/3479511 这是描述锚点的链接: http : //www.htmlgoodies.com/tutorials/getting_started/article.php/3479511

and Here's one describing how to jump to an anchor using javascript: anchor jumping by using javascript 而这里有一个描述如何使用JavaScript跳转到锚: 通过使用javascript锚跳跃

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM