CSS3—3D翻转

本案例主要是css3和html5,不会js也能够作动画◕.◕css

1、涉及到的的样式html

perspectivehtml5

transformcss3

transitiondom

positionwordpress

classList动画

codepenrunjs演示代码和讲解在后面。this

2、html结构spa

3个容器6个盒子,当鼠标通过时:ssr

1.box1绕X轴(transform-origin默认容器中心),翻转180°至背面box2,鼠标移开翻回

2.box3绕Y轴(transform-origin默认容器中心),翻转180°至背面box4,鼠标移开翻回

3.box5,box6绕Z轴,(transform-origin分别为容器左右),翻转180°,鼠标移开翻回

<div class="container" ontouchstart="this.classList.toggle('hover');">
    <div class="box box1">
        <span>front</span>
    </div>
    <div class="box box2">
        <span>back</span>
    </div>
</div>
<div class="container" ontouchstart="this.classList.toggle('hover');">
    <div class="box box3">
        <span>front</span>
    </div>
    <div class="box box4">
        <span>back</span>
    </div>
</div>
<div class="container" ontouchstart="this.classList.toggle('hover');">
    <div class="box box6">
        <span>back</span>
    </div>
    <div class="box box5">
        <span>front</span>
    </div>
</div>

 

3、CSS样式

1.容器上加了perspective子元素box有透视效果

2.box2,box4是背面因此先翻转-180°,这样当翻到后面再翻回时符合正常视觉

3.每一个box都加了backface-visibility:隐藏被旋转的 div 元素的背面

4.box5,box6,改了旋转中心点

<style>
.container{
    perspective: 400px;
    transform-style: preserve-3d;
}
.container, .box{
    width: 150px;
    height: 80px;
    margin: 10px auto;
}
.box{
    backface-visibility: hidden;
    transition: 1s;
    transform-style: preserve-3d;
    position: absolute;
    text-align: center;
}
.box1,.box3,.box5{background-color: pink;}
.box2,.box4,.box6{background-color: red;}
.box2{transform: rotateX(-180deg);}
.box4{transform: rotateY(-180deg);}
.box5{transform-origin:left;}
.box6{transform-origin:right;}
span{
    font-size: 20px;
    line-height: 80px;
}
.container:hover .box1{transform: rotateX(180deg);}
.container:hover .box2{transform: rotateX(0deg);}
.container:hover .box3{transform: rotateY(180deg);}
.container:hover .box4{transform: rotateY(0deg);}
.container:hover .box5{transform: rotateZ(180deg);}
.container:hover .box6{transform: rotateZ(-180deg);}
</style>

 

 是否是很简单,接下来随意翻腾吧~

 

博客园:CSS3—3D翻转

相关文章
相关标签/搜索