<!DOCTYPE html><html>
<head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>css3制作立方体</title> <link rel="stylesheet" href="https://stackedit.io/style.css" /></head>
<body class="stackedit"> <div class="stackedit__html"><h2><a id="_0"></a>一、预备知识</h2><ol><li><strong>变形属性</strong></li></ol><p><strong>2D变形属性</strong></p><blockquote><p>transform:他是css3中的变形属性:<br>通过transform(变形) 来实现2d 或者3d 转换,其中2d 有,缩放 scale(x, y) ,移动 translate(x, y),旋转 rotate(deg),倾斜 skew(deg, deg) ,矩阵matrix() ,3D就是在2D基础上增加了Z轴,2D属性仍可以使用。</p></blockquote><p><strong>3D转换属性</strong></p><blockquote><ol><li>透视(perspective)景深(900-1200)<br>电脑显示屏是一个2D平面,图像之所以具有立体感(3D效果),其实只是一种视觉呈现,通过透视可以实现此目的。<br>透视可以将一个2D平面,在转换的过程当中,呈现3D效果。</li></ol></blockquote><blockquote><ol start="2"><li>3D呈现(transform-style),值为preserve-3d:保留3D空间(本次正方形就是利用此)<br>形成一个3D空间:( 让父元素形成3D,让其子元素在3D空间进行变化 )</li></ol></blockquote><blockquote><ol start="3"><li>backface-visibility:设置元素背面是否可见</li></ol></blockquote><h2><a id="_20"></a>二、步骤:</h2><p>1、首先设置一个大盒子包裹住六个div</p><pre><code class="prism language-html"> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>box<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>con1<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>1<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>con2<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>2<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>con3<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>3<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>con4<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>4<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>con5<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>5<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>con6<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>6<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span></code></pre><p>2、 <strong>让正方体呈现3D效果</strong></p><blockquote><p>形成一个3D空间:( 让父元素形成3D,让其子元素在3D空间进行变化 )</p></blockquote><pre><code class="prism language-css"><span class="token selector">.box</span><span class="token punctuation">{</span> <span class="token property">width</span><span class="token punctuation">:</span>300px<span class="token punctuation">;</span> <span class="token property">height</span><span class="token punctuation">:</span>300px<span class="token punctuation">;</span> <span class="token property">position</span><span class="token punctuation">:</span>fixed<span class="token punctuation">;</span> <span class="token property">left</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span><span class="token property">right</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span> <span class="token property">top</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span><span class="token property">bottom</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span> <span class="token property">margin</span><span class="token punctuation">:</span>auto<span class="token punctuation">;</span> <span class="token comment">/* 3D的空间 */</span> <span class="token property">transform-style</span><span class="token punctuation">:</span> preserve-3d<span class="token punctuation">;</span> <span class="token comment">/* 为了方便观察让3D舞台转动角度 */</span> <span class="token property">transform</span><span class="token punctuation">:</span><span class="token function">rotateX</span><span class="token punctuation">(</span>20deg<span class="token punctuation">)</span> <span class="token function">rotateY</span><span class="token punctuation">(</span>30deg<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token property">transition</span><span class="token punctuation">:</span>2s<span class="token punctuation">;</span> <span class="token punctuation">}</span></code></pre><p>3.、定位6个面,使其在正方形里面</p><pre><code class="prism language-css"><span class="token selector">.box div</span><span class="token punctuation">{</span> <span class="token property">width</span><span class="token punctuation">:</span>300px<span class="token punctuation">;</span> <span class="token property">height</span><span class="token punctuation">:</span>300px<span class="token punctuation">;</span> <span class="token property">text-align</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span> <span class="token property">line-height</span><span class="token punctuation">:</span>300px<span class="token punctuation">;</span> <span class="token property">font-size</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span> <span class="token property">font-weight</span><span class="token punctuation">:</span>bolder<span class="token punctuation">;</span> <span class="token property">color</span><span class="token punctuation">:</span>#fff<span class="token punctuation">;</span> <span class="token comment">/* 让6个面全部定位在父元素里面 */</span> <span class="token property">position</span><span class="token punctuation">:</span>absolute<span class="token punctuation">;</span> <span class="token property">left</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span><span class="token property">top</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span> <span class="token comment">/* 透明 */</span> <span class="token property">opacity</span><span class="token punctuation">:</span>0.5<span class="token punctuation">;</span> <span class="token property">border</span><span class="token punctuation">:</span>2px solid #000<span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p>现在的效果看一下:<br><img src="https://www.icode9.com/i/ll/?i=20200308212510430.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMjgwNzQ2,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p><p>4、接下来就要为每个面来进行位移和旋转</p><pre><code class="prism language-css"> <span class="token selector">.con1</span><span class="token punctuation">{</span> <span class="token comment">/* 第一个面往前走 */</span> <span class="token property">background</span><span class="token punctuation">:</span>red<span class="token punctuation">;</span> <span class="token property">transform</span><span class="token punctuation">:</span><span class="token function">translateZ</span><span class="token punctuation">(</span>150px<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token selector">.con2</span><span class="token punctuation">{</span> <span class="token comment">/* 第二个面往后走 */</span> <span class="token property">background</span><span class="token punctuation">:</span>blue<span class="token punctuation">;</span> <span class="token property">transform</span><span class="token punctuation">:</span><span class="token function">translateZ</span><span class="token punctuation">(</span>-150px<span class="token punctuation">)</span> <span class="token function">rotateY</span><span class="token punctuation">(</span>180deg<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">/*rotateY(180deg) 让正面朝外*/</span> <span class="token punctuation">}</span> <span class="token selector">.con3</span><span class="token punctuation">{</span> <span class="token comment">/* 先往上位移150px 再绕着X轴转动90deg */</span> <span class="token property">background</span><span class="token punctuation">:</span>pink<span class="token punctuation">;</span> <span class="token property">transform</span><span class="token punctuation">:</span><span class="token function">translateY</span><span class="token punctuation">(</span>-150px<span class="token punctuation">)</span> <span class="token function">rotateX</span><span class="token punctuation">(</span>90deg<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token selector">.con4</span><span class="token punctuation">{</span> <span class="token comment">/* 先往下位移150px,再绕着X轴转动90deg */</span> <span class="token property">background</span><span class="token punctuation">:</span>green<span class="token punctuation">;</span> <span class="token property">transform</span><span class="token punctuation">:</span><span class="token function">translateY</span><span class="token punctuation">(</span>150px<span class="token punctuation">)</span> <span class="token function">rotateX</span><span class="token punctuation">(</span>-90deg<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
<span class="token selector">.con5</span><span class="token punctuation">{</span> <span class="token comment">/* 先往左位移150px , 再绕着Y轴转动90deg */</span> <span class="token property">background</span><span class="token punctuation">:</span>rosybrown<span class="token punctuation">;</span> <span class="token property">transform</span><span class="token punctuation">:</span><span class="token function">translateX</span><span class="token punctuation">(</span>-150px<span class="token punctuation">)</span> <span class="token function">rotateY</span><span class="token punctuation">(</span>-90deg<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token selector">.con6</span><span class="token punctuation">{</span> <span class="token comment">/* 先往右侧位移150px,再绕着Y轴转动90deg */</span> <span class="token property">background</span><span class="token punctuation">:</span>#000<span class="token punctuation">;</span> <span class="token property">transform</span><span class="token punctuation">:</span><span class="token function">translateX</span><span class="token punctuation">(</span>150px<span class="token punctuation">)</span> <span class="token function">rotateY</span><span class="token punctuation">(</span>90deg<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
</code></pre><p>我们再来看下效果就出来了<br><img src="https://www.icode9.com/i/ll/?i=20200308213047845.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMjgwNzQ2,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br>以下两个属性不是必须的,为了显示,我们都学习下。</p><p>5、这里我加了给元素加backface-visibity:hidden;就可使元素背面不可见</p><pre><code class="prism language-css"><span class="token selector">.box div</span><span class="token punctuation">{</span> <span class="token comment">/* 使背面不可见! */</span> <span class="token property">backface-visibility</span><span class="token punctuation">:</span>hidden<span class="token punctuation">;</span> <span class="token punctuation">}</span></code></pre><p><img src="https://www.icode9.com/i/ll/?i=20200308213430244.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMjgwNzQ2,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p><p>6、最后我们在将开头提到的“景深”应用下</p><p>一般我们 都在父元素中加</p><pre><code class="prism language-css"><span class="token selector">.box</span><span class="token punctuation">{</span><span class="token property">perspective</span><span class="token punctuation">:</span> 1200px<span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><p>通常的数值在900-1200之间<br>如果当你的视线距离物体足够远的时候,基本上就不会有近大远小的感觉</p><p>对比下,既可以看到效果<br><img src="https://www.icode9.com/i/ll/?i=2020030821402781.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMjgwNzQ2,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p></div></body>
</html>