页面两侧浮窗

需求:

1、页面滚动到距离顶部一定距离时,侧边浮窗从侧边出现

2、点击关闭按钮,再向上滑动,侧边浮窗不再出现,直到回到页面顶部

简单代码示例如下:

##HTML

<!-- 右侧浮窗 -->

    <div class="Right">

        <div class="right">

            右侧浮窗

        </div>

        <div class="close">

            X

        </div>

    </div>

##CSS


/* 右侧浮窗 */
.Right {
    height: 150px;
    width: 120px;
    position: fixed;
    top: 50%;
    right: -120px;
    margin-top: -75px;
    font-size: 18px;
    text-align: center;
    background-color: pink;
    transition: 1.0s;
}

.right {
    line-height: 150px;
}

.close {
    height: 25px;
    line-height: 25px;
    width: 25px;
    position: absolute;
    top: 0;
    right: 0;
    cursor: pointer;
    transition: .5s;
}

/* 旋转 */
.close:hover {
    transform: rotate(180deg);
}

##JS

window.onload = function () {

    //动画函数x
    function animateX(floatSelector, distance) {
        const floatElement = document.querySelector(floatSelector);
        floatElement.style.transform = 'translateX(' + distance + 'px)';
    }
    //使用Isclose来标记
    let Isclose = false;
    // 监听页面滚动事件
    window.addEventListener('scroll', function () {
        // 获取页面滚动距离
        const scrollTop = window.scrollY || document.documentElement.scrollTop;
        // 判断滚动距离是否达到触发条件
        if (scrollTop >= 200 && !Isclose) {
            animateX('.Right', -120);
        }
        else {
            animateX('.Right', 120);
        }
    });

    //监听页面滚动到顶部事件
    window.addEventListener("scroll", function () {
        // 获取页面滚动距离
        const scrollTop = window.scrollY || document.documentElement.scrollTop;
        if (scrollTop == 0) {
            Isclose = false;
        }
    })

    //关闭侧边浮窗
    const close = document.querySelector(".close");
    close.addEventListener('click', function () {
        animateX(".Right", 120);
        Isclose = true;
    })

}

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值