首頁  >  文章  >  web前端  >  純css3實作3D立方體旋轉特效程式碼

純css3實作3D立方體旋轉特效程式碼

零下一度
零下一度原創
2017-05-17 17:36:092998瀏覽

上網易前端微專業課程,裡面有一個課外作業是實現一個3D旋轉立方體,花了點時間做了下,還有點意思,寫個簡單教程,供大家學習。 先放上最終要實現的效果.

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><title>CSS3 3D立方体多边形动画特效 - 代码笔记</title><style type="text/css">*{
 margin:0 auto;
    padding:0;
}
@keyframes rotate{
    0%{
        transform:rotateX(0deg) rotateY(0deg);
    }
    100%{
        transform:rotateX(360deg) rotateY(360deg);
    }
}
html{
    background:linear-gradient(#ff0 0%, #000 80%);
    height:100%;
}
.wrap{
    margin-top:200px;
    perspective: 1000px; /* 视图距元素的距离 相当于摄像机 */
}
.cube{
    width:200px;
    height:200px;
    position:relative;
    color:#fff;
    font-size:36px;
    font-weight:bold;
    text-align:center;
    line-height:200px;
    transform-style:preserve-3d; /* 默认flat 2D */
    transform:rotateX(-30deg) rotateY(-70deg); 
    animation:rotate 20s infinite linear; /*播放时间 播放次数为循环 缓动效果为匀速 */
}
.cube > div{
    width:100%;
    height:100%;
    border:1px solid #fff;
    position:absolute;
    background-color:#333;
    opacity:.6;
    transition:transform 0.4s ease-in;
}
.cube .out-front{
    transform: translateZ(100px);
}
.cube .out-back{
    transform: translateZ(-100px) rotateY(180deg);
}
.cube .out-left{
    transform: translateX(-100px) rotateY(-90deg);
}
.cube .out-right{ 
    transform: translateX(100px) rotateY(90deg);
}
.cube .out-top{
    transform: translateY(-100px) rotateX(90deg);
}
.cube .out-bottom{
    transform: translateY(100px) rotateX(-90deg);
}
.cube > span{
    display:block;
    width:100px;
    height:100px;
    border:1px solid black;
    background-color:#999;
    position:absolute;
    top:50px;
    left:50px;
}
.cube .in-front{
    transform: translateZ(50px);
}
.cube .in-back{
    transform: translateZ(-50px) rotateY(180deg);
}
.cube .in-left{
    transform: translateX(-50px) rotateY(-90deg);
}
.cube .in-right{
    transform: translateX(50px) rotateY(90deg);
}
.cube .in-top{
    transform: translateY(-50px) rotateX(90deg);
}
.cube .in-bottom{
    transform: translateY(50px) rotateX(-90deg);
}
.wrap:hover .out-front{
    transform: translateZ(200px);
}
.wrap:hover .out-back{
    transform: translateZ(-200px) rotateY(180deg);
}
.wrap:hover .out-left{
    transform: translateX(-200px) rotateY(-90deg);
}
.wrap:hover .out-right{
    transform: translateX(200px) rotateY(90deg);
}
.wrap:hover .out-top{
    transform: translateY(-200px) rotateX(90deg);
}
.wrap:hover .out-bottom{
    transform: translateY(200px) rotateX(-90deg);
}
 
</style>
</head>
<body>
<div class="wrap">
    <div class="cube">
        <div class="out-front">前面</div>
        <div class="out-back">后面</div>
        <div class="out-left">左面</div>
        <div class="out-right">右面</div> 
        <div class="out-top">上面</div>
        <div class="out-bottom">下面</div>
 
        <span class="in-front"></span>
        <span class="in-back"></span>
        <span class="in-left"></span>
        <span class="in-right"></span>
        <span class="in-top"></span>
        <span class="in-bottom"></span>
    </div>
</div>
 
<div style="text-align:center;margin:50px 0; font:normal 14px/24px &#39;MicroSoft YaHei&#39;;">
<p>适用浏览器:360、FireFox、Chrome、Opera、傲游、搜狗、世界之窗. 不支持Safari、IE8及以下浏览器。</p>
</div>
</body>
</html>

【相關推薦】

#1. #特別推薦#:“ php程式設計師工具箱」V0.1版本下載

2. 免費css線上影片教學

3. php.cn獨孤九賤(2)-css影片教學

#

以上是純css3實作3D立方體旋轉特效程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn