CSS3 卡片翻转(transform)

这里只考虑chrome的兼容。

card1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>卡片翻转——以下边为轴</title>
    <style>
        #my3dspace {
            -webkit-perspective: 800;
            -webkit-perspective-origin: 50% 50%;

            overflow: hidden;
        }
        #pagegroup {
            width: 400px;
            height: 400px;
            margin: 100px auto 0 auto;

            -webkit-transform-style: preserve-3d;
            position: relative;
        }
        .page {
            width: 360px;
            height: 360px;
            padding: 20px;
            background-color: black;
            color: white;

            font-weight: bold;
            font-size: 360px;
            line-height: 360px;
            text-align: center;

            position: absolute;
        }

        #page1 {
            -webkit-transform-origin: bottom; /*设置旋转元素的基点位置(以x轴)*/
            -webkit-transition: -webkit-transform 1s linear;
        }
        #page2,#page3,#page4,#page5,#page6 {
            -webkit-transform-origin: bottom;
            -webkit-transition: -webkit-transform 1s linear;

            -webkit-transform: rotateX(90deg); //沿着x轴进行90°的翻转(平躺)
        }

        #op {
            text-align: center;
        }
        #op a {
            display: inline-block;
            margin: 20px;
            text-decoration: none;

            color: #696969;
            border: 1px solid #CCC;
            padding: 10px 20px;
        }
        #op a:hover {
            background-color: #ABCDEF;
            color: #FFF;
        }
    </style>

    <script>
        var cur = 1;
        function next() {
            if(cur == 6)
                return;

            document.getElementById("page" + cur).style.webkitTransform = "rotateX(-90deg)";
            cur++;
            document.getElementById("page" + cur).style.webkitTransform = "rotateX(0)";
        }
        function previous() {
            if(cur == 1)
                return;

            document.getElementById("page" + cur).style.webkitTransform = "rotateX(90deg)";
            cur--;
            document.getElementById("page" + cur).style.webkitTransform = "rotateX(0)";
        }
    </script>
</head>
<body>
    <div id="my3dspace">
        <div id="pagegroup">
            <div class="page" id="page1">1</div>
            <div class="page" id="page2">2</div>
            <div class="page" id="page3">3</div>
            <div class="page" id="page4">4</div>
            <div class="page" id="page5">5</div>
            <div class="page" id="page6">6</div>
        </div>
    </div>
    <div id="op">
        <a href="javascript:next()">next</a> <a href="javascript:previous()">previous</a>
    </div>
</body>
</html>

运行结果如下:


点击next:


点击previous:



card2.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>卡片翻转——以左边翻转</title>
    <style>
        #my3dspace {
            -webkit-perspective: 800px;
            -webkit-perspective-origin: 50% 50%;

            overflow: hidden;
        }
        #pagegroup {
            width: 400px;
            height: 400px;
            margin: 100px auto;

            -webkit-transform-style: preserve-3d;
            position: relative;
        }
        .page {
            width: 360px;
            height: 360px;
            padding: 20px;
            font-size: 360px;
            line-height: 360px;
            background-color: black;
            color: white;
            text-align: center;
            position: absolute;
            font-weight: bold;
        }

        #page1 {
            -webkit-transform-origin: left;
            -webkit-transition: -webkit-transform 1s ease-in;
        }
        #page2, #page3, #page4, #page5, #page6 {
            -webkit-transform-origin: left;
            -webkit-transition: -webkit-transform 1s ease-in;

            -webkit-transform: rotateY(0);
        }

        #op {
            text-align: center;
        }
        #op a {
            display: inline-block;
            margin: 20px;
            text-decoration: none;

            color: #696969;
            border: 1px solid #CCC;
            padding: 10px 20px;
        }
        #op a:hover {
            background-color: #ABCDEF;
            color: #FFF;
        }
    </style>
    <script>
        var cur = 1;
        function next() {
            if(cur == 7)
                return;

            document.getElementById("page" + cur).style.webkitTransform = "rotateY(-160deg)";
            cur++;
        }

        function previous() {
            if(cur == 0)
                return;
            cur--;
            document.getElementById("page" + cur).style.webkitTransform = "rotateY(0)";
        }
    </script>
</head>
<body>
    <div id="my3dspace">
        <div id="pagegroup">
            <div class="page"></div>
            <div class="page" id="page6">6</div>
            <div class="page" id="page5">5</div>
            <div class="page" id="page4">4</div>
            <div class="page" id="page3">3</div>
            <div class="page" id="page2">2</div>
            <div class="page" id="page1">1</div>
        </div>
    </div>
    <div id="op">
        <a href="javascript:next()">next</a> <a href="javascript:previous()">previous</a>
    </div>
</body>
</html>
运行结果如下:


点击next:


点击previous:




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值