简体   繁体   English

用ajax加载页面并滚动到加载的页面锚点

[英]Load page with ajax and scroll to loaded page anchor

I load a page with ajax and I want main page to auto scroll to the beginning of loaded page. 我使用ajax加载页面,并且希望主页自动滚动到加载页面的开头。 Here is the code I use: 这是我使用的代码:

html HTML

<button class="btn btn-info btn-lg" type="button" id="photos"> <i class="fa fa-plus fa-5x"></i><br>Add Photo</button>

and at the bottom of the page I have this js code 在页面底部,我有此js代码

$(document).ready(function(){
$("#photos").click(function(){
    $(".content-loader").fadeOut('slow', function()
    {
        $(".content-loader").fadeIn('slow');
        $(".content-loader").load('somepage.php?pid=3');
    });
});

The above works fine and page 'somepage.php?pid=3' is loaded in <div class="content-loader"> . 上面的工作正常,页面'somepage.php?pid = 3'已加载到<div class="content-loader"> How can I make the page to autoscroll to <div class="content-loader"> when page 'somepage.php?pid=3' is loaded? <div class="content-loader">页面'somepage.php?pid = 3'后,如何使页面自动滚动到<div class="content-loader">

the following should work for the .load() function. 以下应该适用于.load()函数。 It should scroll on load complete 它应在加载完成时滚动

$(".content-loader").load('somepage.php?pid=3', function () {
  $('html,body').animate({scrollTop: $(".content-loader").offset().top});
});

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

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