線上體驗:https://hovertree.com/texiao/css3/39/
這是一個完全以 CSS3 實作的圖文切換效果,全程沒有寫任何一行 JavaScript。點擊左右箭頭或下方的圓點導航鈕,即可切換內容。
本特效大量使用了 CSS3 新增的 :nth-of-type(n) 選擇器,它會匹配屬於父元素中特定類型的第 n 個子元素,其中 n 可以是數字、關鍵字或公式。程式碼中也出現了子元素選擇器(大於號),兩者搭配就能精準控制每一張投影片的樣式與狀態。
整個效果的關鍵在於「用 radio 按鈕記錄狀態」:把幾個 <input type="radio"> 當成狀態開關,再用 :checked 搭配 ~ 泛義兄弟選擇器去控制後方的 <ul> 內容位移,如此一來,切換的狀態完全由 CSS 承接,不需要 JS 介入。箭頭與圓點則是一堆 <label for="...">,點擊 label 等同於點擊對應的 radio。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>純 CSS3 實現圖文切換特效</title>
<link rel="stylesheet" href="https://hovertree.com/texiao/css3/39/themes/csslider.default.css" />
<style>
* { margin: 0; padding: 0; }
::-webkit-scrollbar { width: 2px; background: rgba(255,255,255,0.1); }
::-webkit-scrollbar-track { background: none; }
::-webkit-scrollbar-thumb { background: rgba(74,168,0,0.6); }
ul, ol { padding-left: 40px; }
html, body {
height: 100%;
text-align: center;
font: 400 100% 'Raleway', 'Lato';
background-color: #282828;
color: #CCC;
}
h1 { font-weight: 700; font-size: 310%; }
h2 { font-weight: 400; font-size: 120%; color: #71AD37; }
#hewenqislider { margin: 20px; font-family: 'Lato'; }
#hewenqislider > ul > li:nth-of-type(3) {
background: url(https://hovertree.com/texiao/css3/39/themes/fruit.jpg);
}
/* 第三張投影片被選中時,展開文字背景區塊 */
#hewenqislider > input:nth-of-type(3):checked ~ ul #hovertreebg {
width: 80%;
padding: 22px;
transition: .5s .5s;
}
#hewenqislider > input:nth-of-type(3):checked ~ ul #hovertreebg div {
transform: translate(0);
transition: .5s .9s;
}
#hovertreebg {
color: #000;
padding: 22px 0;
position: absolute;
left: 0;
top: 16%;
height: 20%;
width: 0;
z-index: 10;
overflow: hidden;
}
#hovertreebg:before {
content: '';
position: absolute;
left: -1px; top: 1px;
height: 100%; width: 100%;
z-index: -1;
background: url(https://hovertree.com/texiao/css3/39/themes/fruit.jpg) 1px 23%;
-webkit-filter: blur(7px);
}
#hovertreebg:after {
content: '';
position: absolute;
left: 0; top: 0;
height: 100%; width: 100%;
z-index: 20;
background: rgba(0,0,0,0.35);
pointer-events: none;
}
#hovertreebg div { transform: translate(120%); }
.scrollable p {
padding: 30px;
text-align: justify;
line-height: 140%;
font-size: 120%;
}
#hewenqislider a { color: greenyellow; }
.csslider > ul { width: 500px; } /* 在此調整寬度 */
</style>
</head>
<body>
<div style="padding: 1em 0;">
<div id="hewenqislider" class="csslider">
<input type="radio" name="slides" id="slides_1" />
<input type="radio" name="slides" id="slides_2" />
<input type="radio" name="slides" id="slides_3" checked />
<input type="radio" name="slides" id="slides_4" />
<ul>
<li>
<h1>歡迎光臨</h1>
<p>CSSlider is lightweight & easy to use slider. No JS - pure CSS.<br />
這是一個使用純 CSS3 實現的圖文切換效果,沒有使用任何 JS 腳本。</p>
</li>
<li>
<a href="https://hovertree.com/texiao/"><img src="https://hovertree.com/texiao/css3/39/themes/stones.jpg" /></a>
</li>
<li>
<div id="hovertreebg">
<div>
<h1>CSS Events</h1>
<p>When slide 3 is reached - play CSS animation!</p>
</div>
</div>
</li>
<li class="scrollable">
<h1>Lots of text</h1>
<h2>Scrollable one</h2>
<p>這裡可以放很長的文字,超出部分會自動出現滾動條。</p>
</li>
</ul>
<div class="arrows">
<label for="slides_1"></label>
<label for="slides_2"></label>
<label for="slides_3"></label>
<label for="slides_4"></label>
</div>
<div class="navigation">
<div>
<label for="slides_1"></label>
<label for="slides_2"></label>
<label for="slides_3"></label>
<label for="slides_4"></label>
</div>
</div>
</div>
</div>
<div style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';">
<p>適用瀏覽器:360、FireFox、Chrome、Safari、Opera、傲游、搜狗、世界之窗。不支援 IE8 及以下瀏覽器。</p>
<p>來源:<a href="https://hovertree.com/" target="_blank">何問起</a></p>
</div>
</body>
</html>
相關範例:
原始碼下載:https://hovertree.com/h/bjaf/er3dmrth.htm
這種寫法的優點是效能極佳、沒有 JS 依賴,很適合放在靜態頁面;缺點是無法做到自動播放,若需要自動輪播就得回到 JavaScript 或改用 CSS animation 搭配延时控制。