jQuery从页面B滚动到页面A上的ID(节点ejs) [英] jQuery scroll to ID on page A from page B (node, ejs)
问题描述
这是我的导航栏
< ul id =jumpclass =dropdown-menurole =menu >
< li>< a href =/ main_page#top_something>人物< / a>< / li>
< li>< a href =/ main_page>主页< / a>< / li>
< / ul>
我正在使用这个函数
<$ p ($)b
$ b $ preventDefault();
var target = $(this).attr(href);
} else {
var target = location.hash;
}
$('html,body')。animate(
{
scrollTop:$(target).offset()。top
},1000,function()
{
location.hash = target;
});
}
$('html,body')。hide()
$(document).ready(function()
{
$('#jump a [href]')。bind(click,jump);
if(location.hash){
setTimeout(function(){
$('html,body')。scrollTop(0).show()
jump()
},0);
} else {
$('html,body')。show()
}
});
})(jQuery)
main_page 是一个ejs文件,并在
我试图点击navbar一页并转到div部分的B页面。 这是同一主题, jQuery从不同页面滚动到ID 但我无法得到它的工作......谢谢 区别在于我有node.js和* .ejs文件
这给了我:
jump.js:18 Uncaught TypeError:无法读取未定义的属性'top'
错误
$ div class =h2_lin>解决方案
(function(){
if(location.hash ==='#top_something') {
window.setTimeout(
function(){
$('html,body')。animate({
scrollTop:$('#top_something')。offset() .top
},'slow');
},2000);
}
})();
在这种情况下,这对我很有用:)
this is my navbar
<ul id="jump" class="dropdown-menu" role="menu">
<li><a href="/main_page#top_something">People</a></li>
<li><a href="/main_page">Main Page</a></li>
</ul>
and I am using this function
(function($){
var jump=function(e)
{
if (e){
e.preventDefault();
var target = $(this).attr("href");
}else{
var target = location.hash;
}
$('html,body').animate(
{
scrollTop: $(target).offset().top
},1000,function()
{
location.hash = target;
});
}
$('html, body').hide()
$(document).ready(function()
{
$('#jump a[href]').bind("click", jump);
if (location.hash){
setTimeout(function(){
$('html, body').scrollTop(0).show()
jump()
}, 0);
}else{
$('html, body').show()
}
});
})(jQuery)
main_page is a ejs file , and on /main_page route, node.js will render main_page.ejs file.
I am trying to click on navbar on A page and go to B page on div section. This give me : jump.js:18 Uncaught TypeError: Cannot read property 'top' of undefined error
Here is the same topic , jQuery scroll to ID from different page but I can't get it work ...thanks
Difference is that I have node.js and *.ejs files
(function (){
if(location.hash === '#top_something'){
window.setTimeout(
function(){
$('html, body').animate({
scrollTop: $('#top_something').offset().top
}, 'slow');
},2000);
}
})();
This is what worked for me in this case :)
这篇关于jQuery从页面B滚动到页面A上的ID(节点ejs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!