如何用CSS3構建一個具有遮蓋和粗邊框效果的Webpack Logo旋轉立方體?

使用css3構建具有遮罩和粗邊框效果的旋轉webpack logo立方體

本文詳細介紹如何利用css3構建一個酷炫的Webpack Logo旋轉立方體,該立方體包含內外兩層,并具有遮罩和粗邊框效果。 我們將改進初始代碼結構,以更有效地實現預期效果。

如何用CSS3構建一個具有遮蓋和粗邊框效果的Webpack Logo旋轉立方體?

初始方案嘗試使用::before和::after偽元素創建立方體,但在旋轉和遮罩效果方面存在不足。 這是因為偽元素并非構建三維立方體的理想選擇。 更優的方案是使用

元素分別表示立方體的六個面,并利用css3transform-style: preserve-3d;屬性,以及rotateX、rotateY、translateZ等屬性來實現三維旋轉和定位。 內層立方體通過調整尺寸和位置來實現對外層立方體的遮罩效果。 粗邊框則通過設置各個面的border-width屬性來實現。

改進后的html結構如下:

<div class="cube outer">   <div class="face front"></div>   <div class="face back"></div>   <div class="face top"></div>   <div class="face bottom"></div>   <div class="face left"></div>   <div class="face right"></div>   <div class="cube inner">     <div class="face front"></div>     <div class="face back"></div>     <div class="face top"></div>     <div class="face bottom"></div>     <div class="face left"></div>     <div class="face right"></div>   </div> </div>

對應的CSS樣式:

立即學習前端免費學習筆記(深入)”;

body {   background: #2b3a42; }  :root {   --depth: 50px; }  .cube {   width: 100px;   height: 100px;   position: relative;   transform-style: preserve-3d;   transform: translate(-50%, -50%) rotateX(-35deg) rotateY(-135deg) translateZ(var(--depth));   position: absolute;   top: 50%;   left: 50%; }  .face {   position: absolute;   width: 100px;   height: 100px;   box-sizing: border-box;   z-index: -1; }  .front {   transform: translateZ(var(--depth)); }  .back {   transform: rotateY(180deg) translateZ(var(--depth)); }  .top {   transform: rotateX(90deg) translateZ(var(--depth)); }  .bottom {   transform: rotateX(-90deg) translateZ(var(--depth)); }  .left {   transform: rotateY(-90deg) translateZ(var(--depth)); }  .right {   transform: rotateY(90deg) translateZ(var(--depth)); }  .outer > .face {   background: #75afcc;   border: 1px solid white; }  .outer > .back, .outer > .top, .outer > .right {   background: none;   border-width: 0.5px;   border-right-width: 5px;   border-bottom-width: 5px;   border-left-width: 5px; /*統一調整粗邊框*/   z-index: 100; }  .inner {   width: 50px;   height: 50px;   transform: translate(-50%, -50%); }  .inner > .face {   --depth: 25px;   width: 50px;   height: 50px;   background: #5299c8; }

通過將外層立方體部分面的背景設置為none,并調整border-width屬性,即可輕松實現遮罩和粗邊框效果。 內層立方體的尺寸和位置可根據需要調整。 此方法有效地構建了符合預期的Webpack Logo效果。

? 版權聲明
THE END
喜歡就支持一下吧
點贊15 分享