主要思路:通过鼠标悬浮控制每一个半边的宽度变化
<!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>split landing page</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="container">
<div class="split left">
<h1>Page Left</h1>
<a href="#" class="btn">qaq</a>
</div>
<div class="split right">
<h1>Page Right</h1>
<a href="#" class="btn">qaq</a>
</div>
</div>
<script src="./script.js"></script>
</body>
</html>
:root {
--left-bg-color: rgba(105, 104, 179, 0.7);
--right-bg-color: rgba(43, 43, 43, 0.8);
--left-btn-hover-color: rgba(87, 84, 236, 1);
--right-btn-hover-color: rgb(36, 41, 36);
--hover-width: 75%;
--other-width: 25%;
--speed: 1000ms;
}
*{
box-sizing: border-box;
}
body{
height: 100vh;
margin: 0;
}
h1{
position: absolute;
top: 20%;
left: 50%;
transform: translateX(-50%);
font-size: 4rem;
color: #fff;
white-space: nowrap;
}
.container{
position: relative;
width: 100%;
height: 100%;
}
.split{
position: absolute;
width: 50%;
height: 100%;
}
.left{
left: 0;
background: url('../public/images/1.webp') no-repeat center center/cover;
}
.right{
right: 0;
background: url('../public/images/4.webp') no-repeat center center/cover;
}
.btn{
position: absolute;
top: 40%;
left: 50%;
transform: translateX(-50%);
display: flex;
justify-content: center;
align-items: center;
text-decoration: none;
color: #fff;
font-size: 1rem;
font-weight: bold;
border: 0.2rem solid #fff;
width: 14rem;
padding: 1.5rem;
text-transform: uppercase;
}
.left .btn:hover{
border-color: var(--left-btn-hover-color);
background-color: var(--left-btn-hover-color);
}
.right .btn:hover{
border-color: var(--right-btn-hover-color);
background-color: var(--right-btn-hover-color);
}
.left::before{
position: absolute;
content: "";
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: var(--left-bg-color);
}
.right::before{
position: absolute;
content: "";
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: var(--right-bg-color);
}
.hover-left .left{
width: var(--hover-width);
}
.hover-left .right{
width: var(--other-width);
}
.hover-right .left{
width: var(--other-width);
}
.hover-right .right{
width: var(--hover-width);
}
.right,
.left,
.right::before,
.left::before{
transition: all var(--speed) ease-in-out;
}
const container = document.querySelector('.container')
const left = document.querySelector('.left')
const right = document.querySelector('.right')
left.addEventListener('mouseenter', () => {container.classList.add('hover-left')})
left.addEventListener('mouseleave', () => {container.classList.remove('hover-left')})
right.addEventListener('mouseenter', () => {container.classList.add('hover-right')})
right.addEventListener('mouseleave', () => {container.classList.remove('hover-right')})
效果如下
Q.E.D.