從零開始學 Web 之 CSS3(六)動畫animation,Web字體

你們好,這裏是「 從零開始學 Web 系列教程 」,並在下列地址同步更新......javascript

  • github:https://github.com/Daotin/Web
  • 微信公衆號:Web前端之巔
  • 博客園:http://www.cnblogs.com/lvonve/
  • CSDN:https://blog.csdn.net/lvonve/

在這裏我會從 Web 前端零基礎開始,一步步學習 Web 相關的知識點,期間也會分享一些好玩的項目。如今就讓咱們一塊兒進入 Web 前端學習的冒險之旅吧!css

1、動畫

一、建立動畫

好的前端工程師,會更注重用戶的體驗和交互。那麼動畫就是將咱們的靜態頁面,變成具備靈動性,爲咱們的界面添加個性的一種方式。html

一個動畫至少須要兩個屬性:前端

animation-name :動畫的名字(建立動畫時起的名字,以下爲 moveTest)java

animation-duration :動畫的耗時git

animation-name: moveTest;
animation-duration: 2s;

如需在 CSS3 中建立動畫,須要學習 @keyframes 規則。@keyframes 規則用於建立動畫。在 @keyframes 中規定某項 CSS 樣式,就能建立由當前樣式逐漸改成新樣式的動畫效果。github

使用 @keyframes關鍵字來建立動畫。web

@keyframes moveTest {
  /*百分比是指整個動畫耗時的百分比*/
  0% {
    transform: translate(0px, 0px);
  }
  50% {
    transform: translateY(200px);
  }
  100% {
    transform: translate(200px,200px);
  }
}

其中,百分比是指整個動畫耗時的百分比。chrome

示例:瀏覽器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            background-color: blue;

            animation-name: moveTest;
            animation-duration: 2s;
        }

        @keyframes moveTest {
            0% {
                transform: translate(0px, 0px);
            }
            50% {
                transform: translateY(200px);
            }
            100% {
                transform: translate(200px,200px);
            }
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

0%:動畫起始位置,也能夠寫成 from

100%:動畫終點位置,也能夠寫成 to。

二、動畫的其餘屬性

animation-iteration-count:設置動畫的播放次數,默認爲1次

animation-direction:設置交替動畫

animation-delay:設置動畫的延遲

animation-fill-mode:設置動畫結束時的狀態:默認狀況下,動畫執行完畢以後,會回到原始狀態

animation-timing-function:動畫的時間函數(動畫的效果,平滑?先快後慢等)

animation-play-state:設置動畫的播放狀態 paused:暫停 running:播放

/*3.設置動畫的播放次數,默認爲1次  能夠指定具體的數值,也能夠指定infinite(無限次)*/
animation-iteration-count: 1;
/*4.設置交替動畫  alternate:來回交替*/
animation-direction: alternate;
/*5.設置動畫的延遲*/
animation-delay: 2s;
/*5.設置動畫結束時的狀態:默認狀況下,動畫執行完畢以後,會回到原始狀態
forwards:會保留動畫結束時的狀態,在有延遲的狀況下,並不會馬上進行到動畫的初始狀態
backwards:不會保留動畫結束時的狀態,在添加了動畫延遲的前提下,若是動畫有初始狀態,那麼會馬上進行到初始狀態
both:會保留動畫的結束時狀態,在有延遲的狀況下也會馬上進入到動畫的初始狀態*/
animation-fill-mode: both;
/*6.動畫的時間函數:linear,ease...*/
animation-timing-function: linear;
/*設置動畫的播放狀態  paused:暫停   running:播放*/
animation-play-state: running;

三、案例:無縫滾動

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        div {
            width: 600px;
            height: 100px;
            margin: 100px auto;
            background-color: #ccc;
            overflow: hidden;
        }
        ul {
            width: 200%;
            animation: moveLeft 6s linear 0s infinite;
        }
        ul > li {
            float: left;
            list-style: none;
        }
        li > img {
            width: 200px;
            height: 100px;
        }
        div:hover > ul {
            cursor: pointer;
            animation-play-state: paused;
        }
        @keyframes moveLeft {
            from {
                transform: translateX(0);
            }
            to {
                transform: translateX(-600px);
            }
        }
    </style>
</head>
<body>
    <div>
        <ul>
            <li><img src="images/img1.jpg" alt=""></li>
            <li><img src="images/img2.jpg" alt=""></li>
            <li><img src="images/img3.jpg" alt=""></li>
            <!-- 複製的一份圖片 -->
            <li><img src="images/img1.jpg" alt=""></li>
            <li><img src="images/img2.jpg" alt=""></li>
            <li><img src="images/img3.jpg" alt=""></li>
        </ul>
    </div>
</body>
</html>

一、將要顯示的圖片複製一份,以完成無縫滾動的需求。

二、而後讓 ul 移動整個ul的寬度便可,而且無限循環,就實現無線輪播的效果。

三、而後在鼠標放上去的時候,使得動畫暫停。

四、案例:時鐘

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        .clock {
            width: 300px;
            height: 300px;
            margin: 100px auto;
            border: 10px solid #ccc;
            border-radius: 50%;
            position: relative;
        }
        .line {
            width: 8px;
            height: 300px;
            background-color: #ccc;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }
        .line2 {
            transform: translate(-50%, -50%) rotate(30deg);
        }
        .line3 {
            transform: translate(-50%, -50%) rotate(60deg);
        }
        .line4 {
            width: 10px;
            transform: translate(-50%, -50%) rotate(90deg);
        }
        .line5 {
            transform: translate(-50%, -50%) rotate(120deg);
        }
        .line6 {
            transform: translate(-50%, -50%) rotate(150deg);
        }
        .cover {
            width: 250px;
            height: 250px;
            background-color: #fff;
            border-radius: 50%;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }
        .center {
            width: 20px;
            height: 20px;
            background-color: #ccc;
            border-radius: 50%;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }
        .hour {
            width: 12px;
            height: 80px;
            background-color: red;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -100%);
            transform-origin: center bottom;
            animation: clockMove 43200s linear infinite;
        }
        .minute {
            width: 8px;
            height: 100px;
            background-color: blue;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -100%);
            transform-origin: center bottom;
            animation: clockMove 3600s linear infinite;
        }
        .second {
            width: 4px;
            height: 120px;
            background-color: green;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -100%);
            transform-origin: center bottom;
            animation: clockMove 60s infinite steps(60);
        }
        
        @keyframes clockMove {
            from {
                transform: translate(-50%, -100%) rotate(0deg);
            }
            to {
                transform: translate(-50%, -100%) rotate(360deg);
            }
        }
    </style>
