html5获取网址返回,Html5页面中的返回实现的方法,html5页面返回实现分享

看到这个题目你可能觉得这是什么鬼? 其实我想说的是这种,看下面的录制:

270f017a5d180a88ecbffa65b762ad09.png

这种

这些操作都是不要点击左上角/右上角的关闭按钮就可以进行的,飞猪的H5是前进出现弹层,返回时弹层关闭,其他家都不行(去哪儿网H5飞机票,美团H5酒店)。

为什么要这么

因为H5是在

如何实现

既然有这种非常好的需求,那作为开发肯定就会想法设法的实现这个功能了。 你甚至都不用google,你就应该会想到类似的history.back(),history.go()这些方法了。 然而想到这些依旧没用,理论上 浏览器/

the-history-interface

%pushState

replaceState

还有一个事件

onpo

pushState,replaceState 用来改变histroy堆栈顺序,onpopstate 在返回,前进的时候触发

vue-router中的实现也是如此(第1694行)

具体实现

既然说了这么多,那我们来看下怎么实现这种功能。

来看下 pushState 和 replaceState 的兼容性

270f017a5d180a88ecbffa65b762ad09.png

全绿,用起来放心多了。

思路:

点击弹层时 pushState 添加 hash

"轻触返回"的时候触发 onpopstate 事件时候隐藏弹层并修改 hash

城市 日历 说明

模拟城市弹框层
模拟日历弹框层
模拟说明弹框层

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 代码,监听页面的前进和后退事件来控制history。

270f017a5d180a88ecbffa65b762ad09.png

源码在此

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持登博教程。

—-想了解更多的

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/html5/110769.html

发表评论邮箱地址不会被公开。 必填项已用*标注

*昵称:

*邮箱:

网址:

记住昵称、邮箱和网址,下次评论免输入

提交

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值