我的页面上有一个链接,当点击时使用。$ get然后追加将内容加载到div#top-main-content中。加载此内容后,我希望页面向下滚动到加载到#top-main-content的内容。我怎样才能做到这一点?
$(document).ready(function()
{
$('a.newsflash1').click(function()
{
$('html,body').animate({
scrollTop: '+=' + $('#scrollTo').offset().top + 'px'
}, 'fast');
$('#top-main-content').empty();
$.get('index.xml', function(c){
$(c).find('news_story').each(function(){
var $story = $(this);
var storyTitle = $story.find("story_title").text();
var storyCompany = $story.find("story_company").text();
var storyAuthor = $story.find("story_author").text();
var storyYear = $story.find("story_date").attr("year");
var storyMonth = $story.find("story_date").attr("month");
var storyDay = $story.find("story_date").attr("day");
var description = $story.find('story_content').text();
var html = '<div class="story-content darkborder">';
html += '<div class="story-contrast lightborder">';
html += '<h3 class="story-title">' + storyTitle + '</h3>';
html += '<div class="story-copyright">';
html += '<p class="story-company">'+ storyCompany +'</p>';
html += '<p class="story-author">'+ storyAuthor +'</p>';
html += '<p class="story-date">'+ storyYear+'.'+storyMonth+'.'+storyDay +'</p>';
html += '</div>';
html += '<p class="story-body">' + description + '</p>';
html += '</div>';
html += '</div>';
$('#top-main-content').append($(html));
window.location.hash = '#top-main-content';
});
});
});
});
好的,我已经用下面的代码弄明白了:
$('html,body').animate({
scrollTop: '+=' + $('#scrollTo').offset().top + 'px'
}, 'fast');
我还修改了上面的代码,以便您可以看到它是如何工作的。