是否可以通过jQuery.load()加载外部元素后滚动到锚点
Is it possible to scroll to anchor after external elements are loaded through jQuery.load()?
场景是这样的:
- 从RSS源链接到我的网站
http://website.com/#anchorelement
- 我想跳转/滚动到
#anchorelement
在 http://website.com/
准备好后,jQuery.load()
加载#anchorelement
。因此,使用链接http://website.com/#anchorelement
不会跳转到#anchorelement
,因为在执行跳转时还没有加载元素。
谁有什么建议,如何使这个工作?是否有可能用javascript拦截锚跳并使其等待直到jQuery.load()
调用完成?
谢谢!
$(function(){
// so this is inside your document ready function.
// you need to add a callback
$( somecontainer ).load('somecontent', function(){
// after the content has been loaded
// check if a hash is present in the location
if( window.location.hash != '' ){
// find an element with matching id
var anchor = $( location.hash ).get(0);
// if element was found scroll it into view
if( anchor ){ anchor.scrollIntoView() }
}
});
});
如果你有多个内容要加载,使用回调函数逐个加载,如果你想在其中一个上滚动,在最后一个回调时调用函数scroll。
$(function ()
{
//Get the hash and load your content
var hash = location.hash.replace('#', '');
loadLocationContent(hash);
});
function loadLocationContent(hash)
{
$('container1').load("someContent1", function(){
$('container2').load("someContent2", function(){
$('container3').load("someContent3", function(){
$('container4').load("someContent4", scrollToHashAfterContentLoad(hash));
});
});
});
}
function scrollToHashAfterContentLoad(hash, contentId)
{
if (hash != '')
{
$('html, body').animate({ scrollTop: $('#' + hash).offset().top }, 2000);
}
}
相关文章:
- 指示禁用元素滚动行为并改为滚动页面
- 在某个点停止固定元素滚动
- jQuery 航点在元素滚动到视图中时添加类
- 到达(窗口)上的中间元素.滚动
- 当元素滚动出屏幕时更改元素位置
- jQuery-将元素滚动到屏幕中间,而不是使用锚链接滚动到顶部
- 我想为一个元素添加一个事件监听器,当相应的元素滚动到视图中时,该监听器就会被激发
- 将元素滚动到页面底部的视图中
- fadeIn元素滚动
- 当光标到达底部时,有没有办法防止contentEditable元素滚动
- 固定元素滚动到Div
- 如何平滑地防止固定元素滚动经过指定元素
- 将所有HTML元素滚动到视图中
- 如果浏览器窗口高度为X,并且元素滚动到其中的X点,则合并条件
- 使用isroll4将最后一个元素滚动到第一个元素的开始位置
- 如何显示单独的锂元素滚动基于窗口的高度
- 根据所单击的元素滚动到页面上的相同位置
- 元素滚动的同时滚动另一个
- 修改jQuery.ScrollTo插件将元素滚动到页面中心(垂直/水平)或尽可能靠近中心
- 我如何检测一个元素滚动到视窗