DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
CSS3 動手做:打造不用 JavaScript 的照片長廊
編輯:CSS詳解     

CSS3 動畫在當時雖然只有少數 WebKit 內核的瀏覽器支援,但身為前端開發者,提前熟悉這些新特性絕對是明智之舉。本文就用純 CSS3 實作一個會自動捲動的照片長廊,並示範滑鼠移入放大、搭配音樂與模擬音頻均衡器的效果。

一、鋪一層漸變背景

先讓 body 由上而下漸變,從淺灰過渡到深灰:

background: -webkit-gradient(linear, 0 0, 0 100%, from(#F9F9F9), to(#5F5F5F));

這是 WebKit 內核的寫法,讓背景顏色從 #F9F9F9 漸變到 #5F5F5F。由於本例只針對 WebKit,暫不考慮其他內核的相容性。

二、建立放置照片的容器並加上動畫

.player {
  width: 1500px;
  position: absolute;
  left: 0px;
  top: 0px;
  -webkit-animation-name: goleft;        /* 關鍵幀名稱 */
  -webkit-animation-duration: 15s;       /* 播放時間 */
  -webkit-animation-iteration-count: infinite; /* 循環次數 */
  -webkit-animation-direction: alternate;      /* 動畫方向 */
}

單張圖片寬 300px,一共 5 張,所以容器寬度設為 1500px,並讓它脫離文檔流浮起來。最關鍵的是那幾行動畫設定,每一行的用途都寫在註解裡,想深入了解可查閱 W3C 草案。

三、定義關鍵幀

動畫要有什麼樣的效果,都在這裡決定。以 goleft 為例,就是描述每一步移動的距離:

@-webkit-keyframes goleft {
  from { left: 0;    -webkit-animation-timing-function: ease-in-out; }
  25%  { left: -300px;  -webkit-animation-timing-function: ease-in-in; }
  50%  { left: -600px;  -webkit-animation-timing-function: ease-in-out; }
  75%  { left: -900px;  -webkit-animation-timing-function: ease-in-in; }
  to   { left: -1200px; }
}

加點花樣:滑鼠移入放大

還不夠炫?讓滑鼠移到圖片上時該張圖放大:

.player a:hover img {
  -webkit-animation-name: zoomIn;
  -webkit-animation-duration: 5s;
  -webkit-animation-iteration-count: infinite;
  -webkit-animation-direction: alternate;
}

這裡又定義了一個 zoomIn 關鍵幀,用 CSS 的 transform 放大屬性,逐步設定放大尺寸,讓圖片平穩放大:

@-webkit-keyframes zoomIn {
  from { -webkit-transform: scale(1); }
  25%  { -webkit-transform: scale(1.2); }
  50%  { -webkit-transform: scale(1.4); }
  to   { -webkit-transform: scale(1.6); }
}

用 HTML5 加入音樂播放器

光看圖不夠,再來點音樂。用 HTML5 的 audio 標籤就能輕鬆搞定:

<div id="mbox"><audio src="https://nio.name/content/mp3/09.mp3" autoplay="false" controls="true"></audio></div>

最後加一個假的音頻均衡器

先寫幾個空標籤:

<div id="mBar">
  <span></span> <span></span> <span></span> <span></span> <span></span>
</div>

再用 CSS3 選擇器為每根長條設定不同的動畫時長,讓它們伸縮錯落,模擬音頻跳動的波形:

#mBar span:nth-child(1){ -webkit-animation-name: bar; -webkit-animation-duration: 3s; -webkit-animation-iteration-count:infinite; -webkit-animation-direction:alternate; background: -webkit-gradient(linear, 0 0, 0 100%, from(#FF00CC), to(#E8E8E8)); }
#mBar span:nth-child(2){ -webkit-animation-name: bar; -webkit-animation-duration: 2s; -webkit-animation-iteration-count:infinite; -webkit-animation-direction:alternate; background: -webkit-gradient(linear, 0 0, 0 100%, from(#66FF00), to(#E8E8E8)); }
#mBar span:nth-child(3){ -webkit-animation-name: bar; -webkit-animation-duration: 1s; -webkit-animation-iteration-count:infinite; -webkit-animation-direction:alternate; background: -webkit-gradient(linear, 0 0, 0 100%, from(#FF3300), to(#E8E8E8)); }
#mBar span:nth-child(4){ -webkit-animation-name: bar; -webkit-animation-duration: 2s; -webkit-animation-iteration-count:infinite; -webkit-animation-direction:alternate; }
#mBar span:nth-child(5){ -webkit-animation-name: bar; -webkit-animation-duration: 3s; -webkit-animation-iteration-count:infinite; -webkit-animation-direction:alternate; }

再定義 bar 關鍵幀,控制長條的高度變化:

@-webkit-keyframes bar {
  from { height: 10px; }
  to   { height: 80px; }
}

這樣一個完全不需要 JavaScript 的照片長廊就完成了。發揮你的想像力,也用 CSS3 動手做看看吧!

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved