首页 > 解决方案 > 滚动捕捉无法正确捕捉

问题描述

我正在做一个个人项目,我在这个项目中使用 scroll-snap

但由于某种原因,滚动捕捉有时会超出或落在页面两个部分之间的尴尬位置

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@100&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Titillium+Web:wght@600&display=swap');

:root {
    --bg-color: rgb(33, 32, 41);
    --header-color: rgb(255, 170, 55);
    --color-white: white;
    --color-black: black;
}
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    
    scroll-behavior: smooth;
}
html{
    scroll-snap-type: y mandatory;
}
html, body {
    font-family: 'Titillium Web', sans-serif;
    background-color: var(--bg-color);
    color: var(--color-white);
    width: 100%;
}
body div {
    scroll-snap-align: start;
}






header {
    background: var(--header-color);
    position: fixed;
    top: 0;
    z-index: 10000;
    width: 100%;
}

header div {
    width: 80%;
    margin: 0 auto;
}
header div::after {
    content: '';
    display: table;
    clear: both;
}

header div img {
    position: absolute;
    float: left;

    margin: 0.6em;
}

nav ul {
    width: auto;
    float: right;

    margin: 2em;
}

nav ul li {
    display: inline-block;
    margin-left: 2em;
    font-family: 'Roboto', sans-serif;
}

nav ul li a {
    font-size: 1.5em;

    color: black;
    text-decoration: none;
    text-transform: uppercase;
}
nav ul li a:hover {
    color: rgb(93, 93, 93);
}






#about {
    display: flex;
    justify-content: center;
    align-items: center;

    width: clamp(750px, 80%, 100%);
    height: 100vh;

    margin: 0 auto;

    text-align: center;

    font-size: 1.5em;
}
#about p {
    display: inline;
    color: var(--header-color);
}
#about h1 {
    margin-bottom: 1em;
}





#invite {
    display: flex;
    justify-content: center;
    align-items: center;

    height: 100vh;

    margin: 0 auto;

    text-align: center;

    font-size: 1.5em;

    background-color: var(--header-color);
    color: var(--color-black);
}
#invite div {
    width: clamp(750px, 80%, 100%);

}
#invite div h1 {
    margin-bottom: 2em;
}
#invite div button {
    width: 6em;
    height: 3em;

    background-color: rgba(0, 0, 0, 0);
    color: var(--color-black);

    font-size: 1em;
    font-family: 'Roboto', sans-serif;
    text-transform: uppercase;
    
    border: 1px solid black;

    transition: 0.2s;
}
#invite button:hover {
    margin-top: 0.3em;
}
#invite button a {
    color: var(--color-black);
    text-decoration: none;
}





#contact {
    display: flex;
    justify-content: center;
    align-items: center;

    width: clamp(750px, 80%, 100%);
    height: 100vh;

    margin: 0 auto;

    text-align: center;

    font-size: 1.5em;
}
#contact div h1 {
    margin-bottom: 1em;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FoxoBot</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    
    <header>
        <div>
            <img src="files/logo.png" alt="Logo" height="72em"> 

            <nav>
                <ul>
                    <li><a href="#about">About</a></li>
                <!--<li><a href="#showcase">showcase</a></li>-->
                    <li><a href="#invite">Invite</a></li>
                    <li><a href="#contact">contact</a></li>
                </ul>
            </nav>
        </div>
    </header>

    <div id="about">
        <div>
            <h1>About</h1>

            <p><p>FoxoBot</p> is a Discord bot developed by Lappland and Ralkey.</p>
            <br>
            <p><p>FoxoBot</p> is a easy to use and a friendly bot for general, fun & administrative commands on Discord</p>
            <br>
            <p><p>FoxoBot</p> is always in development and we also learn alot along the way</p>
        </div>
    </div>

<!--<div id="showcase">
    -- i dont have anything for showcase yet --
    </div>-->

    <div id="invite">
        <div>
            <h1>Invite FoxoBot to your server</h1>
            <button><a href="#" target="_blank">Invite!</a></button>
        </div>
    </div>
    
    <div id="contact">
        <div>
            <h1>Contact us</h1>

            <p>you can contact us throught Discord.</p>
            <p>our tags are:</p>
            <p>Ralkey: blank</p>
            <p>Lappland: blank</p>
        </div>
    </div>
    
</body>
</html>

(我建议在整页中打开片段)

经过无数时间寻找解决方案后,我放弃并降落在这里

我希望你们中的至少一个能够帮助我

标签: htmlcssscroll-snapscroll-snap-type

