喜欢网络 - 技术宅分享站

HTML+CSS实现玻璃形态卡片悬停效果!!附源码!!

效果演示

完整源码

<!DOCTYPE html>
<html>
    <head>    
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">    <title>上浮文字</title>
    <style>
        /* 引入网络字体:Poppins */
        @import url("http://fonts.googleapis.com/css?family=Poppins:200,300,400,500,600,700,800,900&display=swap");

        * {
            /* 初始化 */
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            /* 设置字体 */
            font-family: "Poppins";
        }

        body {
            /* 100%窗口高度 */
            min-height: 100vh;
            /* 弹性布局 水平+垂直居中 */
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #161626;
        }

        /* 给背景增加两个渐变圆 */
        body::before {
            content: "";
            /* 绝对定位 */
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            /* 渐变背景 */
            background: linear-gradient(#2193b0, #6dd5ed);
            /* 将元素剪切微一个圆形【30%表示圆的直径】【right 70%表示圆心位置】 */
            clip-path: circle(30% at right 70%);
        }

        body::after {
            content: "";
            /* 绝对定位 */
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            /* 渐变背景 */
            background: linear-gradient(#ee9ca7, #ffdde1);
            /* 将元素剪切微一个圆形【30%表示圆的直径】【right 70%表示圆心位置】 */
            clip-path: circle(20% at 10% 10%);
        }

        .container {
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-wrap: wrap;
            z-index: 1;
        }

        .container .card {
            /* 相对定位 */
            position: relative;
            width: 280px;
            height: 400px;
            background-color: rgba(255, 255, 255, 0.1);
            margin: 30px;
            border-radius: 15px;
            /* 阴影 */
            box-shadow: 20px 20px 50px rgba(0, 0, 0, 0.5);
            /* 溢出隐藏 */
            overflow: hidden;
            display: flex;
            justify-content: center;
            align-items: center;
            border-top: 1px solid rgba(255, 255, 255, 0.5);
            border-left: 1px solid rgba(255, 255, 255, 0.5);
            /* 背景模糊 */
            backdrop-filter: blur(5px);
        }

        .container .card .content {
            padding: 20px;
            text-align: center;
            /* 默认下移+隐藏 */
            transform: translateY(100px);
            opacity: 0;
            transition: 0.5s;
        }

        .container .card:hover .content {
            /* 鼠标移入,上移+显示 */
            transform: translateY(0);
            opacity: 1;
        }

        .container .card .content h2 {
            position: absolute;
            top: -80px;
            right: 25px;
            font-size: 128px;
            color: rgba(255, 255, 255, 0.05);
        }

        .container .card .content h3 {
            font-size: 28px;
            color: #fff;
        }

        .container .card .content p {
            font-size: 16px;
            color: #fff;
            font-weight: 300;
            margin: 10px 0 15px 0;
        }

        .container .card .content a {
            position: relative;
            display: inline-block;
            padding: 8px 20px;
            margin-top: 15px;
            background-color: #fff;
            color: #000;
            text-decoration: none;
            border-radius: 20px;
            font-weight: 500;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }

        /* 这里要让玻璃效果更加逼真,需要用到一个JS库:vanilla-tilt.js */
        /* 我事先下载好了,需要这个JS库的可以找我拿 */
        /* 看操作~ */
    </style>
    </head>
    <body>
        <div class="container">
            <div class="card">
                <div class="content">
                    <h2>01</h2>
                    <h3>Card One</h3>
                    <p>Realistic glass card hover effect,
                        realistic glass card hover effect, realistic glass card hover effect.</p>
                    <a href="#">Read More</a>
                /div>
            </div>
            <div class="card">
                <div class="content">
                    <h2>02</h2>
                    <h3>Card Two</h3>
                    <p>Realistic glass card hover effect, realistic glass card hover effect, realistic glass card hover effect.</p>
                    <a href="#">Read More</a>
                </div>
            </div>
            <div class="card">
                <div class="content">
                    <h2>03</h2>
                    <h3>Card Three</h3>
                    <p>Realistic glass card hover effect, realistic glass card hover effect,realistic glass card hover effect, realistic glass card hover effect.</p><a href="#">Read More</a>
                </div>
            </div>
        </div>
    <script src="../js/vanilla-tilt.js"></script>
    <script>
        // vanilla-tilt.js是一个平滑的3D倾斜JS库,具体参数配置度娘可查到        VanillaTilt.init(document.querySelectorAll(".card"),{            max: 15,  //最大倾斜度数            speed: 400,  //倾斜转换的速度            glare: true,  //是否开启眩光效果            "max-glare": 1  //最大眩光的不透明度        })    
    </script>
    </body>
</html>

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »