简体   繁体   English

滚动至锚点+调整顶部固定菜单

[英]scroll to anchor + adjust for fixed menu at top

I've seen several Q&As on this but i cant seem to get any of them to work in my situation, such as: jQuery Smooth Page Anchor Transitions with position:fixed menu 我已经看到了几个有关此问题的问答,但我似乎无法在我的情况下让它们起作用,例如: 带有位置:固定菜单的jQuery平滑页面锚转换
:( :(

Page example: http://fpco-site.squarespace.com/familylife 页面示例: http : //fpco-site.squarespace.com/familylife

As you scroll down the page, the main menu sticks/fixes to the top. 当您向下滚动页面时,主菜单在顶部停留/修复。 there is also a dropdown tied into the fixed menu. 固定菜单中还有一个下拉菜单。 inside the menu (and possibly other places on the page) i have regular links as well as anchor links. 在菜单中(可能还有页面上的其他位置),我有常规链接以及锚链接。 currently when the anchor links are clicked the menu & dropdown cover up the content when in the fixed position. 当前,当单击锚链接时,菜单和下拉菜单将内容固定在固定位置。

I need to get the jump to anchors to adjust to the menu and also the dropdown when open. 我需要跳转到锚点以适应菜单,还需要在打开时下拉菜单。 it would be ideal for the content to animate up and down as it scrolls to the anchor and up and down as the dropdown opens and closes. 内容在滚动到锚点时可以上下动画,而下拉菜单的打开和关闭时则可以上下动画。

In case it matters, my anchors throughout the page are added via jquery. 如果很重要,我的整个页面锚点都通过jquery添加。

I tried the following but there is something wrong in the code that breaks my dropdown menu: 我尝试了以下操作,但是代码中的某些错误打破了我的下拉菜单:

    /* scrollTo */
    function scrollTo(id){
        $('html,body').animate({scrollTop: $(id).offset().top},'slow');
    };
    $('a[href^="#"]').click(function(){
        scrollTo($(this).attr('href'));
        return false;
    });

I figured if i could get that to work then i could then work on adding in the additional offest to account for the menu and then try to offset as the dropdown opens/closes. 我想如果我能使它正常工作,那么我可以接着添加其他项目以解决菜单问题,然后尝试在下拉菜单打开/关闭时进行抵消。 but i couldnt get this first idea to work .... 但是我无法让这个第一个想法起作用....

I was able to fix this in my situation by using some css to re-position my anchors. 我可以通过使用一些CSS重新定位锚来解决此问题。 in my case the anchors are not applied to any visual element, so i could move them without it hurting anything. 在我的情况下,锚点未应用于任何视觉元素,因此我可以移动它们而不会伤到任何东西。 i used: 我用了:

.anchor {
position: relative;
top: -160px;
height: 1px;
display: block;

} }

i also told the dropdown to close when an anchor link in the dropdown is clicked. 我还告诉下拉菜单在单击下拉菜单中的锚链接时关闭。 that way i didnt have to worry about wheather the dropdown was open or closed. 这样,我不必担心下拉菜单是打开还是关闭。

and now when i click to go to one of the anchors it lines up right under my fixed menu at the top :) there probably is a better way, but this works for me!~ 现在,当我单击以转到其中一个锚点时,它会在顶部的固定菜单下对齐:)可能有更好的方法,但这对我有用!

Next to see if i can get it to animate to scroll to the anchor instead of jumping right to it. 接下来查看我是否可以使其动画以滚动到锚点,而不是直接跳到锚点。 that would be nice :) 那样就好了 :)

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

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