0

我正在尝试实现一个书本视图,其中一次可见一页,用户可以滑动浏览每一页进行导航。所有页面的大小都相同,我必须以 100% 的视口宽度显示每个页面。我的问题是,当我在没有 X 的页面上并尝试调整浏览器窗口的大小时,我最终会在其他页面没有。我希望它只保留在第 X 页。当我在第一页或最后一页时,这不会发生。我为此创建了一个小提琴。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获得灵感。这有一个水平滚动的菜单。我为此创建了一个小提琴:https ://jsfiddle.net/xyzasmb2/ 这个问题不会在这里发生。

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

4

2 回答 2

2

问题是内容随容器缩放,因为它是容器宽度的倍数......而 scrollLeft 位置保持不变,以左边缘的像素为单位。目前我能想到的解决这个问题的唯一方法是检测容器的调整大小,但您只能绑定到窗口的调整大小,而不是单个元素的调整大小事件......除了使用一些技巧:如何检测 DIV 的尺寸改变了吗?

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

于 2019-11-16T14:45:52.520 回答
0

就像皮特说的那样,问题出在 ScrollLeft 位置。您可以尝试在 window.onresize 上执行添加的功能。

window.onload = reorder;

function reorder(){
element.scrollLeft +=1}

因为滚动是强制性的,如果您调整它的大小,它将重新排序。每次调整大小时,都会添加 1,然后在循环中订购强制 x。所以总是它调整大小它会保持居中。

于 2021-12-29T06:39:51.143 回答