How to scroll to a certain point after clicking a link with jQuery?
I have a link on my page that when clicked uses .$get and then append to load content into a div#top-main-content. After this content is loaded I want the page to scroll down to the content that was loaded into #top-main-content. How can I achieve this?
$(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);
开发者_JAVA百科 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';
});
});
});
});
Okay I have figured it out with the below code :
$('html,body').animate({
scrollTop: '+=' + $('#scrollTo').offset().top + 'px'
}, 'fast');
I've also fixed my code above so that you can see how it works.
Try this:
window.location.hash = '#top-main-content';
Or try this scrollTo
plugin
精彩评论