关于iframe或document监听滑动(scroll)失败的问题

出现了这个问题我们总是把时间花在分析为什么iframe这个element不能滑动?该怎么设置?

其实iframe监听滑动如果出了问题,首先该分析的是里面的document到底能不能滑动.

--------------------------------------------------------------------------------------------------------------------------

iframe监听滑动本身其实很简单,只需要iframe的contentWindow增加一个scroll事件的监听即可.

window.addEventListener('scroll', function(e) {
  console.log('scrollllllllll')
});

	<body>
		<p>两个iframe互相触发scroll,形成循环(可以打开控制台,看当前触发scroll的iframe),</p>
		<p>(注:safaric上效果不兼容)</p>
		<div class='iframe_container' id='iframe1_c'>
			<iframe id='iframe1' src='a.html'></iframe>
		</div>

		<div class='iframe_container' id='iframe2_c'>
			<iframe id='iframe2' src='a.html'></iframe>
		</div>

		<script src='/js/jquery-1.9.0.min.js'></script>
		<script>
			$(function() {
				var iframe1Window,iframe2Window;
				$('iframe').on('load', function() {
					var contentWindow = $(this)[0].contentWindow,
						_id = $(this).attr('id');
					if(_id == 'iframe1' && !iframe1Window) {
						iframe1Window = contentWindow;
					}
					if(_id == 'iframe2' && !iframe2Window) {
						iframe2Window = contentWindow;
					}

					contentWindow.onscroll = function(e) {
						var scrollTop = $(contentWindow).scrollTop();


						console.log(_id);
						if(_id == 'iframe1') {
							if(iframe2Window) {
								$(iframe2Window).scrollTop(scrollTop);

							}
						} else if(_id == 'iframe2') {
							if(iframe1Window) {
								$(iframe1Window).scrollTop(scrollTop);

							}
						}
					};

				})
			})
		</script>
	</body>

或者iframe.contentDocument增加一个监听事件

 可是为什么我的同源协议没为题,可还是监听无法奏效呢?

原因很可能就在于你的那个网页不经过iframe,直接给window或document增加scroll监听都不能用.

不信你测试一下

document.addEventListener('scroll', function(e) {
  console.log('scrollllllllll_doc')
});

完后滑动,果然没有输出.

为啥有的document不能监听滑动?

答案,有的document.scrollingElement本身就不可滑动(你滑动的是里面的孩子,而不document.scrollingElement本身,document.scrollingElement本身没有溢出问题,他的孩子有溢出问题,他的孩子产生的scrollbar)

document.scrollingElement的解释

The scrollingElement read-only property of the Document interface returns a reference to the Element that scrolls the document. In standards mode, this is the root element of the document, document.documentElement.

用js代码测试元素是否可滑动:

const isScrollable = function (ele) {
    // Compare the height to see if the element has scrollable content
    const hasScrollableContent = ele.scrollHeight > ele.clientHeight;

    // It's not enough because the element's `overflow-y` style can be set as
    // * `hidden`
    // * `hidden !important`
    // In those cases, the scrollbar isn't shown
    const overflowYStyle = window.getComputedStyle(ele).overflowY;
    const isOverflowHidden = overflowYStyle.indexOf('hidden') !== -1;

    return hasScrollableContent && !isOverflowHidden;
};

其中scrollHeight

其中clientHeight它是元素的内部高度(以像素为单位)。它包括填充但不包括边框、边距和水平滚动条(如果存在)。

在控制台测试一下,就知道你的文档可不可以滑动了.

 可以

 不可以,但他的孩子page-container可以

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在小程序中,我们可以通过监听 `scroll-view` 组件的 `scrolltolower` 事件来判断页面是否滑动到底部。具体步骤如下: 1. 首先,在 `scroll-view` 组件上绑定 `scrolltolower` 事件,例如:`<scroll-view bind:scrolltolower="onScrollToLower">`。 2. 在页面的 `Page` 中定义 `onScrollToLower` 方法。 3. 在 `onScrollToLower` 方法中,通过获取 `scroll-view` 组件的滚动位置和宽度等信息,判断是否已经滑动到底部。 具体代码如下所示: ```javascript // 在Page的定义中 Page({ onScrollToLower(e) { // 获取scroll-view的相关信息 const scrollView = e.detail.scrollView; const { scrollHeight, scrollTop, clientHeight } = scrollView; // 判断是否滑动到底部 if (scrollTop + clientHeight >= scrollHeight) { console.log("已滑动到底部"); // 在此处可以执行滑动到底部后的相关操作 } } }); ``` 上述代码中,我们首先通过 `e.detail.scrollView` 获取到 `scroll-view` 组件的各项信息,包括滚动高度 `scrollHeight`、滚动距离 `scrollTop` 和可视区域高度 `clientHeight`。 然后,我们判断当前的滚动距离和可视区域高度之和是否大于或等于滚动高度,如果是,则说明已经滑动到底部。 最后,在判断滑动到底部后,你可以执行相应的操作,如加载更多内容或展示相关提示信息等。 这样,当用户滑动到底部时,你就能够通过 `scrolltolower` 事件监听到,并进行相应的处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

子燕若水

吹个大气球

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

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

打赏作者

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

抵扣说明:

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

余额充值