解决方案


看来您的问题是您正在应用scroll-snap-align: start;到每个div内部body,因此当您滚动它时,它会捕捉到页面中的每个 div。而您只想将其应用于body页面的第一个子项或您的情况下的每个部分。

因此,我在下面的示例中所做的一切都更改body divbody > div您的 css。您可以在此处查看有关大于号的更多信息:“>”(大于号)CSS 选择器是什么意思?

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@100&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Titillium+Web:wght@600&display=swap');

:root {
    --bg-color: rgb(33, 32, 41);
    --header-color: rgb(255, 170, 55);
    --color-white: white;
    --color-black: black;
}
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    
    scroll-behavior: smooth;
}
html{
    scroll-snap-type: y mandatory;
}
html, body {
    font-family: 'Titillium Web', sans-serif;
    background-color: var(--bg-color);
    color: var(--color-white);
    width: 100%;
}
body > div {
    scroll-snap-align: start;
}






header {
    background: var(--header-color);
    position: fixed;
    top: 0;
    z-index: 10000;
    width: 100%;
}

header div {
    width: 80%;
    margin: 0 auto;
}
header div::after {
    content: '';
    display: table;
    clear: both;
}

header div img {
    position: absolute;
    float: left;

    margin: 0.6em;
}

nav ul {
    width: auto;
    float: right;

    margin: 2em;
}

nav ul li {
    display: inline-block;
    margin-left: 2em;
    font-family: 'Roboto', sans-serif;
}

nav ul li a {
    font-size: 1.5em;

    color: black;
    text-decoration: none;
    text-transform: uppercase;
}
nav ul li a:hover {
    color: rgb(93, 93, 93);
}






#about {
    display: flex;
    justify-content: center;
    align-items: center;

    width: clamp(750px, 80%, 100%);
    height: 100vh;

    margin: 0 auto;

    text-align: center;

    font-size: 1.5em;
}
#about p {
    display: inline;
    color: var(--header-color);
}
#about h1 {
    margin-bottom: 1em;
}





#invite {
    display: flex;
    justify-content: center;
    align-items: center;

    height: 100vh;

    margin: 0 auto;

    text-align: center;

    font-size: 1.5em;

    background-color: var(--header-color);
    color: var(--color-black);
}
#invite div {
    width: clamp(750px, 80%, 100%);

}
#invite div h1 {
    margin-bottom: 2em;
}
#invite div button {
    width: 6em;
    height: 3em;

    background-color: rgba(0, 0, 0, 0);
    color: var(--color-black);

    font-size: 1em;
    font-family: 'Roboto', sans-serif;
    text-transform: uppercase;
    
    border: 1px solid black;

    transition: 0.2s;
}
#invite button:hover {
    margin-top: 0.3em;
}
#invite button a {
    color: var(--color-black);
    text-decoration: none;
}





#contact {
    display: flex;
    justify-content: center;
    align-items: center;

    width: clamp(750px, 80%, 100%);
    height: 100vh;

    margin: 0 auto;

    text-align: center;

    font-size: 1.5em;
}
#contact div h1 {
    margin-bottom: 1em;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FoxoBot</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    
    <header>
        <div>
            <img src="files/logo.png" alt="Logo" height="72em"> 

            <nav>
                <ul>
                    <li><a href="#about">About</a></li>
                <!--<li><a href="#showcase">showcase</a></li>-->
                    <li><a href="#invite">Invite</a></li>
                    <li><a href="#contact">contact</a></li>
                </ul>
            </nav>
        </div>
    </header>

    <div id="about">
        <div>
            <h1>About</h1>

            <p><p>FoxoBot</p> is a Discord bot developed by Lappland and Ralkey.</p>
            <br>
            <p><p>FoxoBot</p> is a easy to use and a friendly bot for general, fun & administrative commands on Discord</p>
            <br>
            <p><p>FoxoBot</p> is always in development and we also learn alot along the way</p>
        </div>
    </div>

<!--<div id="showcase">
    -- i dont have anything for showcase yet --
    </div>-->

    <div id="invite">
        <div>
            <h1>Invite FoxoBot to your server</h1>
            <button><a href="#" target="_blank">Invite!</a></button>
        </div>
    </div>
    
    <div id="contact">
        <div>
            <h1>Contact us</h1>

            <p>you can contact us throught Discord.</p>
            <p>our tags are:</p>
            <p>Ralkey: blank</p>
            <p>Lappland: blank</p>
        </div>
    </div>
    
</body>
</html>


推荐阅读