</head>
<body>
    <div class="clock">
        <div class="line line1"></div>
        <div class="line line2"></div>
        <div class="line line3"></div>
        <div class="line line4"></div>
        <div class="line line5"></div>
        <div class="line line6"></div>    
        <div class="cover"></div>
        <div class="hour"></div>
        <div class="minute"></div>
        <div class="second"></div>
        <div class="center"></div>
    </div>
</body>
</html>

咱們讓秒針step(60)一步一步走,效果更好。

2、Web字體與圖標

一、web字體

咱們有些時候須要在網頁上顯示一些特殊的字體,若是這些特殊的字體在電腦上沒有安裝的話,就會顯示系統默認的字體,而不是這些特殊的字體。

這時就有了 Web 字體。開發人員能夠爲自已的網頁指定特殊的字體,無需考慮用戶電腦上是否安裝了此特殊字體,今後把特殊字體處理成圖片的時代便成爲了過去。它的支持程度比較好,甚至 IE 低版本瀏覽器也能支持。

二、字體格式

不一樣瀏覽器所支持的字體格式是不同的,咱們有必要了解一下有關字體格式的知識。

  • TureTpe(.ttf)格式

.ttf字體是Windows和Mac的最多見的字體,是一種RAW格式,支持這種字體的瀏覽器有IE9+、Firefox3.5+、Chrome4+、Safari3+、Opera10+、iOS Mobile、Safari4.2+;

  • OpenType(.otf)格式

.otf字體被認爲是一種原始的字體格式,其內置在TureType的基礎上,支持這種字體的瀏覽器有Firefox3.5+、Chrome4.0+、Safari3.1+、Opera10.0+、iOS Mobile、Safari4.2+;

  • Web Open Font Format(.woff)格式

