【JS】公共鼠标滚动事件(从下进入,从上进入),html 版本

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>Document</title>
	</head>
	<style>
		* {
			margin: 0;
			padding: 0;
		}
		:root {
			--c1-Height: 308vh;
			--c2-Height: 230vh;
		}

		html,
		body {
			width: 100%;
			height: 100%;
			box-sizing: border-box;
		}
		body {
			overflow-y: auto;
		}
		.box {
			width: 100%;
			height: 120%;
			background-color: #b1ffb1;
		}
		.container {
			top: 0;
			width: 100%;
			position: relative;
			background-color: #4787e7b9;
			height: calc(var(--c1-Height) + var(--c2-Height));
		}

		.c1 {
			left: 0;
			width: 50%;
			position: absolute;
			height: var(--c1-Height);
			background-color: #cf7bcf;
		}
		.c2 {
			right: 0;
			width: 50%;
			position: absolute;
			height: var(--c2-Height);
			background-color: #cfad7b;
		}

		.flooter {
			width: 100%;
			height: 130vh;
			overflow: hidden;
			background-color: black;
		}
	</style>
	<body>
		<div class="box"></div>

		<div class="container">
			<div class="c1">
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
				<div>11111111111111111</div>
			</div>
			<div class="c2">
				<div>55555555555555555555555</div>
				<div>55555555555555555555555</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
				<div>22222222222222222222222</div>
			</div>
		</div>

		<div class="flooter"></div>
	</body>

	<script>
		const con = document.querySelector(".container");
		const c1 = document.querySelector(".c1");
		const c2 = document.querySelector(".c2");

        c1.style.top = 0;
        con.style.height = con.offsetHeight - window.innerHeight + 20 + "px";

        /** 从下进入 */
        const enterFromBelow = () => {
            /** 判断父元素距离顶部可视窗口的距离是否小于等于0 */
            if(con.getBoundingClientRect().top <= 0 && con.getBoundingClientRect().top >= -500) {
                c2.style.top = 0;
                c2.style.position = "fixed";
            };

            let bottom = window.innerHeight - c1.getBoundingClientRect().bottom;
            /** 判断左侧元素距离底部可视窗口的距离是否大于等于0 */
            if(bottom >= 0 && bottom < 500) {
                c1.style.top = '';
                c1.style.bottom = 0;
                c1.style.position = "fixed";
                c2.style.position = "absolute";
                c2.style.top = c1.offsetHeight - window.innerHeight + 20 + "px";
            };

            let pb = window.innerHeight - con.getBoundingClientRect().bottom;
            /** 判断父元素距离底部可视区域的距离 */
            if(pb >= 0 && pb < 500) {
                c1.style.position = "absolute";
            };
        };
        /** 从上进入 */
        const enterFromAbove = () => {
            /** 判断父元素距离顶部可视窗口的距离是否大于等于0 */
            if(con.getBoundingClientRect().bottom >= 0 && con.getBoundingClientRect().bottom < 500) {
                c1.style.top = '';
                c1.style.bottom = 0;
                c1.style.position = "absolute";
                c2.style.top = '';
                c2.style.bottom = 0;
                c2.style.position = "absolute";
            };

            let pb = window.innerHeight - con.getBoundingClientRect().bottom;
            /** 判断父元素距离底部可视区域的距离 */
            if(pb <= 0 && pb > -500) {
                c1.style.top = "";
                c1.style.bottom = 0;
                c1.style.position = "fixed";
            };

            let top = c2.getBoundingClientRect().top;
            /** 判断右侧元素距离顶部可视窗口的距离是否大于等于0 */
            if(top >= 0 && top < 500) {
                c1.style.top = 0;
                c1.style.bottom = '';
                c1.style.position = "absolute";
                c2.style.top = 0;
                c2.style.position = "fixed";
            };
            /** 判断父元素距离顶部可视窗口的距离是否小于等于0 */
            let ct = con.getBoundingClientRect().top;
            if(ct >= 0 && ct < 500) {
                c2.style.top = 0;
                c2.style.position = "absolute";
            };
        }

        /** 进入方式 0:未进入 1:进入 2:离开 */
        let way = 0;
		/** 定义滚动条滚动方向 */
		let scrollType = true;
		/** 获取初始滚动位置 */
		let scrollPos = window.pageYOffset || document.documentElement.scrollTop;
		/** 监听滚动条事件 */
		window.addEventListener("scroll", function (e) {
			/** 确定新的滚动位置 */
			const newScrollPos = window.pageYOffset || document.documentElement.scrollTop;
			if (newScrollPos > scrollPos) scrollType = true;
			else scrollType = false;
			/** 更新滚动位置 */
			scrollPos = newScrollPos;
            /** 不同方向进入可视区域 */
            if(scrollType && way != 0) way == 1 ? enterFromBelow() : '离开事件';
            else if(!scrollType && way != 0) way == 1 ? enterFromAbove() : '离开事件';
		});

		/** 观察元素 */
		const callback = (entries) => entries.forEach((entry) => entry.isIntersecting ? way = 1 : way = 2);

		/** 创建一个观察者 */
		const observer = new IntersectionObserver(callback, {
			threshold: 0,
		});
        observer.observe(document.querySelector(".container"));
	</script>
</html>

左右分屏滚动

> vue 版本

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端小袁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值