和上一个案例 拖拽是连在一起的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
//设置另外一个盒子
.box1 {
width: 100px;
height: 100px;
background: blue;
position: absolute;
left: 0;
top: 0;
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
<script>
var obox = document.querySelector(".box1");
//开始时也要记录盒子移动位置
var pos = JSON.parse(localStorage.getItem("pos"))
obox.style.left = pos.l + "px";
obox.style.top = pos.t + "px";
//通过window的属性onstorage来操作,当另一个盒子移动时,蓝色盒子也
//会移动
onstorage = function() {
var pos = JSON.parse(localStorage.getItem("pos"))
obox.style.left = pos.l + "px";
obox.style.top = pos.t + "px";
}
</script>
</html>