CSS Scroll Snapping與scroll-snap-align

新屬性scroll-snap-type與scroll-snap-align
大漠老師小demo


<!DOCTYPE html>
<html lang="en" >

<head>

    <meta charset="UTF-8">
    <link rel="shortcut icon" type="image/x-icon"  />
    <link rel="mask-icon" type=""  color="#111" />
    <title>CodePen - Creating horizontal scrolling containers the right way</title>


    <link rel="stylesheet" >


    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        :root {
            --gutter: 20px;
        }

        ul, li {
            list-style: none outside none;
        }

        body {
            display: flex;
            justify-content: center;
        }

        .app {
            width: 375px;
            height: 667px;
            background: #DBD0BC;
            overflow-y: scroll;
            padding: 30px 0;
            margin: 0 10px;
        }

        .item {
            background: url("https://images.pexels.com/photos/1474657/pexels-photo-1474657.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=350") no-repeat center, linear-gradient(to bottom, #f36, #890), url("https://images.pexels.com/photos/1473095/pexels-photo-1473095.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=350") no-repeat center;
            background-size: cover;
            background-blend-mode: overlay, screen;
            color: #fff;
        }

        .hs-flex {
            padding: 0 20px;
            overflow-x: scroll;
            scroll-snap-type: x proximity;
            display: flex;
        }
        .hs-flex .item {
            scroll-snap-align: center;
            padding: calc(var(--gutter) / 2 * 1.5);
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            border-radius: 8px;
            min-width: calc(50% - var(--gutter) * 2);
            max-width: calc(50% - var(--gutter) * 2);
            min-height: 150px;
        }
        .hs-flex .item:not(:last-child) {
            margin-right: calc(var(--gutter) / 2);
        }

        .app-grid {
            display: grid;
            grid-gap: var(--gutter) 0;
            grid-template-columns: var(--gutter) 1fr var(--gutter);
            align-content: start;
        }

        .hs-grid {
            display: grid;
            grid-gap: calc(var(--gutter) / 2);
            grid-template-columns: repeat(6, calc(50% - var(--gutter) * 2));
            grid-template-rows: minmax(150px, 1fr);
            grid-column: 1 / -1;
            padding: 0 20px;
            overflow-x: scroll;
            scroll-snap-type: x proximity;
        }
        .hs-grid .item {
            scroll-snap-align: center;
            padding: calc(var(--gutter) / 2 * 1.5);
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            border-radius: 8px;
        }

    </style>
</head>

<body translate="no" >

<div class="app app-flex">
    <ul class="hs-flex">
        <li class="item">test</li>
        <li class="item">test</li>
        <li class="item">test</li>
        <li class="item">test</li>
        <li class="item">test</li>
        <li class="item">test</li>
    </ul>
</div>
<div class="app app-grid">
    <ul class="hs-grid">
        <li class="item">test</li>
        <li class="item">test</li>
        <li class="item">test</li>
        <li class="item">test</li>
        <li class="item">test</li>
        <li class="item">test</li>
    </ul>
</div>

</body>

</html>
 
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容