javascript - 如何确定我所在页面的哪一部分以便在我的导航栏上反射(reflect)出来?

标签 javascript html css vue.js

我创建了一个固定的侧边栏导航,由 3 个看起来像点的 div 组成,每个 div 代表我的一个页面部分。这些页面部分中的每一个都采用 100vh 高度。现在我想弄清楚如何确定滚动条在页面上的位置以及我在看哪个 div。

我正在使用 Vue.js,一旦我弄清楚我的问题,它肯定会让事情变得更容易。

为了做到这一点,我假设我需要的最低限度是 window.scrollYOffsetTop 和每个部分的 height为了确定 window.scrollY 是否位于该部分的 OffsetTop(该部分的起始位置)和 OffsetTop + height(该部分的结束位置)。到目前为止,我的想法是否正确?

到目前为止我有这个:

*, *:after, *:before {
        margin: 0;
        padding: 0;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        text-decoration: none;
        list-style-type: none;
        color: rgba(0, 0, 0, 0.8);
    }
    body {
        background-color: #f1f1f1;
        font-family: 'Roboto', 'Helvetica', 'Helvetica Neue', 'Arial', sans-serif;
        background-image: url("./assets/svg/topography.svg");
        background-attachment: fixed;
    }
    section {
        min-height: 100vh;
    }
    .navigation {
        display: flex;
        flex-direction: column;
        position: fixed;
        right: 5%;
        top: 50%;
        transform: translateY(-50%);
        text-align: right;
    }
    .navigation div {
        width: 20px;
        height: 20px;
        border-radius: 50%;
        margin-bottom: 30px;
        transition: all .1s linear;
        border: 5px solid rgba(0, 0, 0, 0.5);
        cursor: pointer;
    }
    .navigation div:hover {
        transform: scale(1.2)
    }
    .one {
    background-color: red;
    }
    .two {
    background-color: blue;
    }
    .three {
    background-color: green;
    }
<nav class='navigation'>
    <div></div>
    <div></div>
    <div></div>
</nav>
<section class='one'></section>
<section class='two'></section>
<section class='three'></section>

最佳答案

我们尝试在各部分之间的页面中导航的名称来自 emacs

*, *:after, *:before {
        margin: 0;
        padding: 0;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        text-decoration: none;
        list-style-type: none;
        color: rgba(0, 0, 0, 0.8);
    }
    body {
        background-color: #f1f1f1;
        font-family: 'Roboto', 'Helvetica', 'Helvetica Neue', 'Arial', sans-serif;
        background-image: url("./assets/svg/topography.svg");
        background-attachment: fixed;
    }
    section {
        min-height: 100vh;
    }
    .navigation {
        display: flex;
        flex-direction: column;
        position: fixed;
        right: 5%;
        top: 50%;
        transform: translateY(-50%);
        text-align: right;
    }
    .navigation div {
        width: 20px;
        height: 20px;
        border-radius: 50%;
        margin-bottom: 30px;
        transition: all .1s linear;
        border: 5px solid rgba(0, 0, 0, 0.5);
        cursor: pointer;
    }
    .navigation div:hover {
        transform: scale(1.2)
    }
    .one {
    background-color: red;
    }
    .two {
    background-color: blue;
    }
    .three {
    background-color: green;
    }
<nav class='navigation'>
    <div></div>
    <div></div>
    <div></div>
</nav>
<section class='one'></section>
<a name='one'></a>
<section class='two'></section>
<section class='three'></section>
<a href='#one'> go to section one </a>

关于javascript - 如何确定我所在页面的哪一部分以便在我的导航栏上反射(reflect)出来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58722110/

相关文章:

php - 如何对 html/php 网页进行反注释?

html - 创建侧边栏 - 滚动条显示问题 - HTML/CSS

html - 通过使用显示 : table-cell for table row, 显示:td 表的 block 不起作用

javascript - Tinymce image_class_list 不工作

javascript - 淡入不起作用

html - IE6 和 IE7 出现双水平边距的问题

css - 如何在某些后代上排除 Twitter Bootstrap

jquery - 使用 RotateY 缓慢加载卡片翻转

javascript - 当我尝试计算密码字段时,以下代码有什么问题?

javascript - 无法使用 GMAIL API 发送富文本电子邮件