<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); } } });