最近浏览到一款相当震撼我的css,仅仅使用css,就能做出动态点击切换卡片的效果。
html源码
<div class="card">
<input type="radio" name="select" id="slide_1" checked>
<input type="radio" name="select" id="slide_2">
<input type="radio" name="select" id="slide_3">
<input type="checkbox" id="slideImg">
<div class="slider">
<label for="slide_1" class="slide slide_1"></label>
<label for="slide_2" class="slide slide_2"></label>
<label for="slide_3" class="slide slide_3"></label>
</div>
<div class="inner_part">
<label for="slideImg" class="img">
<img class="img_1" src="https://c4.wallpaperflare.com/wallpaper/978/131/617/kiz-kulesi-turkey-istanbul-maiden-s-tower-wallpaper-preview.jpg">
</label>
<div class="content content_1">
<div class="title">İstanbul</div>
<div class="text">
Istanbul, a fascinating city built on two Continents, divided by the Bosphorus Strait. This is one of the greatest cities in the world.
</div>
<button>Read More</button>
</div>
</div>
<div class="inner_part">
<label for="slideImg" class="img">
<img class="img_2" src="https://c4.wallpaperflare.com/wallpaper/649/96/56/ankara-cityscape-night-night-sky-wallpaper-preview.jpg">
</label>
<div class="content content_2">
<div class="title">Ankara</div>
<div class="text">
Ankara is Turkey's beating heart, second largest city, located in the Central Anatolia region and home to the Grand National Assembly of Turkey.
</div>
<button>Read More</button>
</div>
</div>
<div class="inner_part">
<label for="slideImg" class="img">
<img class="img_3" src="https://c4.wallpaperflare.com/wallpaper/620/34/558/turkey-izmir-mountains-wallpaper-preview.jpg">
</label>
<div class="content content_3">
<div class="title">İzmir</div>
<div class="text">Located on the shores of the Aegean Sea, west of the Anatolian Peninsula, İzmir is the third-largest city in Turkey.
</div>
<button>Read More</button>
</div>
</div>
</div>
css源码
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html, body {
height: 100vh;
font-family: 'Open Sans', sans-serif;
background-color: #666666;
}
body {
display: flex;
align-items: center;
justify-content: center;
}
.card {
position: absolute;
height: 350px;
width: 100%;
max-width: 850px;
margin: auto;
background-color: #ffffff;
border-radius: 25px;
box-shadow: 10px 0 50px rgba(0, 0, 0, 0.5);
}
.card .inner_part {
display: flex;
align-items: center;
justify-content: center;
padding: 0 0 0 30px;
height: 350px;
position: absolute;
}
#slideImg:checked ~ .inner_part {
padding: 0;
transition: .1s ease-in;
}
.inner_part .img {
height: 260px;
width: 260px;
overflow: hidden;
flex-shrink: 0;
border-radius: 20px;
box-shadow: 0 10px 50px rgba(0,0,0,0.2);
}
#slideImg:checked ~ .inner_part .img {
height: 350px;
width: 850px;
z-index: 99;
transition: .3s .2s ease-in;
}
.img img {
height: 100%;
width: 100%;
cursor: pointer;
opacity: 0;
transition: .6s;
}
#slide_1:checked ~ .inner_part .img_1,
#slide_2:checked ~ .inner_part .img_2,
#slide_3:checked ~ .inner_part .img_3 {
opacity: 1;
transition-delay: .2s;
}
.content {
padding: 0 20px 0 35px;
width: 530px;
margin-left: 50px;
opacity: 0;
transition: .6s;
}
#slideImg:checked ~ .inner_part .content {
display: none;
}
#slide_1:checked ~ .inner_part .content_1,
#slide_2:checked ~ .inner_part .content_2,
#slide_3:checked ~ .inner_part .content_3 {
opacity: 1;
margin-left: 0;
z-index: 100;
transition-delay: .3s;
}
.content .title {
font-size: 30px;
font-weight: 700;
color: #0d0925;
margin: 0 0 20px 0;
}
.content .text {
font-size: 19px;
color: #4e4a67;
margin: 0 auto 30px auto;
line-height: 1.5em;
text-align: justify;
}
.content button {
padding: 15px 20px;
border: none;
font-size: 16px;
color: #fff0e6;
font-weight: 600;
letter-spacing: 1px;
border-radius: 50px;
cursor: pointer;
outline: none;
background: #000000;
float: right;
}
.content button:hover {
background: #cecece;
color: #000000;
}
.slider {
position: absolute;
bottom: 25px;
left: 55%;
transform: translateX(-50%);
z-index: 1;
}
#slideImg:checked ~ .slider {
display: none;
}
.slider .slide {
position: relative;
height: 10px;
width: 50px;
background: #d9d9d9;
border-radius: 5px;
display: inline-flex;
margin: 0 3px;
cursor: pointer;
}
.slider .slide:before {
position: absolute;
content: '';
top: 0;
left: 0;
height: 100%;
width: -100%;
background: #000000;;
border-radius: 10px;
transform: scaleX(0);
transition: transform .6s;
transform-origin: left;
}
#slide_1:checked ~ .slider .slide_1:before,
#slide_2:checked ~ .slider .slide_2:before,
#slide_3:checked ~ .slider .slide_3:before {
transform: scaleX(1);
width: 100%;
}
input {
display: none;
}
在研究了一小会后,发现了里面的秘密。这是我尝试的简约版本
主要实现手段
1:使用单选框完成三张图片的切换(比较好理解)
2:使用position使三张卡片重叠
3:在未点击时,透明的为0;
4:点击后通过兄弟元素~使后面card内的卡片显示 (css中有注释)
5:后续可通过label实现其他动画
<style>
#card {
width: 800px;
height: 300px;
box-shadow: 2px 3px rgba(0, 0, 0, 0.137);
position: relative;
}
.inner_part {
width: 800px;
height: 300px;
position: absolute;//使用position使三张卡片重叠
}
#c1,
#c2,
#c3 {
opacity: 0;
width: 250px;
height: 250px;
margin: 25px;
border-radius: 10px;
overflow: hidden;
float: left;
}
img {
height: 100%;
width: 100%;
cursor: pointer;
}
.content {
width: 440px;
height: 250px;
opacity: 0;//在未点击时,透明的为0;
float: left;
margin: 25px;
}
#slider{
position: absolute;
z-index: 99;
right: 25px;
bottom: 25px;
}
#c1 {
background-color: rgb(0, 47, 167);
}
#c2 {
background-color: rgb(255, 0, 0);
}
#c3 {
background-color: rgb(179, 255, 0);
}
#test1:checked~#card .inner_part #c1,
#test2:checked~#card .inner_part #c2,
#test3:checked~#card .inner_part #c3 {//点击后通过兄弟元素~使后面card内的卡片显示
opacity: 1;
}
#test1:checked~#card .inner_part #content1,
#test2:checked~#card .inner_part #content2,
#test3:checked~#card .inner_part #content3 {
opacity: 1;
}
</style>
</head>
<body>
<input type="radio" value="t1" name="t1" id="test1" checked>
<input type="radio" value="t2" name="t1" id="test2">
<input type="radio" value="t3" name="t1" id="test3">
<div id="card">
<div class="inner_part">
<div id="c1">
<img src="https://c4.wallpaperflare.com/wallpaper/978/131/617/kiz-kulesi-turkey-istanbul-maiden-s-tower-wallpaper-preview.jpg"
alt="">
</div>
<div class="content " id="content1">
测试1
</div>
</div>
<div class="inner_part">
<div id="c2">
<img src="https://c4.wallpaperflare.com/wallpaper/649/96/56/ankara-cityscape-night-night-sky-wallpaper-preview.jpg"
alt="">
</div>
<div class="content " id="content2">
测试2
</div>
</div>
<div class="inner_part">
<div id="c3">
<img src="https://c4.wallpaperflare.com/wallpaper/620/34/558/turkey-izmir-mountains-wallpaper-preview.jpg"
alt="">
</div>
<div class="content " id="content3">
测试3
</div>
</div>
</div>
</body>
大三迷茫网工学生一枚,如果觉得该文章对您有用,不要忘记点赞哦~