css图片翻转

最近在复习css动画效果的时候,跟着做了一个图片翻转和开门大吉的css小玩意:
图片翻转:

图片翻转的难点在于如何使一张图片不随着另一张图片一起翻转,使得每翻转一次的结果都只是一张图片,要解决这个问题就要用到css中的 backface-visibility属性,顾名思义,就是使图片不是正面对向屏幕的时候就隐藏。源代码如下:

<!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>
        *{
            padding: 0%;
            margin: 0%;
        }
        div{
            width: 260px;
            height: 260px;
            margin: 100px auto;
            position: relative;
        }
        div img{
            position: absolute; /*子绝父相*/
            top:0;
            left: 0;
            transition: all 1s;
        }
        div img:first-child{
            z-index: 1;
            backface-visibility: hidden; /*不是正面对向屏幕就隐藏  试试删除这行代码会有什么效果*/
        }
        div:hover img{
            transform: rotateY(180deg);
        }
    </style>
</head>
<body>
    <div>
        <img src="img/qian.jpg">
        <img src="img/hou.jpg">
    </div>  
</body>
</html>

开门大吉:

这里我使用的图片太大了以至于只有半截,可以调整盒子宽度或者选择合适的图片进行操作,开门大吉的关键点在于css定位的使用,开门大吉的源代码如下:

<!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>
        body{
            padding: 0%;
            margin: 0%;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: rgba(0, 0, 0, .1);
        }
        section{
            width: 450px;
            height: 300px;
            border: 1px solid #000;
            margin: 100px auto;
            position: relative;
            perspective: 1000px;
            background: url(img/1.jpg) no-repeat;
        }
        .door-1,.door-2{
            position: absolute;
            top:0;
            width: 50%;
            height: 100%;
            background-color: darkgoldenrod;
            transition: all 1s;
        }
        .door-1{
            left: 0;
            border-right: 1px solid #000;
            transform-origin: left;
        }
        .door-2{
            right: 0;
            border-left: 1px solid #000;
            transform-origin: right;
        }
        .door-1::before,
        .door-2::before{
            content: "";
            position: absolute;
            top: 50%;
            width: 20px;
            height: 20px;
            border: 1px solid #000;
            border-radius: 50%;
            transform: translateY(-50%);
        }
        .door-2::before{
            left: 5px;
        }
        .door-1::before{
            right: 5px;
        }
        section:hover .door-1{
            transform: rotateY(-130deg);
        }
        section:hover .door-2{
            transform: rotateY(130deg);
        }
    </style>
</head>
<body>
    <section>
        <div class="door-1"></div>
        <div class="door-2"></div>
    </section>

</body>
</html>

如有问题可以给我留言,看到了就会解答哦

点赞
  1. Royal CBD说道:

    This post is invaluable. Where can I find out more?

    My web page; Royal CBD

发表评论

昵称和uid可以选填一个,填邮箱必填(留言回复后将会发邮件给你)
tips:输入uid可以快速获得你的昵称和头像

Title - Artist
0:00