使用html+css+js实现页面分割效果(学习 L01)

学习分割查看

效果如图 ---html

<!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>
        /* 全局变量 */
        :root {
            --container-bg-color: #333;
            --left-bg-color: rgba(223, 39, 39, 0.7);
            --right-bg-color: rgba(43, 35, 35, 0.7);
            --left-button-hover-color: rgba(161, 11, 11, 0.3);
            --right-button-hover-color: rgba(92, 92, 92, 0.3);
            --hover-width: 75%;
            --other-width: 25%;
            --speed: 1000ms;
            --button-hover-border-color: rgba(236, 222, 14, 0.7);
        }

        html,
        body {
            margin: 0;
            padding: 0;
            height: 100%;
            width: 100%;
        }

        .container {
            position: relative;
            width: 100%;
            height: 100%;
            background-color: var(--container-bg-color);
        }

        .split {
            position: absolute;
            width: 50%;
            height: 100%;
            overflow: hidden;
        }

        .split.left {
            left: 0;
            background: url("./img/designer.jpg") center center no-repeat;
            background-size: cover;
        }

        .split.left::before {
            position: absolute;
            content: "";
            width: 100%;
            height: 100%;
            background: var(--left-bg-color);
        }

        .split.right {
            right: 0;
            background: url("./img/programmer.png") center center no-repeat;
            background-size: cover;
        }

        .split.right::before {
            position: absolute;
            content: "";
            width: 100%;
            height: 100%;
            background: var(--right-bg-color);
        }

        h1 {
            font-size: 4rem;
            color: #fff;
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
            white-space: nowrap;
            top: 20%;
        }

        .button {
            position: absolute;
            display: block;
            font-weight: bold;
            text-decoration: none;
            border: #fff solid 0.2rem;
            color: #fff;
            left: 50%;
            transform: translateX(-50%);
            /* 左移-50% */
            top: 40%;
            text-align: center;
            padding-top: 1.3rem;
            font-size: 1rem;
            height: 2.5rem;
            width: 15rem;
            text-transform: uppercase;
            /* 大写 */
        }

        .split.left .button:hover {
            background-color: var(--left-button-hover-color);
            border-color: var(--left-button-hover-color);
        }

        .split.right .button:hover {
            background-color: var(--right-button-hover-color);
            border-color: var(--right-button-hover-color);
        }


        /* hover-right and hover-left */
        .hover-left .left {
            width: var(--hover-width);
        }

        .hover-left .right {
            width: var(--other-width);
        }
        .hover-left .right::before {
            z-index: 2;
        }

        .hover-right .right {
            width: var(--hover-width);
        }

        .hover-right .left {
            width: var(--other-width);
        }
        .hover-right .left::before {
            z-index: 2;
        }

        /* 过度动画效果 */

        .split.left, .split.right, .split.right::before, .split.left::before {
            transition: 1000ms all ease-in;
        }

        @media(max-width:800){
            h1{
                font-size: 2rem;
            }
            .button{
               width: 12rem; 
            }
        }
    </style>
</head>

<body>


    <div class="container">
        <div class="split left">
            <h1>The Designer</h1>
            <a href="#" class="button">read more</a>
        </div>
        <div class="split right">
            <h1>The Programmer</h1>
            <a href="#" class="button">read more</a>
        </div>

    </div>



    <script>
        const left = document.querySelector('.left'); // 获取对象
        const right = document.querySelector('.right');
        const container = document.querySelector('.container');


        left.addEventListener("mouseenter", () => { // 添加鼠标进入时间
            container.classList.add("hover-left"); // 添加样式
        });

        left.addEventListener("mouseleave", () => {
            container.classList.remove("hover-left"); // 删除样式
        });

        right.addEventListener("mouseenter", () => {
            container.classList.add("hover-right");
        });

        right.addEventListener("mouseleave", () => {
            container.classList.remove("hover-right");
        });


    </script>
</body>

</html>
````复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值