Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

jquery - Reset URL in Wordpress Menu

I have a page which is basically a one pager. The menu uses # tags to scroll to different positions on the page. Now i added a shop to this page, which has the url : mypage.com/shop.

Now if i click in my menu on home. It scrolls to #home. But when i click on the shop it goes to mypage.com/shop and when i now click on the home link it goes to: mypage.com/shop#home. If i change the links in my menu from #home to mypage.com#home it works, but the scrolling doesnt work anymore. I made a jquery script to scroll to the different anchors, but this doenst work with a full url like: mypage.com#home.

jQuery(function($) {
$(".menu-item").on("click",function() {
  var href = $(this).attr('href');
  var elem = $(href).offset().top;
  $('html, body').animate({
    scrollTop:elem - 80
  },'slow');
});
});

is there a way to change this script to work with href links like : mypage.com#home ? Or is there a way to use #home only on the base url? So if i switch from mypage.com#home to mypage.com/shop and then back, that it will use mypage.com as the url instead of keeping /shop?

question from:https://stackoverflow.com/questions/66045779/reset-url-in-wordpress-menu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

i think you can solve this with window.location. for example

var getUrl = window.location;
console.log(getUrl);

that code give you current url. you can combine it with your href variable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...