如何使用 jQuery 单击链接后滚动到某个点?
我的页面上有一个链接,单击该链接时使用 .$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({ 滚动顶部: '+=' + $('#scrollTo').offset().top + 'px' }, 'fast');
我还修复了上面的代码,以便您可以看到它是如何工作的。
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);
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
或者尝试这个
scrollTo
插件Try this:
Or try this
scrollTo
plugin