VUE iframe标签预览HTML链接增加监听滚到底部事件

 需求是: 有个在服务器上的html文件,希望在vue项目的页面里去预览该文件内容,并且监听查看内容完毕后(即内容滚到底部后)再进行一些操作

先附上已成功的代码

<iframe :src="cont" class="f-cont" ref="myIframe"></iframe>
 window.addEventListener('message', (event) => {
      if (event.data === 'scrollToBottom') {
          console.log('已经滑动到底部');
      }
    }, false);

在html文件里增加代码

window.addEventListener('scroll', function() {
  const isBottom = window.innerHeight + window.pageYOffset >= document.body.offsetHeight;
  if (isBottom) {
    parent.postMessage('scrollToBottom', '*');
  }
});

 这是分隔线 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


 拿到该需求,就在思考,必须得拿到iframe里的DOM结构,所以,开始敲代码,如下

// iframe 加载完成后才能获取iframe 内部的 DOM 元素
    handleIframeLoad() {
      // 获取 iframe dom 对象
      const iframe = this.$refs.myIframe;
      const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
      // 监听 iframe 内容滚动事件
      iframeDocument.addEventListener('scroll', this.handleIframeScroll);
    },
    // iframe 内容滚动事件回调
    handleIframeScroll(e) {
      const iframe = this.$refs.myIframe;
      const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;

      const isScrollToBottom = ((iframeDocument.documentElement.clientHeight + iframeDocument.documentElement.scrollTop) - iframeDocument.documentElement.scrollHeight) >= 0;
      if (isScrollToBottom) {
        console.log('已滚动到底')
      }
    },

发测试站,手机测试,🍎手机ok,安卓手机问题多多,所以,开始在网上百度兼容安卓手机的方法,找到
该方法: 在该示例代码中,我们使用 MutationObserver 监听 iframe 内容变化:通过 MutationObserver 可以监控 DOM 元素的变化,从而实现监听 iframe 内容滚动事件的效果。

<template>
  <div>
    <iframe ref="myIframe" src="./path/to/your/iframe.html"></iframe>
  </div>
</template>

<script>
export default {
  methods: {
    handleIframeLoad() {
      const iframe = this.$refs.myIframe;
      const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;

      const observer = new MutationObserver((mutations) => {
        const iframeScroll = iframeDocument.scrollingElement || iframeDocument.body;
        const htmlScroll = document.documentElement;

        const iframeIsBottom = iframeScroll.scrollHeight - iframeScroll.scrollTop - iframeScroll.clientHeight <= 1;
        const htmlIsBottom = htmlScroll.scrollHeight - htmlScroll.scrollTop - window.innerHeight <= 1;

        if (iframeIsBottom || htmlIsBottom) {
          console.log('已经滑动到底部');
        }
      });

      observer.observe(iframeDocument.documentElement, { attributes: true, childList: true, subtree: true });
    }
  },
  mounted() {
    const iframe = this.$refs.myIframe;
    iframe.addEventListener('load', this.handleIframeLoad);
  }
}
</script>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
`vue3-seamless-scroll`是一个用于Vue 3应用中的无限滚动库,它提供了一种便捷的方式来处理滚动事件,特别是在数据加载和滚动到底部加载更多场景中。当你想要监听页面滚动到底部,以便自动加载更多数据,你可以使用`vue3-seamless-scroll`提供的`scroll-event`或`scroll-position`指令。 以下是使用`vue3-seamless-scroll`监听数据滚动到底部的基本步骤: 1. 安装`vue3-seamless-scroll`库: ```bash npm install vue3-seamless-scroll ``` 2. 在你的Vue组件模板中,将滚动容器包裹在`<seamless-scroll>`组件中,并添加`v-loading`和`v-scroll-end`指令: ```html <template> <div class="scroll-container"> <seamless-scroll :data="items" ref="scrollRef"> <div v-for="(item, index) in items" :key="index">{{ item }}</div> </seamless-scroll> <button @click="loadMore" v-if="loading">加载更多</button> </div> </template> ``` 3. 在组件的script部分,设置数据源`items`,并初始化`scrollRef`和`loading`状态: ```javascript import { ref, onMounted, emit } from 'vue'; export default { data() { return { items: [], // 假设这是你的数据数组 loading: false, lastItem: null, // 记录最后渲染的元素 }; }, setup() { const scrollRef = ref(null); onMounted(() => { // 首次加载所有数据 this.items = ...; // 根据实际情况填充数据 // 当滚动结束时,检查是否加载了更多数据 scrollRef.value.addEventListener('scroll-end', () => { if (scrollRef.value.isBottom()) { this.loadMore(); } }); }); // 加载更多数据的方法 function loadMore() { this.loading = true; // 加载更多数据的API请求,完成后更新items并清空loading标志 fetchMoreData().then((newItems) => { this.items = [...this.items, ...newItems]; this.lastItem = newItems[newItems.length - 1]; // 更新lastItem this.loading = false; }); } return { scrollRef, loadMore }; }, }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黎轩栀海

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

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

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

打赏作者

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

抵扣说明:

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

余额充值