gpt4 book ai didi

jquery - 链接到不同页面 -> jquery 滚动到特定 anchor

转载 作者:行者123 更新时间:2023-12-03 22:52:59 24 4
gpt4 key购买 nike

在我的主页底部,我添加了一个联系表单,并将此部分的 anchor 指定为 div id="contact"。

当在任何页面上单击联系按钮时,它应该导航到主页并在页面加载时自动滚动到联系表单。

在查看了我在这里找到的类似问题后,我未能成功地使其发挥作用:jQuery scroll to ID from different page当我尝试时,它只是跳转到表单。我希望它能够平滑滚动。

<li><span>Get in touch</span><a href="index.html#contact">Contact</a></li>

问题jquery函数从其他页面滚动到主页上的联系人 anchor :

(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() {
$('a[href^=#]').bind("click", jump);

if (location.hash) {
setTimeout(function(){
$('html, body').scrollTop(0).show()
jump()
}, 0);
} else {
$('html, body').show()
}
});

我正在尝试实现与此示例类似的目标:http://vostrel.cz/so/9652944/page.html区别在于,在本例中,“ anchor id”不是出现在两个页面上,而是我的 anchor id(联系人)仅出现在一页(主页)上。

最佳答案

试试这个,你的脚本没问题,只是缺少最后一行

 (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 () {
$('a[href^=#]').bind("click", jump);

if (location.hash) {
setTimeout(function () {

$('html, body').scrollTop(0).show()
jump();

}, 0);
} else {

$('html, body').show();

}
});
})(jQuery)

关于jquery - 链接到不同页面 -> jquery 滚动到特定 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11820055/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com