看到這個(gè)題目你可能覺得這是什么鬼? 其實(shí)我想說的是這種,看下面的錄制:
這種交互在H5頁面中比比皆是,點(diǎn)擊城市->彈出城市選擇浮層->按返回按鈕關(guān)閉浮層。
這些操作都是不要點(diǎn)擊左上角/右上角的關(guān)閉按鈕就可以進(jìn)行的,飛豬的H5是前進(jìn)出現(xiàn)彈層,返回時(shí)彈層關(guān)閉,其他家都不行(去哪兒網(wǎng)H5飛機(jī)票,美團(tuán)H5酒店)。
為什么要這么設(shè)計(jì)?
因?yàn)镠5是在手機(jī)上操作的,手機(jī)上的手指可操作區(qū)域的覆蓋范圍很小,更別說左上角/右上角這些死角(取消/關(guān)閉)區(qū)域了。你肯定聽過這個(gè)操作:輕觸返回。這個(gè)在用戶操作的時(shí)候非常方便友好,選擇完城市后,不需要點(diǎn)擊取消,直接在大拇指可以操作的地方點(diǎn)擊返回就關(guān)閉了彈層。
如何實(shí)現(xiàn)
既然有這種非常好的需求,那作為開發(fā)肯定就會(huì)想法設(shè)法的實(shí)現(xiàn)這個(gè)功能了。 你甚至都不用google,你就應(yīng)該會(huì)想到類似的history.back(),history.go()這些方法了。 然而想到這些依舊沒用,理論上 瀏覽器/webview 的返回/前進(jìn)的是要重新加載頁面的,因?yàn)閁RL發(fā)生了變化。 如果你真的知道單頁面應(yīng)用(SPA),或者使用React/Vue你就應(yīng)該知道有個(gè)東西叫:路由。 這些通過改變hash且無法刷新的url變化是HTML5時(shí)加入的history功能
the-history-interface
interface History { readonly attribute unsigned long length; attribute ScrollRestoration scrollRestoration; readonly attribute any state; void go(optional long delta = 0); void back(); void forward(); void pushState(any data, DOMString title, optional DOMString? url = null); void replaceState(any data, DOMString title, optional DOMString? url = null);};
還有一個(gè)事件
pushState,replaceState 用來改變histroy堆棧順序,onpopstate 在返回,前進(jìn)的時(shí)候觸發(fā)
vue-router中的實(shí)現(xiàn)也是如此(第1694行)
具體實(shí)現(xiàn)
既然說了這么多,那我們來看下怎么實(shí)現(xiàn)這種功能。
來看下 pushState 和 replaceState 的兼容性
全綠,用起來放心多了。
思路:
<button onclick="city()"> 城市 </button><br> <button onclick="calendar()"> 日歷 </button><br> <button onclick="description()"> 說明 </button> <div id="city" class="com" style="display: none;"> 模擬城市彈框?qū)? </div> <div id="calendar" class="com" style="display: none;"> 模擬日歷彈框?qū)? </div> <div id="description" class="com" style="display: none;"> 模擬說明彈框?qū)? </div>
button { border: #0000; background-color: #f90; } .com { position: absolute ; top: 0; bottom: 0; left: 0; right: 0; background-color: #888589; }
var cityNode = document.getElementById('city'); var calendarNode = document.getElementById('calendar'); var descriptionNode = document.getElementById('description'); function city() { cityNode.style.display = 'block'; window.history.pushState({'id':'city'},'','#city') } function calendar() { calendarNode.style.display = 'block'; window.history.pushState({'id':'calendar'},'','#calendar') } function description() { descriptionNode.style.display = 'block'; window.history.pushState({'id':'description'},'','#description') } window.addEventListener('popstate', function(e){ // alert('state:' + e.state + ', historyLength:' + history.length); if (e.state && e.state.id === 'city') { history.replaceState('','','#'); cityNode.style.display = 'block'; } else if (e.state && e.state.id === 'calendar') { history.replaceState('','','#'); calendarNode.style.display = 'block'; } else if (e.state && e.state.id === 'description') { history.replaceState('','','#'); descriptionNode.style.display = 'block'; } else { cityNode.style.display = 'none'; calendarNode.style.display = 'none'; descriptionNode.style.display = 'none'; } })
主要看 JS 代碼,監(jiān)聽頁面的前進(jìn)和后退事件來控制history。
源碼在此
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選