詳解CSS3實(shí)現(xiàn)響應(yīng)式手風(fēng)琴效果
發(fā)布時間:2020-06-10 16:30:34 作者:NicholWang
我要評論
這篇文章主要介紹了詳解CSS3實(shí)現(xiàn)響應(yīng)式手風(fēng)琴效果,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
最近看了國外大佬用CSS3實(shí)現(xiàn)手風(fēng)琴效果的視頻,所以自己學(xué)習(xí)后寫了一下,以博客的形式記錄下來,方便自己日后復(fù)習(xí),代碼結(jié)構(gòu)如下(字體用的是Genericons ):

最終效果如下:
全屏?xí)r:

屏幕寬度小于960px時:

下面來看一下頁面的基本結(jié)構(gòu)(index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<!--標(biāo)題-->
<header>
<h1>Follow me on social media</h1>
</header>
<!--手風(fēng)琴部分-->
<ul class="accordion">
<li class="tab">
<div class="social youtube">
<a href="#">YouTube</a>
</div>
<div class="content">
<h1>YouTube</h1>
<p>Lorem ipsum dolor sit amet consectetur
adipisicing elit.Culpa, consectetur.</p>
</div>
</li>
<li class="tab">
<div class="social facebook">
<a href="#">Facebook</a>
</div>
<div class="content">
<h1>Facebook</h1>
<p>Lorem ipsum dolor sit amet consectetur
adipisicing elit.Culpa, consectetur.</p>
</div>
</li>
<li class="tab">
<div class="social twitter">
<a href="#">Twitter</a>
</div>
<div class="content">
<h1>Twitter</h1>
<p>Lorem ipsum dolor sit amet consectetur
adipisicing elit.Culpa, consectetur.</p>
</div>
</li>
<li class="tab">
<div class="social instagram">
<a href="#">Instagram</a>
</div>
<div class="content">
<h1>Instagram</h1>
<p>Lorem ipsum dolor sit amet consectetur
adipisicing elit.Culpa, consectetur.</p>
</div>
</li>
<li class="tab">
<div class="social linkedin">
<a href="#">Linkedin</a>
</div>
<div class="content">
<h1>Linkedin</h1>
<p>Lorem ipsum dolor sit amet consectetur
adipisicing elit.Culpa, consectetur.</p>
</div>
</li>
<li class="tab">
<div class="social github">
<a href="#">Github</a>
</div>
<div class="content">
<h1>Github</h1>
<p>Lorem ipsum dolor sit amet consectetur
adipisicing elit.Culpa, consectetur.</p>
</div>
</li>
</ul>
</div>
</body>
</html>
樣式(style.css):
*{
margin: 0;
padding: 0;
border: none;
}
body{
font-family: Arial, Helvetica, sans-serif;
background-color: #222;
color: #fff;
}
/*設(shè)置字體,因?yàn)楹竺娴膱D標(biāo)需要用到*/
@font-face {
font-family: 'Genericons';
src: url('font/genericons-regular-webfont.woff') format('woff'),
url('font/genericons-regular-webfont.eot') format('truetype');
}
/*設(shè)置外面容器的寬度*/
.container{
width: 80%;
margin: 20px auto;
}
header h1{
font-size: 2rem;
padding: 1rem;
text-align: center;
}
/*注意這里font-size設(shè)置為0,不然會出現(xiàn)非常糟糕的畫面,我們后面再去單獨(dú)對需要現(xiàn)實(shí)的文本設(shè)置字體大小
,因?yàn)閍鏈接不想讓它顯示內(nèi)容*/
.accordion{
width: 100%;
min-width: 800px;
height: 200px;
background-color: #333;
list-style: none;
display: block;
overflow: hidden;
font-size: 0;
}
/*對每一個li設(shè)置為inline-block,讓其排列在一行,溢出隱藏,因?yàn)?tab下面的.content寬度為360,而且.tab只有在hover的時候?qū)挾炔艜兂?50px,那時候.content剛好顯示.另外設(shè)置過渡,使其寬度增長的過程平緩*/
.tab{
width: 80px;
height: 100%;
display: inline-block;
position: relative;
margin: 0;
background-color: #444;
border: 1px solid #333;
overflow: hidden;
transition: all .5s ease .1s;
}
.tab:hover{
width: 450px;
}
.tab:hover .social a:after{
transform: translateX(-80px);
}
.tab:hover .social a:before{
transform: translateX(-100px);
}
/*設(shè)置定位為相對定位,不然.content會有部分內(nèi)容被遮住*/
.tab .content{
position: relative;
width: 360px;
height: 100%;
background-color: #fff;
color: #333;
margin-left: 80px;
padding: 50px 0 0 15px;
}
.tab .content h1{
font-size: 2.5rem;
margin-top: 20px;
}
.tab .content p{
font-size: .85rem;
line-height: 1.6;
}
/設(shè)置為元素的寬高及字體為Genericons,不然圖標(biāo)無法顯現(xiàn),只會顯示白色的空框框/
.social a:before,
.social a:after{
position: absolute;
width: 80px;
height: 200px;
display: block;
text-indent: 0;
padding-top: 90px;
padding-left: 25px;
font:normal 30px Genericons;
color: #fff;
transition: all .5s ease;
}
/*因?yàn)楫?dāng)我們hover上去的時候圖標(biāo)會更大,所以after偽類的字體及padding要重新設(shè)置,同時
要將margin-left設(shè)置為80px,這要默認(rèn)情況下顯示的就是before偽類的小圖標(biāo)*/
.social a:after{
font-size: 48px;
padding-top: 80px;
padding-left: 20px;
margin-left: 80px;
}
/*Add icons*/
.youtube a:before,
.youtube a:after{
content: '\f213';
}
.youtube a:after{
background-color: #fc0000;
}
.twitter a:before,
.twitter a:after{
content: '\f202';
}
.twitter a:after{
background-color: #6dc5dd;
}
.facebook a:before,
.facebook a:after{
content: '\f204';
}
.facebook a:after{
background-color: #3b5998;
}
.linkedin a:before,
.linkedin a:after{
content: '\f208';
}
.linkedin a:after{
background-color: #00a9cd;
}
.instagram a:before,
.instagram a:after{
content: '\f215';
}
.instagram a:after{
background-color: #6dc993;
}
.github a:before,
.github a:after{
content: '\f200';
}
.github a:after{
background-color: #6e5494;
}
/*當(dāng)屏幕最大寬度為960px時*/
@media(max-width:960px){
.container{
width: 70%;
}
/*讓高度為auto*/
.accordion{
min-width: 450px;
height: auto;
}
/*讓li顯示為block,這樣就會依次往下排*/
.tab{
width: 100%;
display: block;
border-bottom: 1px solid #333;
}
/*這個一定要設(shè)置,因?yàn)樵镜?tab:hover時寬度為450px,假如.tab的寬度有600px,在hover時就回剩余150px的空白,不是我們想要的效果*/
.tab:hover{
width: 100%;
}
.tab .content{
width: 85%;
}
/*設(shè)置對應(yīng)偽類的padding值,使其大概顯示在中間*/
.social a:before{
padding-top: 60px;
padding-left: 25px;
}
.social a:after{
padding-top: 50px;
padding-left: 20px;
}
}
到此這篇關(guān)于詳解CSS3實(shí)現(xiàn)響應(yīng)式手風(fēng)琴效果的文章就介紹到這了,更多相關(guān)CSS3響應(yīng)式手風(fēng)琴內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
CSS3防疫知識圖文響應(yīng)式布局代碼是一款運(yùn)用css3寫響應(yīng)式的頁面布局,可根據(jù)瀏覽器的窗口大小來自適應(yīng)頁面布局。2020-12-22
CSS3響應(yīng)式個人名片圖文布局代碼是一款個人名片,可根據(jù)瀏覽器的窗口大小來自動改變頁面布局。2020-12-21- 這篇文章主要介紹了css3 響應(yīng)式媒體查詢的示例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-25

HTML5 圖片懸停放大的實(shí)現(xiàn)代碼示例
這篇文章主要介紹了HTML5 圖片懸停放大的實(shí)現(xiàn)代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)2019-12-04html用title屬性實(shí)現(xiàn)鼠標(biāo)懸停顯示文字
實(shí)現(xiàn)鼠標(biāo)懸停顯示文字,html中使用title屬性就可實(shí)現(xiàn)顯示文字的效果,這個屬性還是比較實(shí)用的,需要的朋友可以參考下2014-09-06
html+css實(shí)現(xiàn)響應(yīng)式卡片懸停效果
這篇文章主要介紹了 html+css實(shí)現(xiàn)響應(yīng)式卡片懸停效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-28





