<body class="layout-fixed"> <!-- fixed定位的头部 --> <header> </header> <!-- 能够滚动的区域 --> <main> <!-- 内容在这里... --> </main> <!-- fixed定位的底部 --> <footer> <input type="text" placeholder="Footer..."/> <button class="submit">提交</button> </footer> </body>
header, footer, main { display: block; } header { position: fixed; height: 50px; left: 0; right: 0; top: 0; } footer { position: fixed; height: 34px; left: 0; right: 0; bottom: 0; } main { margin-top: 50px; margin-bottom: 34px; height: 2000px }
<body class="layout-scroll-fixed"> <!-- fixed定位的头部 --> <header> </header> <!-- 能够滚动的区域 --> <main> <div class="content"> <!-- 内容在这里... --> </div> </main> <!-- fixed定位的底部 --> <footer> <input type="text" placeholder="Footer..."/> <button class="submit">提交</button> </footer> </body>
header, footer, main { display: block; } header { position: fixed; height: 50px; left: 0; right: 0; top: 0; } footer { position: fixed; height: 34px; left: 0; right: 0; bottom: 0; } main { /* main绝对定位,进行内部滚动 */ position: absolute; top: 50px; bottom: 34px; /* 使之能够滚动 */ overflow-y: scroll; } main .content { height: 2000px; }
main { /* main绝对定位,进行内部滚动 */ position: absolute; top: 50px; bottom: 34px; /* 使之能够滚动 */ overflow-y: scroll; /* 增长该属性,能够增长弹性 */ -webkit-overflow-scrolling: touch; }
为了防止页面露底,能够在页面拖拽到边缘的时候,经过判断拖拽方向以及是否为边缘来阻止 touchmove 事件,防止页面继续拖拽。css
以上面内滚动 layout-scroll-fixed 布局为例,给出一段代码做为参考html
// 防止内容区域滚到底后引发页面总体的滚动 var content = document.querySelector('main'); var startY; content.addEventListener('touchstart', function (e) { startY = e.touches[0].clientY; }); content.addEventListener('touchmove', function (e) { // 高位表示向上滚动 // 底位表示向下滚动 // 1允许 0禁止 var status = '11'; var ele = this; var currentY = e.touches[0].clientY; if (ele.scrollTop === 0) { // 若是内容小于容器则同时禁止上下滚动 status = ele.offsetHeight >= ele.scrollHeight ? '00' : '01'; } else if (ele.scrollTop + ele.offsetHeight >= ele.scrollHeight) { // 已经滚到底部了只能向上滚动 status = '10'; } if (status != '11') { // 判断当前的滚动方向 var direction = currentY - startY > 0 ? '10' : '01'; // 操做方向和当前容许状态求与运算,运算结果为0,就说明不容许该方向滚动,则禁止默认事件,阻止滚动 if (!(parseInt(status, 2) & parseInt(direction, 2))) { stopEvent(e); } } });