一.history
(1).history基础
history.forward()为前进,但是如果前进到头了则不会有反应,后退也是
(2).replaceState
history.replaceState('this is a test',null,'test')此时浏览器地址后面会变为/test
第二个参数是用来设置网页条目名称但是很多浏览器不支持了一般设置为null
(3).pushState
history.pushState("pushState",null,"pushState.html")
浏览器不会检查pushState.html是否存在,会直接替换。
第三个参数 url 不会立马加载页面 会在重新访问时加载页面,新的url必需和旧的是同源的,否则会报错
(4).示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.container{
width: 800px;
height: 200px;
}
.box{
float: left;
width: 150px;
height: 150px;
margin: 20px;
color: #fff;
font-size: 30px;
font-weight: bold;
text-align: center;
line-height: 150px;
cursor: pointer;
transition: 1s;
}
.red{
background-color: red;
border: 5px solid red;
}
.green{
background-color: green;
border: 5px solid green;
}
.blue{
background-color: blue;
border: 5px solid blue;
}
.black{
background-color: black;
border: 5px solid black;
}
.box:hover{
background-color: #fff;
color: #000;
}
.box.selected{
transform: scale(1.2);
}
</style>
</head>
<body>
<div class="container">
<div class="box red selected" id="box-1">one</div>
<div class="box green " id="box-2">two</div>
<div class="box blue" id="box-3">three</div>
<div class="box black" id="box-4">four</div>
</div>
<script>
let boxes = Array.from(document.getElementsByClassName('box'));
function selectBox(id){
boxes.forEach(item => {
item.classList.toggle('selected',item.id === id);
})
}
boxes.forEach(item =>{
item.addEventListener('click',e=>{
let id = item.id;
history.pushState({id},null,`selected=${id}`);
selectBox(id)
})
})
</script>
</body>
</html>
现在要通过popState来完成点击前进后退之前对应的盒子也放大
在每一次活动的历史条目发生改变的时候就会触发,新增的话(push.replace)是不会触发的
window.addEventListener('popstate',e=>{
let id = e.state.id;
selectBox(id)
})
(5).示例2
<body>
<input type="text" id="inputKwd"/>
<button id="btn">提交</button>
<div class="output"></div>
<script>
let data = [
{name:'VueJs'},
{name:'ReactJs'},
{name:'AngularJs'},
{name:'jQuery'},
{name:'HTML'},
{name:'CSS'}
]
let outputArea = document.getElementsByClassName('output')[0],
btn = document.getElementById('btn'),
inputKwd = document.getElementById('inputKwd');
btn.addEventListener('click',()=>{
let showData = data.filter(item=>{
return item.name.indexOf(inputKwd.value) !== -1;
})
renderDom(showData)
history.pushState({value:inputKwd.value},null,'#'+inputKwd.value)
})
function renderDom(data){
let outputStr = '';
for (let i = 0; i < data.length; i++) {
outputStr += '<p>'+data[i].name+'</p>'
outputArea.innerHTML = outputStr
}
}
renderDom(data)
</script>
</body>
window.addEventListener('popstate',e=>{
let value = e.state ? e.state.value :'';
let showData = data.filter(item=>{
return item.name.indexOf(value) !== -1;
})
inputKwd.value = value;
renderDom(showData);
},false)
(6).hashchange事件
<body>
<div>
<p id="slot">...</p>
<a href="#login">login</a>
<a href="#logout">logout</a>
</div>
<script>
let slot = document.getElementsByTagName('p')[0];
window.addEventListener('hashchange',e=>ouRouterChange(e))
function ouRouterChange(e){
const hashLocation = window.location.hash.substring(1);
loadingContent(hashLocation);
}
function loadingContent(uri){
const contentUri = `${uri}.html`;
fetch(contentUri).then(r =>t.text()).then(content => updateSlot(content));
}
function updateSlot(content){
slot.innerHTML = content;
}
</script>
</body>
建立loginout.html
二.worker
(1).使用方式
worker.js(无法访问window和document,可以写ajax请求)
(2).具体实例
this.onmessage=(e)=>{
if(e.data.addThis !==undefined){
let sumMessage = {result:e.data.addThis.num1+e.data.addThis.num2}
this.postMessage(sumMessage)
}
}
<script>
if(window.Worker){
let myWorker = new Worker('worker.js')
let message = {addThis:{num1:1,num2:3}}
myWorker.postMessage(message);
myWorker.onmessage=(e)=>{
console.log(e.data.result);
}
}
</script>