超级炒鸡丝滑的图片展示框
首先看下效果图:
html:
我们全部使用div来完成,主要看css样式
<body>
<div class="container">
<div class="panel active" style="background-image: url('./images/timg-001-01.png')">
<h3>Explore The World</h3>
</div>
<div class="panel" style="background-image: url('./images/timg-001-02.png')">
<h3>Wild Forest</h3>
</div>
<div class="panel" style="background-image: url('./images/timg-001-03.png')">
<h3>Sunny Beach</h3>
</div>
<div class="panel" style="background-image: url('./images/timg-001-04.png')">
<h3>City on Winter</h3>
</div>
</div>
</body>
css:
这才是重头戏。
/* 加载字体 */
@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');
/* 定义了如何计算一个元素的总宽高: border-box不包含margin */
* {
box-sizing: border-box;
}
/* 采用flex布局 */
body {
font-family: 'Muli', sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
margin: 0;
}
.container {
display: flex;
width: 90vw;
}
.panel {
background-size: cover;
background-position: center;
background-repeat: no-repeat;
height: 80vh;
border-radius: 50px;
color: #fff;
cursor: pointer;
flex: 0.5;
margin: 10px;
position: relative;
-webkit-transition: all 700ms ease-in;
}
.panel h3 {
font-size: 24px;
position: absolute;
bottom: 20px;
left: 20px;
margin: 0;
opacity: 0;
}
.panel.active {
flex: 5;
}
.panel.active h3 {
opacity: 1;
transition: opacity 0.3s ease-in 0.4s;
}
@media (max-width: 480px) {
.container {
width: 100vw;
}
.panel:nth-of-type(4),
.panel:nth-of-type(5) {
display: none;
}
}
js:
添加事件
const panels = document.querySelectorAll('.panel')
panels.forEach(panel => {
panel.addEventListener('click', () => {
removeActiveClasses()
panel.classList.add('active')
})
})
function removeActiveClasses() {
panels.forEach(panel => {
panel.classList.remove('active')
})
}
总体的一个思路呢就是:通过flex布局
中的flex
属性,
首先解释一下flex
这个属性:是flex-grow,flex-shrink 和 flex-basis
的简写,默认值为0 1 auto。
然后添加点击事件,在点击的panel添加一个active的class。将点击的元素放大即可实现效果。然后添加过渡动画增加美观性。
- flex-grow: 定义项目的放大比例,默认为0.
- flex-shrink: 定义项目饿缩小比例,默认为1.
- flex-basis: 定义在分配多余空间之前,项目占据的主轴空间
通过flex-grow的改变,实现一个图片预览的效果,然后添加transition
过渡动画,这样一个简单实用的图片预览框就做出来啦~~~