简体   繁体   English

跨不同页面锚定

[英]Anchoring Across Different Pages

So right now I have a script that allows anchoring, but it only works within the page I am currently on. 因此,现在我有一个允许锚定的脚本,但它仅在我当前所在的页面内有效。

    $('.menu a').click(function(){
        var question = this.innerHTML;
        var answer = $.find('dt:contains(' + question + ')');
        $('html, body').animate({
            scrollTop: $(answer).offset().top
        }, 200);
        return false;
    });

  <div class="menu">
        <div>
            <h3 class="heading"><a href="/about">About</a> <span class="expand">+</span></h3>
            <ul class="subheader">
                <li><a href="/about">How Can You Help?</a></li>
                <li><a href="/about">How can I submit a show?</a></li>
                <li><a href="/about">Who are we?</a></li>
            </ul>
        </div>

        <div>
            <h3 class="heading"><a id="contact" href="contact">Contact</a> <span class="expand">+</span></h3>
            <ul class="subheader">
                <li><a href="contact">Email</a></li>
                <li><a href="contact">On Site</a></li>
                <li><a href="contact">Social Media</a></li>
            </ul>
        </div>
</div>

What I'm trying to figure out is how to alter my current script so that I can anchor myself to the correct position on the next page. 我想弄清楚的是如何更改当前脚本,以便可以将自己定位到下一页上的正确位置。

For example, I am currently on my About page, but I click on "Email" in my menu. 例如,我目前在我的“关于”页面上,但是我在菜单中单击“电子邮件”。 What I want to happen is for the page to redirect itself to the Contact page their anchor itself to where Email is. 我要发生的是该页面将自身重定向到“联系”页面,其锚点自身定位到“电子邮件”所在的位置。 Right now, it just puts me on the top of the page. 现在,它只是将我放在页面顶部。

I'm trying not to do this with several id's because these aren't my only pages so it would not be the best idea to have over 30 id's just to get this to work. 我尝试不使用多个id来执行此操作,因为这些不是我唯一的页面,因此拥有30个以上的id只是为了使它正常工作并不是最好的主意。 I guess that's my last resort though. 我想那是我的最后选择。

The question isn't clear, but I assume you have a javascript function that takes a string and finds the correct place to send the user. 问题尚不清楚,但我假设您有一个使用字符串的javascript函数,并找到了发送用户的正确位置。 In that case, you can run this on page-load with the page's current hash: 在这种情况下,您可以使用页面的当前哈希值在页面加载时运行此代码:

$(function() {
    if (location.hash) {
        var question = location.hash;
        var answer = $.find('dt:contains(' + question + ')');
        $('html, body').animate({
            scrollTop: $(answer).offset().top
        }, 200);
    }
});

And your email link can look like this: 您的电子邮件链接可能如下所示:

<li><a href="contact#email">Email</a></li>

Or, if you want to use the <a> content as the hash: 或者,如果要使用<a>内容作为哈希值:

<li><a href="contact">Email</a></li>
...
$(function() {
    $('a').each(function(){$(this).attr('url',$(this).attr('url')+'#'+$(this).text())});
});

this will turn that link into: 这将使该链接变为:

<li><a href="contact#Email">Email</a></li>

Just do... 做就是了...

<li><a href="contact#email">Email</a></li>
<li><a href="contact#onsite">On Site</a></li>
<li><a href="contact#social">Social Media</a></li>

Then set the anchors on the page youre talking about 然后在您要谈论的页面上设置锚点

Like so.... 像这样...

//email section to link to
 <div id="email"></div>
//onsite section to link to
 <div id="onsite"></div>

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

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