woff字體是Web字體中最佳格式,他是一個開放的TrueType/OpenType的壓縮版本,同時也支持元數據包的分離,支持這種字體的瀏覽器有IE9+、Firefox3.5+、Chrome6+、Safari3.6+、Opera11.1+;

  • Embedded Open Type(.eot)格式

.eot字體是IE專用字體,能夠從TrueType建立此格式字體,支持這種字體的瀏覽器有IE4+;

  • SVG(.svg)格式

.svg字體是基於SVG字體渲染的一種格式,支持這種字體的瀏覽器有Chrome4+、Safari3.1+、Opera10.0+、iOS Mobile Safari3.2+

三、使用步驟

須要注意的是,咱們在使用 Web 字體的時候,應該首先把須要用到特殊字體的這些字寫好,而後在網絡上生成這些字體對應的 Web 字體庫,並將其下載下來。下圖爲一個網站生成和下載web字體的網站,點擊當即使用就能夠了:

下載下來以後,把下在下來的全部文件導入本身的項目,注意路徑的匹配問題。

以後在咱們css樣式裏面使用@font-face關鍵字來自定義 Web 字體。

@font-face {
  font-family: 'shuangyuan';
  src: url('../fonts/webfont.eot'); /* IE9*/
  src: url('../fonts/webfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
    url('../fonts/webfont.woff') format('woff'), /* chrome、firefox */
    url('../fonts/webfont.ttf') format('truetype'), /* chrome、firefox、opera、Safari, Android, iOS 4.2+*/
    url('../fonts/webfont.svg#webfont') format('svg'); /* iOS 4.1- */
}

最後在使用的時候:font-family: "shuangyuan"; 就可使用 shuangyuan 這種字體了。

四、字體圖標

字體圖標就是咱們常見的字體,不過這個字體的表現形式爲一個圖標。這樣咱們就可使用這些特殊的字體來代替精靈圖了。

常見的是把網頁經常使用的一些小的圖標,藉助工具幫咱們生成一個字體包,而後就能夠像使用文字同樣使用圖標了。

優勢:

  • 將全部圖標打包成字體庫,減小請求;
  • 具備矢量性,可保證清晰度;
  • 使用靈活,便於維護

4.一、方法一

使用方法和Web字體同樣。也是先下載須要的圖標字體庫文件,而後使用關鍵字 @font-face 生成本身的web圖標字體。

示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        @font-face {
            font-family: 'iconfont';
            src: url('../fonts/iconfont.eot'); /* IE9*/
            src: url('../fonts/iconfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
            url('../fonts/iconfont.woff') format('woff'), /* chrome、firefox */
            url('../fonts/iconfont.ttf') format('truetype'), /* chrome、firefox、opera、Safari, Android, iOS 4.2+*/
            url('../fonts/iconfont.svg#iconfont') format('svg'); /* iOS 4.1- */
        }
        .myFont{
            font-family: iconfont;
        }
        /*笑臉*/
        .smile::before{
            content: "\e641";
            color: red;
            font-size: 50px;
        }
        /*輸出*/
        .output::before{
            content: "\e640";
            color: blue;
            font-size: 50px;
        }
    </style>
</head>
<body>
<!--使用字體圖標的時候,得本身指定你想使用的圖片-->
<span class="myFont smile"></span>
<span class="myFont output"></span>
<span class="myFont">&#xe642;</span>
</body>
</html>

4.二、方法二

直接在線調用網上web圖標 css庫

<link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.css">

使用的時候:和方法一同樣,直接使用類屬性 class='fa fa-play 的方式,fa-play是一個播放的圖標,不一樣的圖標的名字含義不一樣,只須要到 font-awesome 官網(http://www.fontawesome.com.cn/)找到對應的圖標的名稱便可。

示例:

<a href="javascript:void(0);" class="fa fa-play"></a>  <!--播放圖標-->
<a href="javascript:void(0);" class="fa fa-arrows-alt"></a>   <!--全屏圖標-->

注意:class 樣式的 第一個 fa 是必寫的,表示的是用的 font-awesome 的字體圖標。

相關文章
相關標籤/搜索