【前端知識體系】CSS特效實現之Transition和Transform對比

来源:https://www.cnblogs.com/fecommunity/archive/2019/11/18/11874695.html
-Advertisement-
Play Games

本文介紹了CSS繪製三角形、CSS高級動畫等知識,並對Transition和Transform的使用進行了對比。 ...


CSS效果

1.使用div繪製圖形(三角形)?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .triangle-top {
            width: 0;
            height: 0;
            border-width: 0 40px 40px;
            border-style: solid;
            border-color: transparent transparent red;
        }

        .triangle-bottom {
            width: 0;
            height: 0;
            border-style: solid;
            border-width: 40px 40px 0 40px;
            border-color: blueviolet transparent transparent transparent;
        }

        .triangle-left {
            width: 0;
            height: 0;
            border-style: solid;
            border-width: 40px 0 40px 40px;
            border-color: transparent transparent transparent #89ff56;
        }

        .triangle-right {
            width: 0;
            height: 0;
            border-style: solid;
            border-width: 40px 40px 40px 0;
            border-color: transparent yellowgreen transparent;
        }


        /*缺口的三角形*/
        .box{
            position: absolute;
            left: 0;
            top: 0;
            width: 0;
            height:0;
            border: 6px solid transparent;
        }
        .b1{
            /*底層的盒子三角形相當於全部是黑色的*/
            border-bottom-color:#000 ;
        }
        .b2{
            /*上層的為白色覆蓋*/
            border-bottom-color:#fff ;
        }
    </style>
</head>
<body>

<div class="triangle-top"></div>
<div class="triangle-bottom"></div>
<div class="triangle-left"></div>
<div class="triangle-right"></div>

<div class="box b1"></div>
<div class="box b2"></div>
</body>
</html>

2.如何產生一個不占空間的邊框?( box-sizing屬性)?

[!NOTE]
知識點:IE和標準盒子模型的異同點
標準盒子模型

  • 標準盒子模型:元素的width或height=content的width或height;

IE盒子模型

  • IE盒子模型:元素的width或height=content的width或height+padding2+border2;
    /*IE盒子模型:width_sum = margin + width*/
    .border-box-use {
        box-sizing : border-box;
        /*通過IE的盒子模型設置出來之後,這個盒子內容區域的寬度實際只有180px
            這裡指定的width 實際上是包含了border的寬度的
        */
        width: 200px;
        height: 200px;

        border: 10px solid #89ff56;
    }

    /*標準盒子模型:width_sum = margin + width + padding + width*/
    .border-content-box-use {
        /*設置為標準的盒子模型,預設值*/
        box-sizing: content-box;

        width: 200px;
        height: 200px;

        padding: 1px;
        border: 10px solid #ff255f;
    }

    .parent-box-inhrit {
        /*設置當前的盒子模型是從父級盒子中繼承,這裡相當於是繼承了IE的盒子模型*/
        box-sizing: inherit;
        width: 50px;
        height: 50px;
        background-color: #48adff;

        border: 1px solid #000;

        /*對一個元素自身設置padding,相當於是把盒子撐大了*/
        padding: 5px;
    }

2.1 使用box-shadow實現

/*box-shadow 製作邊框*/
.box-shadow-border {
    width: 200px;
    height: 200px;
    /*設置外陰影:x y 模糊區域 擴展區域*/
    box-shadow: 0 0 0 10px red , 0 0 0 10px blue;
}

2.2 使用outline實現

在元素邊框邊緣的外圍繪製一條包圍元素的線,包括outline-color、outline-style、outline-width三個子屬性的設置,可預設,無固定順序。輪廓不占據頁面空間,也不一定是矩形。即不會增加額外的width或者height。

.borner-no-space {
    width: 200px;
    height: 200px;
    outline: 10px solid red;
}

3.如何實現IOS圖標的圓角?

/*clip-path的使用
            1. 對容器進行裁剪
            2. 常見集合圖形
            3. 自定義路徑
*/
.container-clippath {
    width: 400px;
    height: 300px;
    border: 1px solid #000;
    background-image: url("bg.jpg");
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center center;

    /*開始進行區域裁剪*/
    /*clip-path: circle(50px at 100px 100px);*/
    /*clip-path: inset(100px 50px);*/
    clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
    /*同時,也是支持svg矢量圖的裁剪*/
}

.container-clippath:hover {
    clip-path: circle(80px at 100px 100px);
}

5.說下背景圖的居中顯示/重覆/改變大小?

background-position: 背景圖片相對容器原點的起始位置
background-repeat
background-size : cover/contain(設置大小)

background-position

/* 一個值: 這個值指定圖片的寬度,那麼第二個值為auto */
background-size: auto
background-size: 50%
background-size: 3em
background-size: 12px
 
