【HTML】向右侧展开div

<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style>
        .userpic {
            position: relative;
        }

        .userinfo {
            position: absolute;
            z-index: 99;
            left: 0;
            top: 0;
        }
        @keyframes go-left {
            0% {
                /* 背景色 */
                background: #000000;
                /* 距离左侧高度 */
                left: 0;
                /* 距离顶部高度 */
                top: 20px;
                /* 元素的不透明级别 */
                opacity: 0
            }
            100% {
                background: #000000;
                left: calc(50% + 20px);
                top: 20px;
                opacity: 3;
            }
        }

        @keyframes go-right {
            0% {
                background: #000000;
                left: calc(50% + 20px);
                top: 20px;
                opacity: 3
            }

            100% {
                background: #000000;
                left: 0;
                top: 20px;
                opacity: 0;
            }
        }

        /* Safari and Chrome */
        /* @-webkit-keyframes go-left {
            0% {
                background: #000000;
                left: 0;
                top:  calc(10vh + 20px);
                opacity: 0
            }
            100% {
                background: #000000;
                left: calc(50% + 20px);;
                top: calc(10vh + 20px);
                opacity: 3;
            }
        }

        @-webkit-keyframes go-right {
            0% {
                background: #000000;
                left: calc(50% + 20px);
                top: calc(10vh + 20px);
                opacity: 0
            }
            100% {
                background: #000000;
                left: 0;
                top: calc(10vh + 20px);
                opacity: 3;
            }
        } */

        .go_left {
            /* 属性为 @keyframes 动画规定名称 */
            animation-name: go-left;
            animation: myfirst 1s normal;
            animation-direction: normal;
            animation-fill-mode: forwards;
            /* Safari and Chrome */
            -webkit-animation-name: go-left;
            -webkit-animation: go-left 3s normal;
            -webkit-animation-direction: normal;
            -webkit-animation-fill-mode: forwards;
        }

        .go_right {
            pointer-events: none;
            animation-name: go-right;
            animation: myfirst 3s normal;
            animation-direction: normal;
            animation-fill-mode: forwards;
            -webkit-animation-name: go-right;
            -webkit-animation: go-right 3s normal;
            -webkit-animation-direction: normal;
            -webkit-animation-fill-mode: forwards;
        }

        .go_display {
            pointer-events: none;
            animation-name: go-right;
            animation: myfirst 3s normal;
            animation-direction: normal;
            animation-fill-mode: forwards;
            -webkit-animation-name: go-right;
            -webkit-animation: go-right 1s normal;
            -webkit-animation-direction: normal;
            -webkit-animation-fill-mode: forwards;
        }
    </style>
</head>

<body>
    <!-- <button id="btn" οnclick="fadeIn()" style="height: calc(10%);width: 10vw;">显示更多</button> -->

    <div style="width: calc(100%-40px); height: calc(100%-40px);margin: 20px;background-color: #0050fc;display: flex;">
        <div class="userpic" style="width: 50vw; height: 50vh; background-color: #ff4a0d;" >
            <button id="btn" onclick="fadeIn()" style="height: calc(10%);width: 10vw;">显示更多</button>
        </div>
        <div class="userinfo sidebar go_display" style="width: 30vw; height: 50vh;" ></div>
    </div>

<script>
    function fadeIn() {
        if (document.querySelector(".sidebar").className.indexOf('go_left') < 0) {
            document.querySelector(".sidebar").className = 'userinfo sidebar';
            document.querySelector(".sidebar").classList.add('go_left');
            document.getElementById("btn").innerText = '收起'
        } else {
            document.querySelector(".sidebar").className = 'userinfo sidebar';
            document.querySelector(".sidebar").classList.add('go_right');
            document.getElementById("btn").innerText = '显示更多'
        }

    }
</script>
</body>

</html>

注意

偶尔可能会遇到动画会把按钮挡住,导致按钮不可以点击,就要添加这个属性 pointer-events

外层(被遮挡的层){
    pointer-events: none;
}
 
内层(要发生事件的层){
    pointer-events: auto;
}

pointer-events属性本身有很多取值,但**只有****noneauto**可以用在浏览器上,其他都只能应用在SVG上

取值none:元素永远不会成为鼠标事件的target

取值auto:与pointer-events属性未指定时的表现效果相同(即将元素恢复成为鼠标事件的target

这里不要忘了给内层添加**auto属性,否则被外层包裹的所有内层无法成为鼠标事件的target**

详解链接

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值