html - 当用户单击 anchor 链接时,某些内容会出现在固定标题下

标签 html css

我正在尝试创建一个 anchor 链接,以便当用户单击菜单上的某个元素时,它会转到指定的目的地。我的菜单包含三项(一、二、三)。例如,当我单击“三”时,它会跳转到“三”,但其标题位于标题下方。我怎样才能防止这种情况发生?我希望用户能够看到标题。 请注意,我希望我的标题是固定的,我希望内容滚动到标题后面。

HTML:

<body>
<header>
    <nav>
        <ul>
            <li><a href="#one">One</a></li>
            <li><a href="#two">Two</a></li>
            <li><a href="#three">Three</a></li>
        </ul>
    </nav>
</header>

<section>
    <div id="one">
        <h1>One</h1>
        <p>Text...</p>
    </div>
    <div id="two">
        <h1>Two</h1>
        <p>Text...</p>
    </div>
    <div id="three">
        <h1>Three</h1>
        <p>Text...</p>
    </div>
</section>

<footer>
</footer>
</body>

CSS:

body {
    background-color: #cf8;
}

header {
    background-color: #000;
    height: 4em;
    width: 100%;
    top: 0;
    left: 0;
    position: fixed;
}

nav ul {
    list-style: none;
}

nav ul li {
    margin-top: 0em;
    padding: 5px;    
    float: left;
}

nav ul li a {
    color: white;
    text-decoration: none;
}

section {
    height: auto;
    width: 50%;
    margin-top: 4em;
    margin-left: 25%;
}

#one,#two,#three {
    margin-top: 1em;
}

div {
    background-color: #c00;
    height: auto;
    width: 100%;
}

footer {
    background-color: #000;
    height: 2em;
    width: 100%;
    clear: both;
}

JSFIDDLE , JSFIDDLE (Version 2)

JSFIDDLE (FULLSCREEN) , JSFIDDLE (FULLSCREEN (VERSION 2))

最佳答案

我建议改用基于 jQuery 的解决方案(p/s:请参阅[Edit #2] 了解最终代码,其中我还检测到 window.location.hash 属性):

$(function() {
    // Only trigger .click() event when the link points to an internal anchor
    $("header a[href^='#']").click(function(e) {

        // Get the ID of the target
        var target = $(this).attr("href");

        // Animated scrolling to the vertical offset of the target element
        // PLUS the outer height of the <header> element
        $("html, body").animate({
            scrollTop: $(target).offset().top - $("header").outerHeight()
        });

        // Prevent default scrolling action
        // (I didn't use return false, because it stops event bubbling, too)
        e.preventDefault();
    });
});

http://jsfiddle.net/teddyrised/NHtvM/13/


[编辑]:但是,您应该注意,当访问者通过在 url 中输入位置散列(例如/page.html#one)导航到特定的 div 时,此方法不起作用).


[编辑 #2]: 好的,我修改了我的脚本,以便它可以检测散列 URL(如果存在),并执行与上面相同的操作 (updated Fiddle here)。一个例子是:http://jsfiddle.net/teddyrised/NHtvM/15/show/light/#three ,您希望浏览器直接导航到 <div> ID为“三”。

$(function () {
    // Scroll to function
    function scrollTo(ele) {
        $("html, body").animate({
            scrollTop: $(ele).offset().top - $("header").outerHeight()
        });
    }

    // Detect location hash
    if (window.location.hash) {
        scrollTo(window.location.hash);
    }

    // Detect click event
    $("header a[href^='#']").click(function (e) {
        var target = $(this).attr("href");
        scrollTo(target);
        e.preventDefault();
    });
});

关于html - 当用户单击 anchor 链接时,某些内容会出现在固定标题下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19471486/

相关文章:

javascript - 在for循环中绑定(bind)点击事件

html - 不同分辨率的 CSS 问题

css - 修复了 twitter bootstrap 2.0 中的侧边栏导致重叠

html - CSS 文件未在子目录中链接

javascript - 无法运行 JQuery 函数

javascript - 使用 javascript/jquery 一次打开多个页面

html - CSS 定位

html - chrome 移动版 VS chrome 桌面版 : different position of the same element

html - Css hover image transition 行为怪异

javascript - Angular 路由器导出冷却不占据所有宽度