简体   繁体   English

滚动到uRL中具有多个锚点的锚点,

[英]Scroll to an Anchor with multiple anchor in uRL,

Got a question about the Anchor point. 有一个关于Anchor问题。

I have a URL like domain .tld/ sub /#point1/#point2 and I also using tabs which is triggered by the first #point1 . 我有一个像domain .tld / sub /#point1 /#point2这样的URL,并且我还使用了由第一个#point1触发的选项卡。 So if someone opens the link the browser is detecting the first tab on that page and then it needs to find the second #point2 which is a anchor point like <div id="point2"></div> . 因此,如果有人打开链接,浏览器将检测到该页面上的第一个标签,然后需要找到第二个#point2 ,它是一个锚点,例如<div id="point2"></div>

But now comes te question, since it is not possible to use multiple anchor scrolldowns when the pages is loaded, I have to find a other option. 但是现在出现了一个问题,因为在页面加载时不可能使用多个锚点向下滚动,所以我必须找到另一个选项。 But offcourse didn't have that much experience. 但是,课程没有那么多经验。 Who could help me out? 谁能帮助我?

Regards Martijn 问候马丁

$(document).ready(function() {
    goToUrl();
});


function goToUrl() {
    var newUrl      = document.URL;
    var newUrlArr   = newUrl.split("#");
    var newUrlArrLength = newUrlArr.length;
    if (newUrlArrLength>0) {
        var last = parseInt(newUrlArrLength) -1;
        var lastUrl         = newUrlArr[last];
        var lastItem        = $("#"+lastUrl);
        $('html, body').animate({
            scrollTop: lastItem.offset().top
        }, 1000);
    }
}

Ok I changed the answer from Stavros a bit like underneath: 好的,我像下面这样从Stavros更改了答案:

$(function() {
    goToUrl();


    function goToUrl() {
    var newUrl      = document.URL;
    var newUrlArr   = newUrl.split("?target=");
    var defUrl = newUrlArr[1].slice(0, newUrlArr[1].indexOf("#"));

    var newUrlArrLength = defUrl.length;
    if (newUrlArrLength>0) {
        var tag = $("#"+defUrl+"");
         $(window).scrollTop(tag.offset().top);
    }
}

});

This does the job for me! 这为我完成了工作! Thanks Stavros. 谢谢斯塔夫罗斯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM