javascript - 在 CSS 滚动捕捉中调整浏览器大小的问题

标签 javascript html css snapping

我正在尝试实现一个书本 View ,其中一页一次可见,用户可以轻扫每一页进行导航。所有页面的大小都相同,我必须在 100% 视口(viewport)宽度上显示每个页面。我的问题是,当我在没有 X 的页面上并尝试调整浏览器窗口的大小时,我最终会在其他页面上没有。我希望它只保留在第 X 页。当我在第一页或最后一页时,这不会发生。我为此创建了一个 fiddle 。 https://jsfiddle.net/8y5swhj9/ .

<!DOCTYPE html>
    <html>
    <head>
        <title>POC</title>
        <style type="text/css">
            .container {
                -webkit-box-sizing: border-box;
                box-sizing: border-box;
                display: -webkit-box;
                display: -ms-flexbox;
                display: flex;
                -ms-flex-item-align: start;
                outline: none;
                -webkit-overflow-scrolling: touch;
                -ms-overflow-style: none;
                overflow-x: scroll;
                overflow-y: hidden;
                overflow-y: -moz-hidden-unscrollable;
                -ms-scroll-chaining: none;
                -ms-scroll-snap-coordinate: 100vw 0;
                scroll-snap-coordinate: 100vw 0;
                -ms-scroll-snap-destination: 100vw 0;
                scroll-snap-destination: 100vw 0;
                -ms-scroll-snap-points-x: snapInterval(0%, 100%);
                -ms-scroll-snap-points-x: repeat(100vw);
                scroll-snap-points-x: repeat(100vw);
                -ms-scroll-snap-type: mandatory;
                -ms-scroll-snap-type: x mandatory;
                scroll-snap-type: x mandatory;
                scroll-snap-type: mandatory;
                scrollbar-width: none;
                width: 100%;
            }
            .page {
                align-items: center;
                display: flex;
                height: 100%;
                justify-content: center;
                min-width: 100%;
                position: relative;
                scroll-snap-align: start;
                width: 100%;
                box-shadow: inset 0 0 20px #000000;
            }
        </style>
    </head>
    <body>
        <div class="container" style="height: 359px;">
            <div class="page">
                <h1>1</h1>
            </div>
            <div class="page">
                <h1>2</h1>
            </div>
            <div class="page">
                <h1>3</h1>
            </div>
            <div class="page">
                <h1>4</h1>
            </div>
            <div class="page">
                <h1>5</h1>
            </div>
            <div class="page">
                <h1>6</h1>
            </div>
            <div class="page">
                <h1>7</h1>
            </div>
            <div class="page">
                <h1>8</h1>
            </div>
            <div class="page">
                <h1>9</h1>
            </div>
            <div class="page">
                <h1>10</h1>
            </div>
        </div>
    </body>
    </html>

在这段代码中,如果你移动到一个中心位置,比如第 6 页,然后横向调整浏览器窗口的大小,你会看到整个内容开始流动,我们最终出现在某个随机的第 6 页上。

我的灵感来自 https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_menu_hor_scroll .这有一个水平滚动的菜单。我为此创建了一个 fiddle :https://jsfiddle.net/xyzasmb2/ 这里不会出现这个问题。

我无法理解和解决这个问题。有人可以帮忙吗?

最佳答案

问题是内容随容器缩放,因为它是容器宽度的倍数……而 scrollLeft 位置保持不变,以距左边缘的像素为单位。 目前我能想到的解决这个问题的唯一方法是检测容器的大小调整,但你只能绑定(bind)到窗口的调整大小,而不是单个元素的调整大小事件......除了使用一些技巧:How to detect DIV's dimension changed?

我希望有一种方法可以将 scrollLeft 位置指定为总宽度的百分比,这样就可以解决这个问题。

关于javascript - 在 CSS 滚动捕捉中调整浏览器大小的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57826420/

相关文章:

javascript - 将javascript变量附加到html文本区域

php - 文件上传 Ajax PHP

html - 如何使 Bootstrap 容器自动调整高度?

css - 防止@keyframe变换: rotate() from skipping its initial transform: rotate() value at the end of its animation cycle

javascript - 调试适用于 Office 的 JavaScript API

javascript - 将 json 响应数组的值映射到预定义接口(interface)

javascript - 如何在 Javascript 中更改关键帧的 CSS 属性?

javascript - 使用来自多个输入的数据更改段落文本

javascript - 如何在现代浏览器中单击 html <a> 标记或 javascript 时触发文件下载

javascript - 使用 javascript 从图像数组更改 img 标签 src。骰子游戏