gpt4 book ai didi

javascript - 指向 ID 的超链接 - HTML

转载 作者:行者123 更新时间:2023-11-28 02:31:53 26 4
gpt4 key购买 nike

我的主页上有一个联系人 div。

<div id="contact">....</div>

在我的菜单中,我有指向#contact 的链接,该链接可以平滑向下滚动以联系 div。

<a href="#contact">Contact Us</a>

在另一个页面上,我必须使用

<a href="www.mywebsite.com/#contact">

在使用 wordpress 时,所有页面都有一个菜单,我可以同时拥有这两种功能,还是必须创建 2 个不同的菜单,一个用于主页,一个用于其余页面?

最佳答案

添加自定义链接,<a href="www.mywebsite.com#contact">

在你的自定义 jquery 中添加

//jQuery for Page Scroll - Single page      
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 500);
return false;
}
}
});



// jQuery for Page Navigation
$( window ).load(function() {

if(window.location.hash) {
var url=window.location.href;
var hash = url.substring(url.indexOf('#')+1);
$('html, body').stop().animate({
scrollTop: $("#"+hash).offset().top
}, 1000);
}
});

关于javascript - 指向 ID 的超链接 - HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47648399/

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