【发布时间】:2019-09-06 17:35:28
【问题描述】:
我正在尝试实现一个书本视图,其中一次可见一页,用户可以滑动浏览每一页进行导航。所有页面的大小都相同,我必须以 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/ 这个问题在这里不会发生。
我无法理解并解决此问题。有人可以帮忙吗?
【问题讨论】:
-
这有什么帮助?我尝试将包装容器 div 的左滚动设置为当前页面不乘以一页宽度。但这会导致闪烁效果,并且还会触发导致问题的滚动事件。我正在使用自定义回调处理滚动事件。
-
这个问题首先可能是什么原因。
标签: javascript html css snapping