/* 兩個值: 第一個值指定圖片的寬度,第二個值指定圖片的高度 */
background-size: 50% auto
background-size: 3em 25%
background-size: auto 6px
background-size: auto auto
/*多重背景,請用逗號隔開,在CSS語法中凡語法後跟*或者#,都是可以無限重覆的,但是必須用逗號隔開。 */
background-size: auto, auto     /* 不要跟background-size: auto auto混淆了 */
background-size: 50%, 25%, 25%
background-size: 6px, auto, contain
 
background-size: inherit

6.如何平移/放大一個元素?如何實現0.5px的邊框?

[!NOTE]
知識點:transform的靈活使用

<style>
.custom-border{
    width:200px;
    margin:10px auto;
    height:100px;
    border:1px solid #333;
    background-color:#eee;
    padding:10px;
}
.scale-border{
    margin:10px auto;
    height:100px;
    position:relative;
    padding:10px;
    width: 200px;
}
.border{
    -webkit-transform:scale(0.5);
    transform:scale(0.5);
    position:absolute;
    border:1px solid #333;
    top:-50%;
    right:-50%;
    bottom:-50%;
    left:-50%;
    background-color:#eee;
}
.content{
    position:relative;
    z-index:2;
}
</style>

<body>
    <div class="custom-border border-color">邊框寬度1px</div>
    <div class="scale-border">
        <div class="content">邊框寬度0.5px</div>
        <div class="border border-color"></div>
    </div>
</body>
</html>

7.如何實現3D效果(旋轉的硬幣)?

/* 1. 設置一個透視變換,相機距離圖像的距離 */
/* perspective : 500px */
/* 2. 設置視覺查看的樣式 */
/* transform-style : perspective-3d */
/* 3. 變換圖像 */
/* transform : translate rotate */


/*旋轉的硬幣效果*/
.money {
    width: 100px;
    height: 100px;
    border-radius: 50px;
    background-color: #48adff;
    border: 2px solid #000;

    /*開啟3D效果*/

    perspective: 500px;
    transform-style: preserve-3d;
    /*transform : rotateY(180deg);*/
    animation : rotate 2s linear infinite;
}

@keyframes rotate {
    from {
        transform : rotateY(0deg);
    }
    to {
        transform : rotateY(360deg);
    }
}

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Django csrf CSRF 全稱(Cross Site Request Forgery)跨站請求偽造。也被稱為One Click Attack和Session Riding,通常縮寫為CSRF或XSRF。你可以這樣理解:攻擊者(黑客,釣魚網站)盜用了你的身份,以你的 名義發送惡意請求,這些請求 ...
  • mongodb4.2版本的安裝與配置(windows下),史上最全最基礎最詳細+圖解 ...
  • Apache Flink是什麼? ​ 在當代數據量激增的時代,各種業務場景都有大量的業務數據產生,對於這些不斷產生的數據應該如何進行有效的處理,成為當下大多數公司所面臨的問題。隨著雅虎對hadoop的開源,越來越多的大數據處理技術開始涌入人們的視線,例如目前比較流行的大數據處理引擎Apache Sp ...
  • alert日誌報錯: 2019-11-18T07:15:12.704938+08:00Errors in file /u01/app/oracle/diag/rdbms/sibcyb1/SIBCYB111/trace/SIBCYB111_ora_83111.trc (incident=803537) ...
  • 本文微信公眾號「AndroidTraveler」首發。 背景 在開發過程中,調試是必不可少的一項工作。 當我們要確定項目的邏輯時,當我們要瞭解界面的生命周期時,當我們發現新寫的邏輯與期望效果不一致時,當我們覺得數據有問題時...... 而調試有兩種方式: 第一種就是使用 debug 模式運行 APP ...
  • 一、overflow:hidden;作用 (1)可以將超出標簽範圍的內容裁剪掉 (2)清除浮動 .box1{ background-color: red; /*border:1px white solid;*/ overflow: hidden; } .box2{ background-color: ...
  • 字體屬性介紹 中的字體屬性是乾什麼的呢?字體字體肯定和字體有關咯,就是設置 頁面中文本的字體, 中常用的字體屬性有幾種呢,筆者給大家梳理了下,比較常用的一共有 種,今天我們就看看這 種能給文本的字體帶來什麼效果呢。 字體屬性定義文本的字體系列、大小、加粗、風格(如斜體)。 在 中常用的字體屬性有5種 ...
  • JS、JQ相關小技巧積攢,以備不時之需。 1.js 獲取時間差:時間戳相減。new Date().getTime() 獲得毫秒數,除以(1000*60*60*24) 獲得天數。 2.重定向操作:頁面重定向:window.location.href="http://..."即可(本頁頁面跳轉)。 3. ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...