【发布时间】:2018-03-18 02:52:33
【问题描述】:
我想制作一个分为 2 个部分的首页(一个部分的页面宽度为 1/2),每当有人将鼠标悬停在一个部分上时,它应该占用页面宽度的 2/3。 当一个部分被点击时,它应该占据整个页面的宽度。
- HTML / CSS / JavaScript:
const leftPanel = document.getElementsByClassName('panel--left')[0];
const rightPanel = document.getElementsByClassName('panel--right')[0];
const mouseEnterHandler = e => {
e.target === leftPanel ? leftPanel.style.flexGrow = '2' : rightPanel.style.flexGrow = '2';
};
const mouseLeaveHandler = e => {
e.target === leftPanel ? leftPanel.style.flexGrow = '1' : rightPanel.style.flexGrow = '1';
};
const clickLeftPanelHandler = e => {
leftPanel.style.flexGrow = '1';
rightPanel.style.flexGrow = '0';
e.stopPropagation();
};
const clickRightPanelHandler = e => {
leftPanel.style.flexGrow = '0';
rightPanel.style.flexGrow = '1';
e.stopPropagation();
};
leftPanel.addEventListener('mouseenter', mouseEnterHandler);
rightPanel.addEventListener('mouseenter', mouseEnterHandler);
leftPanel.addEventListener('mouseleave', mouseLeaveHandler);
rightPanel.addEventListener('mouseleave', mouseLeaveHandler);
leftPanel.addEventListener('click', clickLeftPanelHandler);
rightPanel.addEventListener('click', clickRightPanelHandler);
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.container {
display: flex;
height: 100vh;
text-align: center;
width: 100%;
}
.panel {
display: flex;
flex-direction: column;
height: inherit;
justify-content: space-around;
padding: 25px;
transition: flex-grow 0.75s ease;
}
.panel--left {
background-color: #ffffff;
flex-grow: 1;
}
.panel--right {
background-color: #dddddd;
flex-grow: 1;
}
.panel--left:hover,
.panel--right:hover {
flex-grow: 2;
}
.panel--left:active,
.panel--right:active {
flex-grow: 1;
}
.panel__title {
color: #3a424a;
font-family: 'Roboto Mono', sans-serif;
font-size: 12px;
font-weight: 900;
letter-spacing: 1em;
margin: 0 0 45px 0;
text-transform: uppercase;
}
.panel__header {
color: #3a424a;
font-family: 'Roboto', sans-serif;
font-size: 48px;
font-weight: 900;
letter-spacing: 0.05em;
line-height: 60px;
margin: 0 auto 55px;
max-width: 525px;
text-transform: capitalize;
}
.panel__description {
color: #6d7d8c;
font-family: 'Roboto', sans-serif;
font-size: 14px;
font-weight: 400;
letter-spacing: 0.05em;
line-height: 30px;
margin: 0 auto 50px;
max-width: 625px;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<main>
<div class="container">
<section class="panel panel--left">
<div class="panel__text-content">
<p class="panel__title">Sample text</p>
<h1 class="panel__header">Sample text</h1>
<p class="panel__description">Phasellus nec semper ex, ac mollis eros. Maecenas diam nunc, iaculis id est vel, laoreet pulvinar nisl. Aenean vestibulum auctor nulla, a vulputate nibh mollis nec. Sed odio arcu, laoreet nec nulla quis, ornare tincidunt urna. Phasellus pellentesque quam sit amet erat consectetur dapibus.</p>
</div>
</section>
<section class="panel panel--right">
<div class="panel__text-content">
<p class="panel__title">Sample text</p>
<h1 class="panel__header">Sample text</h1>
<p class="panel__description">Phasellus nec semper ex, ac mollis eros. Maecenas diam nunc, iaculis id est vel, laoreet pulvinar nisl. Aenean vestibulum auctor nulla, a vulputate nibh mollis nec. Sed odio arcu, laoreet nec nulla quis, ornare tincidunt urna. Phasellus pellentesque quam sit amet erat consectetur dapibus.</p>
</div>
</section>
</div>
</main>
</body>
</html>
问题是我不知道如何使第二部分缩小到 0。我知道 flex-grow 的工作原理,但这是我能得到的最接近的外观。
希望我能理解地表达自己。
【问题讨论】:
标签: javascript html css flexbox