<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
position: relative;
left: -300px;
}
.box-left {
position: absolute;
width: 300px;
background-color: aquamarine;
height: 100vh;
}
.box-right {
position: absolute;
width: 750px;
background-color: bisque;
height: 100vh;
left: 300px;
}
button {
width: 100px;
height: 100px;
background-color: #fff;
}
</style>
</head>
<body>
<div class="box">
<div class="box-left">
</div>
<div class="box-right">
<button>点击</button>
</div>
</div>
</body>
<script>
var button = document.getElementsByTagName("button")[0];
var box = document.getElementsByClassName("box")[0];
button.onclick = function() {
var left = window.getComputedStyle(box).left;
if (left == "-300px") {
box.style.left = 0;
} else {
box.style.left = "-300px"
}
}
</script>
</html>
实例效果