<style> * { margin: 0; padding: 0; } ul { list-style: none; } .wrapper { width: 100vw; height: 100vh; background: #000; display: flex; justify-content: center; align-items: center; } .wrapper ul { width: 80%; height: 80%; display: flex; justify-content: space-between; align-items: center; } .wrapper ul li { width: 16%; height: 100%; border-radius: 20px; background: #333; overflow: hidden; /* 这是第二次运动 ,关闭时候调用*/ transition: width .5s linear, height .5s linear .5s; } .wrapper ul li .inner { width: 100%; height: 100%; position: relative; transition: all .5s linear; } .wrapper .init li .inner { transform: translateY(100%); } .wrapper ul li .inner .bg { width: 100%; height: 100%; background-position: center; background-size: cover; cursor: pointer; opacity: 0.5; } .wrapper ul li .inner h2 { font-size: 16px; color: #fff; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); } .wrapper ul li:nth-of-type(1) .inner .bg { background-image: url("img/1.jpg"); } .wrapper ul li:nth-of-type(2) .inner .bg { background-image: url("img/2.jpg"); } .wrapper ul li:nth-of-type(3) .inner .bg { background-image: url("img/3.jpg"); } .wrapper ul li:nth-of-type(4) .inner .bg { background-image: url("img/4.jpg"); } .wrapper ul li:nth-of-type(5) .inner .bg { background-image: url("img/5.jpg"); } .wrapper ul li:nth-of-type(6) .inner .bg { background-image: url("img/6.jpg"); } .wrapper ul li:hover .inner .bg { opacity: 1; transition: all .2s linear; } .wrapper ul li:hover h2 { font-size: 24px; transition: all .2s linear; } .wrapper ul li:nth-of-type(1) .inner { transition-delay: 0.1s; } .wrapper ul li:nth-of-type(2) .inner { transition-delay: 0.2s; } .wrapper ul li:nth-of-type(3) .inner { transition-delay: 0.3s; } .wrapper ul li:nth-of-type(4) .inner { transition-delay: 0.4s; } .wrapper ul li:nth-of-type(5) .inner { transition-delay: 0.5s; } .wrapper ul li:nth-of-type(6) .inner { transition-delay: 0.6s; } .wrapper ul .inner .direction { width: 100%; height: 30px; position: absolute; top: 50px; padding: 0 30px; display: flex; justify-content: space-between; box-sizing: border-box; align-items: center; opacity: 0; } #activeWrap li .inner h2 { opacity: 0; transition: all 0.2s linear; } #activeWrap li:not(.activeLi) { width: 0; height: 0; transition: all .5s linear; } #activeWrap li.activeLi { width: 100%; } #activeWrap li.activeLi .inner .bg { opacity: 1; } #activeWrap li { transition: height .5s linear, width .5s linear .5s; } .activeLi .inner .direction .title { color: #ffff; font-size: 24px; } .activeLi .inner .direction .close { width: 30px; height: 30px; cursor: pointer; position: relative; } .activeLi .inner .direction .close::before, .activeLi .inner .direction .close::after { content: ''; width: 30px; height: 4px; position: absolute; top: 50%; margin-top: -2px; background: #fff; } .activeLi .inner .direction .close::before { transform: rotate(45deg); } .activeLi .inner .direction .close::after { transform: rotate(-45deg); } #activeWrap li .inner .direction { opacity: 1; transition: opacity .5s linear; transition-delay: 1s; } #activeWrap li .inner .direction .close { transform: rotate(360deg); transition: all 0.5s linear 1s; } </style>
结构
<div class="wrapper"> <ul class="init"> <li> <div class="inner"> <div class="bg"></div> <h2>image1</h2> <div class="direction"> <div class="title">bird1</div> <div class="close"></div> </div> </div> </li> <li> <div class="inner"> <div class="bg"></div> <h2>image2</h2> <div class="direction"> <div class="title">bird2</div> <div class="close"></div> </div> </div> </li> <li> <div class="inner"> <div class="bg"></div> <h2>image3</h2> <div class="direction"> <div class="title">bird3</div> <div class="close"></div> </div> </div> </li> <li> <div class="inner"> <div class="bg"></div> <h2>image4</h2> <div class="direction"> <div class="title">bird4</div> <div class="close"></div> </div> </div> </li> <li> <div class="inner"> <div class="bg"></div> <h2>image5</h2> <div class="direction"> <div class="title">bird5</div> <div class="close"></div> </div> </div> </li> <li> <div class="inner"> <div class="bg"></div> <h2>image6</h2> <div class="direction"> <div class="title">bird6</div> <div class="close"></div> </div> </div> </li> </ul> </div>
行为:
<script> (function () { var ul = document.querySelector('.init'); var lis = document.querySelectorAll('li'); var closes = document.querySelectorAll('.close'); var last = null; // 点击每个li打开 lis.forEach((li, index) => { li.onclick = function (ev) { // 给ul加上动态的id ul.setAttribute('id', 'activeWrap'); last && last.removeAttribute('class'); li.setAttribute('class', 'activeLi'); last = this; } closes[index].onclick = function (ev) { lis[index].className = ''; ul.removeAttribute('id'); last = null; ev.cancelBubble = true; } }) //动画显示出来 setTimeout(function () { ul.className = ''; }, 200) })() </script>
备注: 把图片换掉就可以实现以下效果:
里面带有一些动画,已经实现了,有兴趣的自己学习一下。