Javascript 当链接已经有锚点滚动时,滚动到另一页上的位置
我在Javascript 当链接已经有锚点滚动时,滚动到另一页上的位置,javascript,jquery,html,Javascript,Jquery,Html,我在主页上有一个菜单,可以滚动到id位置: <li><a href="#event-section">El evento</a></li> <li><a href="#asistentes-section">Asistentes</a></li> <li><a href="#contact-section">Contacto & Inscripciones</a&
主页
上有一个菜单,可以滚动到id
位置:
<li><a href="#event-section">El evento</a></li>
<li><a href="#asistentes-section">Asistentes</a></li>
<li><a href="#contact-section">Contacto & Inscripciones</a></li>
这很好用
问题是我在网站上有另一个页面。当用户进入它并单击菜单的任何链接时,它就是不起作用,因为divs
仅存在于主页上。我希望,如果用户在另一个页面中,并单击菜单的链接,则主页将打开,进入该部分
您知道如何修复此菜单以实现这两个功能吗?jQuery如果您实现了此功能,则不需要:
function scrollTo(anchorName) {
location.hash = "#" + anchorName;
return false;
}
在这种情况下,您的链接应该是:
<a href="#anchorName">my_link</a>
尝试添加index.html,然后在除主页外的所有其他页面中按如下方式散列。这应该行得通
您可以使用此脚本,需要使用动态方式将哈希url向下滚动到那里。
例如:
if(window.location.href.trim().toLowerCase().indexOf(“#事件节”)>-1){
$('html,body').stop().animate({
“scrollTop”:$(“a[href=”#事件节“])。offset().top
},800,'swing',函数(){
});
}
这是作为index.html#event部分的第一个链接页面。最简单的方法是为其他页面创建另一个导航栏,从中重定向最终用户
<li><a href="/index.php?id=event-section">El evento</a></li>
<li><a href="/index.php?id=asistentes-section">Asistentes</a></li>
<li><a href="/index.php?id=contact-section">Contacto & Inscripciones</a></li>
这只是重新加载页面,将#
添加到URL,而不转到主页
是否尝试在document.ready(function())上加载脚本?否则,请尝试第三方插件,如fullpage.js:)这只需将#
添加到URL中,而无需转到主页
即可重新加载页面section@propcode您应该返回false此函数中的代码>未重新加载页面
<script>
if (window.location.href.trim().toLowerCase().indexOf('#event-section') > -1) {
$('html, body').stop().animate({
'scrollTop': $("a[href='#event-section']").offset().top
}, 800, 'swing', function () {
});
}
</script>
<li><a href="/index.php?id=event-section">El evento</a></li>
<li><a href="/index.php?id=asistentes-section">Asistentes</a></li>
<li><a href="/index.php?id=contact-section">Contacto & Inscripciones</a></li>
<script>
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
$(document).ready(function(){
var param_scroll = getParameterByName('id');
if(param_scroll){
$('html, body').animate({
scrollTop: $("#"+param_scroll).offset().top
}, 3000);
}
});
</script>