跳转到主内容
趣航编程网 - 趣学编程,启航技术之路!

CSS3 3D制作实战案例分析

一、前言

上一节,介绍了基础的CSS3 3D动画原理实现,也举了一个小小的例子来演示,但是有朋友跟我私信说想看看一些关于CSS3 3D的实例,所以在这里为了满足一下大家的需求,同时也为了以后能够更好的巩固CSS3 3D的知识,所以在这里写下这篇博文,希望能够帮助你更好的理解3D的制作和实现原理,同时也欢迎各位小伙伴对文中的错误给予指正

二、入门案例分析

这里先说一说我的规划,我打算先从入门级的案例入手,然后依次递推,最后要达到的效果是,理解完所有的例子的设计思路,基本上CSS3-3D制作就能够随心所欲。

1、矩形图片翻滚效果

这个效果比较简单,所以在这里就不多做解释了,先来复习一下上一节说的要创建一个3D环境,我们需要创建一个“灯光”,“舞台”,“演员”(相当于perspective、preserve-3d、image),不清楚的小伙伴请看这里,具体的代码如下:XML/HTML Code复制内容到剪贴板Document</ title><style type="text/css">div div img{width:300px;height:300px;position:absolute;/* -webkit-transition: all 0.8s; */}div div{-webkit-transition: all 1s;}#img1{-webkit-transform: translateZ(150px);/* -webkit-transition: all 0.8s; */}#img2{-webkit-transform: rotateX(-90deg) translateZ(150px);/* -webkit-transition: all 0.8s; */}#img3{-webkit-transform: rotateZ(180deg) translateZ(-150px);}#img4{-webkit-transform: rotateX(90deg) translateZ(150px);}#img5{-webkit-transform:rotateY(90deg) translateZ(150px);}#img6{-webkit-transform: rotateY(-90deg) translateZ(150px);}/* div div:hover{-webkit-transform: rotateX(270deg);} */</ style></ head><body><div style="margin-left: 100px;height:300px;width:300px;position:absolute;perspective: 1000px;"><div id="box"style="transform-style: preserve-3d;height:300px;width:300px;position: relative;"><img id="img1"src="1.jpg"/><img id="img2"src="2.jpg"/><img id="img3"src="3.jpg"/><img id="img4"src="4.jpg"/><img id="img5"src="5.jpg"/><img id="img6"src="6.jpg"/></ div></ div><div style="margin-left: 700px;margin-top: 100px;"><input id="btn1"type="button"value="向上翻转90度"/><input id="btn2"type="button"name=""value="向左翻转90度"/><input id="btn3"type="button"value="向右翻转90度"/><input id="btn4"type="button"name=""value="向下翻转90度"/></ div><script type="text/javascript"src="http://cdn.bootcss.com/jquery/3.1.0/jquery.min.js"></ script><script>var x=0;var y=0;var box=$("#box");$('#btn1').on("click",function(){var value=90+x*90;box.css("-webkit-transform","rotateX("+value+"deg)");x++;});$('#btn2').on("click",function(){var value=y*90+90;box.css("-webkit-transform","rotateY("+value+"deg)");y++;});$('#btn3').on("click",function(){y--;var value=y*90;box.css("-webkit-transform","rotateX("+value+"deg)");});$('#btn4').on("click",function(){x--;var value=x*90;box.css("-webkit-transform","rotateX("+value+"deg)");});</ script></ body></ html></p> <p>分析</p> <p>虽然这个思路浅显易懂,但是作为本文的第一个例子还是想为大家尽量清晰的分析清楚,</p> <p>1、首先我们创建了一个“灯光”persepctive用来产生3D变化的效果的</p> <p>2、然后我们创建了一个“舞台”  transform-style: preserve-3d</p> <p>3、创建“演员”,这里的演员指的是每张的图片,在创建的时候要根据你最终要呈现的形态进行建模,也即是平移旋转之类的操作</p> <h3>脚本的编写:</h3> <p>在编写脚本的时候有一点是需要注意的,当你把图片旋转90度的时候,如果第二次还是设置90度,那么这个图片是不会发生变化的,因为图片的变化(旋转)是以初始位置为基准的,所以相对于初始位置两次的90度都是一样的所以没有任何的效果。这个在制作的时候,留意一下即可</p> <h3>2、矩形自动旋转效果</h3> <p>按照上面的例子进行整改,要实现自动的旋转其实就是定义一个keyframes的运动规则,然后在把这个keyframes套接到animation中,即可实现CSS自动旋转的功能</p> <p>部分的CSS3代码CSS Code复制内容到剪贴板div div{animation:route 10s infinite ease-in-out;}//keyframes运动规则@-webkit-keyframes route{0%{-webkit-transform:rotateX(90deg);}25%{-webkit-transform:rotateX(180deg) rotateY(90deg) rotateZ(90deg);}50%{-webkit-transform:rotateX(270deg) rotateY(180deg) rotateZ(180deg);}75%{-webkit-transform:rotateX(360deg) rotateY(270deg) rotateZ(270deg);}}</p> <h3>展示效果:</h3> <h2>三、CSS3 3D使用特效制作与分析</h2> <h3>1、模拟卡牌翻转效果</h3> <p>这个我就不多说怎样制作了,先上效果,图片是来源于蜗牛的,如有侵犯请告知</p> <p>这个虽然是归类到3D的范围内,但是在使用的时候由于不需要产生平行四边形的变换效果(这个的具体效果详见博主的另外一篇博文),所以不需要使用perspective这个属性,但是确实需要使用到CSS3 3D常见效果中的翻转属性(rotate[X,Y,Z]),这个的具体原理也是简单,但是在使用的时候有两点是需要注意的。</p> <p>一)、因为旋转是相对于卡牌的整体的旋转,也就是两个面都要旋转,所以hover事件要相对于卡牌整体,如果是相对于某一个面的话,那么会出现一个面旋转,而另外一个面不旋转的现象</p> <p>二)、在制作的过程中使用了一个比较少见的属性:backface-visibility,这个属性是为了见图片的背面设置为不可见,从而达到实现翻转的效果</p> <p>具体代码见文章最后</p> <p>2、CSS3 3D轮播</p> <p>说到CSS3 3D轮播主要我们可以分成这样的三类,一类是自动播放的轮播图,另外一类是响应点击事件的轮播图,第三类是两者的结合,即自动播放又响应点击事件,这里我们主要讨论的是前两者,第三类比较复杂这里我们就不多做解释,以后有机会再来跟大家探讨探讨</p> <p>2.1 自动轮播图</p> <p>这个的原理我们已经在前面中多次提到了,但是各位要注意一个地方,在keyframes中设置如:</p> <p>@keyframes test{o%,12.5%:{………………}}这样的一种形式可以实现轮播图的短暂停留。</p> <h3>好了看一下效果了:</h3> <p>2.2 CSS3触发轮播图</p> <p>主要的原理是通过判断点击的次数,然后是给图片父类添加旋转的属性,这样这个功能就实现了,我们对上面的例子进行改进,效果如下所示</p> <h2>四、代码下载</h2> <p>这一次代码进行了迁移,从原本自己的服务器迁移到了git服务器,git的下载地址:https://github.com/leslieSie/CSS3_example/tree/develop以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。</p> <h3>原文链接:</h3> <p>http://www.cnblogs.com/st-leslie/p/5791714.html</p> </div> <!-- 文章导航 --> <nav class="cxy-article-nav" aria-label="文章导航"> <a href="https://www.vqhf.com/article/19227.html" class="cxy-article-nav-link cxy-article-nav-prev"> <span>‹</span> <div> <div class="cxy-article-nav-label">上一篇</div> <div class="cxy-article-nav-title">DIV+CSS通用样式布局实例代码</div> </div> </a> <a href="https://www.vqhf.com/article/22087.html" class="cxy-article-nav-link cxy-article-nav-next"> <div> <div class="cxy-article-nav-label">下一篇</div> <div class="cxy-article-nav-title">CSS加载失败原因的总结与分析</div> </div> <span>›</span> </a> </nav> </div> </article> <!-- 相关文章列表 --> <section class="cxy-content-section" style="margin-top: 20px;"> <h2 class="cxy-widget-title" style="margin-bottom: 20px;">相关文章</h2> <ul class="cxy-article-list"> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/19227.html" class="cxy-article-link">DIV+CSS通用样式布局实例代码</a> <div class="cxy-article-meta">2026-10-07</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/14598.html" class="cxy-article-link">CSS Hack技术介绍及常用的Hack技巧集锦</a> <div class="cxy-article-meta">2026-09-28</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/14077.html" class="cxy-article-link">WEB前端涉及的布局、结构化和标准化</a> <div class="cxy-article-meta">2026-09-27</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/12812.html" class="cxy-article-link">a标签的bug hover状态不触发 可点区域不可点击</a> <div class="cxy-article-meta">2026-09-25</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/12413.html" class="cxy-article-link">css中filter属性和backdrop-filter的应用与区别详解</a> <div class="cxy-article-meta">2026-09-24</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/10993.html" class="cxy-article-link">CSS 样式书写规范(推荐)</a> <div class="cxy-article-meta">2026-09-22</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/9499.html" class="cxy-article-link">CSS鼠标悬浮动画-hover属性详解</a> <div class="cxy-article-meta">2026-09-19</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/22087.html" class="cxy-article-link">CSS加载失败原因的总结与分析</a> <div class="cxy-article-meta">2026-10-11</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/23484.html" class="cxy-article-link">初学者简单学习CSS网页布局</a> <div class="cxy-article-meta">2026-10-15</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/24205.html" class="cxy-article-link">css中hover变大效果</a> <div class="cxy-article-meta">2026-10-16</div> </li> </ul> </section> </div> <!-- 右侧边栏 --> <aside class="cxy-sidebar cxy-sidebar-right" aria-label="侧边栏"> <!-- 推荐阅读 --> <div class="cxy-widget"> <h3 class="cxy-widget-title">推荐阅读</h3> <div class="cxy-recommend-grid"> <a href="https://www.vqhf.com/article/24205.html" class="cxy-recommend-item"> <div class="cxy-recommend-thumb"> <img src="https://www.vqhf.com/static/v0/images/article-6.jpg" alt="css中hover变大效果"> </div> <div class="cxy-recommend-title">css中hover变大效果</div> </a> <a href="https://www.vqhf.com/article/23484.html" class="cxy-recommend-item"> <div class="cxy-recommend-thumb"> <img src="https://www.vqhf.com/static/v0/images/article-6.jpg" alt="初学者简单学习CSS网页布局"> </div> <div class="cxy-recommend-title">初学者简单学习CSS网页布局</div> </a> <a href="https://www.vqhf.com/article/22087.html" class="cxy-recommend-item"> <div class="cxy-recommend-thumb"> <img src="https://www.vqhf.com/static/v0/images/article-6.jpg" alt="CSS加载失败原因的总结与分析"> </div> <div class="cxy-recommend-title">CSS加载失败原因的总结与分析</div> </a> <a href="https://www.vqhf.com/article/20525.html" class="cxy-recommend-item"> <div class="cxy-recommend-thumb"> <img src="https://www.vqhf.com/thumb_1.jpg" alt="CSS3 3D制作实战案例分析"> </div> <div class="cxy-recommend-title">CSS3 3D制作实战案例分析</div> </a> </div> </div> <!-- 最新文章 --> <div class="cxy-widget"> <h3 class="cxy-widget-title">最新文章</h3> <ul class="cxy-article-list"> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/24205.html" class="cxy-article-link">css中hover变大效果</a> <div class="cxy-article-meta">2026-10-16</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/23484.html" class="cxy-article-link">初学者简单学习CSS网页布局</a> <div class="cxy-article-meta">2026-10-15</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/22087.html" class="cxy-article-link">CSS加载失败原因的总结与分析</a> <div class="cxy-article-meta">2026-10-11</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/20525.html" class="cxy-article-link">CSS3 3D制作实战案例分析</a> <div class="cxy-article-meta">2026-10-09</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/19227.html" class="cxy-article-link">DIV+CSS通用样式布局实例代码</a> <div class="cxy-article-meta">2026-10-07</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/14598.html" class="cxy-article-link">CSS Hack技术介绍及常用的Hack技巧集锦</a> <div class="cxy-article-meta">2026-09-28</div> </li> </ul> </div> <!-- 热门标签 --> <div class="cxy-widget"> <h3 class="cxy-widget-title">热门标签</h3> <div class="cxy-article-tag-list"> <a href="https://www.vqhf.com/tag/17629" class="cxy-article-tag-item">实战3D</a> </div> </div> </aside> </div> </div> </main> <!-- 页脚 --> <footer class="cxy-footer"> <div class="cxy-container"> <div class="cxy-footer-links" style="justify-content: center;"> <a href="https://www.vqhf.com/22.html" class="cxy-footer-link">免责声明</a> <a href="https://www.vqhf.com/23.html" class="cxy-footer-link">关于我们</a> <a href="https://www.vqhf.com/24.html" class="cxy-footer-link">联系我们</a> <a href="https://www.vqhf.com/25.html" class="cxy-footer-link">投诉删除</a> <a href="https://www.vqhf.com/26.html" class="cxy-footer-link">投稿须知</a> </div> <div class="cxy-footer-copyright" style="text-align: center; margin-top: 20px;"> Copyright 2026 趣航编程网 版权所有 <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank" style="color: inherit;">黔ICP备2025063733号</a> </div> <div style="text-align: center; margin-top: 10px; font-size: 12px; color: #999;">免责声明:本站信息来源于网络,仅供学习使用,版权归原作者所有,如有不愿意被转载的情况,请联系QQ:182111886,通知我们删除已转载的信息。</div> </div> </footer> <!-- 返回顶部按钮 --> <button class="cxy-back-to-top" id="backToTop" aria-label="返回顶部">⇧</button> <!-- 公共脚本 --> <script src="https://www.vqhf.com/static/v0/js/common.js"></script> <div class="hide"> </div> </body> <script> (function() { var al = document.createElement("script"); al.src = "/api/log?action=views&code=200&id=20525&path=http%3A%2F%2Fwww.vqhf.com%2Farticle%2F20525.html&type=archive&nonce="+Date.now(); document.body.appendChild(al); })(); </script> </html>