温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

css3D+动画的示例分析

发布时间:2021-01-29 13:39:25 阅读:228 作者:小新 栏目:web开发
前端开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章给大家分享的是有关css3D+动画的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

最近玩了玩用css来构建3D效果,写了几个demo,所以博客总结一下。  在阅读这篇博客之前,请先自行了解一下css 3D的属性,例如:transform-style,transform-origin,transform, perspective。

写一个简单的立方体

1、我们先用css实现一个长方体,一个长方体有6个边,我们写6个li,并用一个ul包裹起来,根据我写3D动画的经验,最好有一个父元素来包裹

<p class="parent">
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
    </ul>
</p>

2、先给.parent设置宽高,并且给他设置视距和基点位置。

.parent{
            width800px;
            height400px;
            border1px solid #000;
            margin0 auto;
            perspective2000px;
            perspective-origin: -40% -80%;
            background#000;
        }

3、给ul设置宽高以及preserve-3d属性保留子元素3d转换,子元素li全部绝对定位

        ul{
            width50px;
            position: relative;
            margin100px auto;
            transform-style : preserve-3d;
        }
         li{
            width100px;
            height100px;
            background:  rgba(25525500.3);
            position: absolute;
            text-align: center;           
            border3px solid greenyellow;
        }

效果如下图所示:

css3D+动画的示例分析

4、先写一个面,给他的背景设置 background:  rgba(255, 255, 0, 0.3);

 li:nth-child(1){
            background:  rgba(25525500.3);
            transform:  translateY(50pxrotateX(90deg);
        }

效果如下图所示:

css3D+动画的示例分析

5、我们写好了第一个面,然后我们在将其他6个面调整好,变成下图所示。关于rotate的旋转方向这里不解释,不懂的朋友可以自行查看其他文档。

        /*上面*/
         li:nth-child(1){
            transformtranslateY(-50pxrotateX(90deg);
        } 
        /*下面*/
        li:nth-child(2){
        
            transform:  translateY(50pxrotateX(90deg);
        }
        /*左面*/
        li:nth-child(3){
            transformtranslateX(-50pxrotateY(90deg);
        }
        /*右面*/
        li:nth-child(4){
            transformtranslateX(50pxrotateY(90deg);
        }
        /*前面*/
        li:nth-child(5){
            transformtranslateZ(50px);
        }
        /*后面*/
        li:nth-child(6){
            transformtranslateZ(-50px);
        }

效果如下图所示:

css3D+动画的示例分析

下面是两种css3D+动画的效果

1、代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>书页2</title>
    <style>
        .container{
            width1000px;
            height650px;
            background#000;
            perspective2000px;
            border1px solid transparent;
            overflow: hidden;
            margin0 auto;
            perspective-origin10% 20%;
           
        }
    
        .cube{
            width200px;
            height300px;
            transform-style: preserve-3d;
            margin:100px auto;
            
            position: relative;
            transformrotateX(30deg);
            border-radius50%;
            padding60px;
        }
        .mian{
            width200px;
            height300px;
            background-imageurl(1.jpg);
            background-position:400px 0;
            position: absolute;
           
            border1px solid #ccc;
            transition2s;
        }
        /* .mian1:hover{
            transform-origin: right;
            transform: rotateY(-60deg);
        } */
        .mian1{
            transform-origin: right;
            transformtranslateX(-200pxrotateY(45deg);
            background-position0 0;
        }
        .mian3{
            transform-origin: left;
            transformtranslateX(200pxrotateY(45deg);
            background-position200px 0;
        }
        .mian3:hover{
            transformtranslateX(200pxrotateY(0deg);
        } 
        .mian1:hover{
            transformtranslateX(-200pxrotateY(0deg);
        }
    </style>
</head>
<body>
    <p class="container">
        <p class="cube">
            <p class="mian mian1"></p>
            <p class="mian mian2"></p>
            <p class="mian mian3"></p>
        </p>
    </p>
</body>
</html>

2、代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>立方体</title>

    <style>
        *{
            margin0;
            padding0;
            list-style: none;
        }
        .parent{
            width1000px;
            margin:  0 auto;
            height600px;
            background: black;
            perspective5000px;
            perspective-origin: -40% -120%;
            border1px solid #000;
        }
        ul{
            width100px;
            height300px;
            position: relative;
            margin:100px auto;
            transform-style: preserve-3d;
            animation: zuan 3s linear infinite;
            border1px solid greenyellow;
        }

        li{
            width100px;
            height300px;
            background:  rgba(0000.5);
            position: absolute;
            text-align: center;
            line-height100px;
            
            border3px solid greenyellow;
        }
        li:nth-child(1){
            transformrotateY(30degtranslateZ(-200px);
      
        }
        li:nth-child(2){
            transformrotateY(60degtranslateZ(-200px);
            background:  rgba(255000.5);
        }
        li:nth-child(3){
            transformrotateY(90degtranslateZ(-200px);
            
        }
        li:nth-child(4){
            transformrotateY(120degtranslateZ(-200px);
            background:  rgba(002550.5);
        }
        li:nth-child(5){
            transformrotateY(150degtranslateZ(-200px);
            
        }
        li:nth-child(6){
            transformrotateY(180degtranslateZ(-200px);
            background:  rgba(25502550.5);
        }
        li:nth-child(7){
            transformrotateY(210degtranslateZ(-200px);
      
        }
        li:nth-child(8){
            transformrotateY(240degtranslateZ(-200px);
            background:  rgba(025500.5);
        }
        li:nth-child(9){
            transformrotateY(270degtranslateZ(-200px);
      
        }
        li:nth-child(10){
            transformrotateY(300degtranslateZ(-200px);
            background:  rgba(02552550.5);
        }
        li:nth-child(11){
            transformrotateY(330degtranslateZ(-200px);
      
        }
        li:nth-child(12){
            transformrotateY(360degtranslateZ(-200px);
            background:  rgba(2552552550.5);
        }

        @keyframes zuan{
            0%{
                transformrotateY(0deg);
            }
            100%{
                transformrotateY(360deg);
            }
        }
    
    </style>
</head>
<body>

    <p class="parent">
        <ul>    
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </p>
    
</body>
</html>

效果如下图:

css3D+动画的示例分析

感谢各位的阅读!关于“css3D+动画的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×