CSS3動畫實現程式媛的小浪漫

2020-10-12 16:00:24

不必多說,讓我們來康康程式碼~

首先我們要鋪好HTML骨架

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>小浪漫</title>
    <link rel="stylesheet" href="css/prac-3d.css">
</head>
<body>
    <div class="ffa">
        <div class="fa">
            <!-- 內層 -->
            <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 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>
        </div>
    </div>
</body>
</html>

接下來給我們的HTML穿上漂亮的衣服

/* 初始化 */
* {
    padding: 0;
    margin: 0 auto;
}
/* 背景 */
html,body {
    background-color: plum;
    height: 100%;
}
/* 正方體旋轉動畫 */
@keyframes rotate {
    from {
        transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
    }
    to {
        transform: rotateX(360deg) rotateY(360deg) rotateZ(360deg);
    }
}
/* 開啟視距 */
.ffa {
    margin-top: 200px;
    perspective: 1000px;
}
.fa {
    transform-style: preserve-3d;
    width: 200px;
    height: 200px;
    position: relative;
    transform: rotateX(-30deg) rotate(-70deg);
    animation: rotate 10s infinite linear;
}
.fa div {
	width: 200px;
    height: 200px;
    position: absolute;
    border: 1px solid powderblue;
	transition: transform .4s ease-in;
}
.fa span {
    display: block;
	text-align: center;
	line-height: 100px;
    position: absolute;
    color: black;
    top: 50px;
    left: 50px;
    width: 100px;
    height: 100px;
    border: 1px solid white;
    background-color: pink;
    opacity: .6;
}
/* 內層正方體 */
.in-front {
    transform: translateZ(50px);
}
.in-back {
    transform: translateZ(-50px) rotateY(180deg);
}
.in-left {
    transform: translateX(-50px) rotateY(-90deg);
}
.in-right {
    transform: translateX(50px) rotateY(90deg);
}
.in-top {
    transform: translateY(50px) rotateX(-90deg);
}
.in-bottom {
    transform: translateY(-50px) rotateX(90deg);
}
/* 外層正方體 滑鼠經過動效 */
.ffa:hover .out-front{
	transform: translateZ(200px);
}
.ffa:hover .out-back{
	transform: translateZ(-200px)  rotateY(180deg);
}
.ffa:hover .out-left{
	transform: translateX(-200px)  rotateY(-90deg);
}
.ffa:hover .out-right{
	transform: translateX(200px)  rotateY(90deg);
}
.ffa:hover .out-top{
	transform: translateY(-200px)  rotateX(90deg);
}
.ffa:hover .out-bottom{
	transform: translateY(200px)  rotateX(-90deg);
}
/* 外層正方體 */
.fa .out-front{
    transform: translateZ(100px);
    background: url(../image/bailu.jpg) no-repeat;
    background-size: cover;
}
.fa .out-back{
    transform:translateZ(-100px) rotateY(180deg);
    background: url(../image/bailu.jpg) no-repeat;
    background-size: cover;
}
.fa .out-left{
    transform:translateX(-100px) rotateY(-90deg);
    background: url(../image/bailu.jpg) no-repeat;
    background-size: cover;
}
.fa .out-right{
    transform:translateX(100px) rotateY(90deg);
    background: url(../image/bailu.jpg) no-repeat;
    background-size: cover;
}
.fa .out-top{
    transform:translateY(-100px) rotateX(90deg);
    background: url(../image/bailu.jpg) no-repeat;
    background-size: cover;
}
.fa .out-bottom{
    transform:translateY(100px) rotateX(-90deg);
    background: url(../image/bailu.jpg) no-repeat;
    background-size: cover;
}

到此,一個小小的 3D效果的小浪漫我們就寫好啦,讓我們來康康效果!
滑鼠未經過之前的效果
滑鼠經過後的效果
有沒有手癢,決定動手寫一個呢?

最後:純屬個人練習程式碼,如有不嚴謹處,還望各位大佬指出~