iframe隐藏scrollbar并且还能够继续滚动

查了好久的文档,太累了,目前暂时使用了直接把scrollbar隐藏的策略。太难了。

直接隐藏的策略:
frame 有个属性 scrolling,直接设置 scrolling=‘no’ 即可隐藏scrollbar。

<iframe src="xxx" scrolling="no"></iframe>

另外一个就是外边包一层比iframe窄的div,然后 overflow 设置成隐藏。

<div class="iframe-content">
  <iframe src="xxx"></iframe>
</div>

<style scoped>
.iframe-content {
  position: relative;
  overflow: hidden;
  width: 1200px;
  height: 600px;
}

.iframe-content iframe {
  width: 1200px;
  height: 600px;
  border: none;
  position: absolute; 
  right: -15px; 
  top: 0; 
  bottom: 0;
  overflow-x: hidden; 
  overflow-y: scroll;
}
</style>

不过感觉有可能谁闲的设置过scrollbar的宽度怎么办呢,那就把这个值改成获取的吧。

<div :style="`width:${fwidth-scrollbarWidth-1}px;height:${fheight}px;`" class="overflow-hidden">
      <iframe 
      :style="`width:${fwidth}px;height:${fheight}px;`"
      :src="xxx"></iframe>
</div>
scrollbarWidth.value = inner.contentWindow.innerWidth - inner.contentWindow.document.documentElement.clientWidth

以下是全部代码,基于vue+tailwindcss

<template>
  <div :style="`width:${fwidth-scrollbarWidth-1}px;height:${fheight}px;`" 
      class="overflow-hidden">
    <iframe 
      id="xxxxframe"
      src="http://localhost:5173/"
      :style="`width:${fwidth}px;height:${fheight}px;`"
      class="border-none overflow-x-hidden overflow-y-scroll shadow"
    ></iframe>
  </div>
</template>
<script setup lang="ts">
import { Ref, onMounted, ref } from 'vue';

const fwidth: Ref<number> = ref(1200)
const fheight: Ref<number> = ref(600)
const scrollbarWidth: Ref<number> = ref(0)

onMounted(() => {
  console.log('content scripts onMounted')
  let inner = document.getElementById('xxxxframe') as HTMLIFrameElement

  inner.onload = () => {
    scrollbarWidth.value = inner.contentWindow.innerWidth - inner.contentWindow.document.documentElement.clientWidth
  }
})
</script>

目前感觉这个办法还算是效果比较好。

相关推荐

  1. iframe隐藏scrollbar并且能够继续滚动

    2024-05-03 06:02:04       33 阅读
  2. css隐藏溢出隐藏滚动

    2024-05-03 06:02:04       35 阅读
  3. 最佳解决Css一隐藏滚动

    2024-05-03 06:02:04       50 阅读
  4. vue2隐藏浏览器右侧滚动

    2024-05-03 06:02:04       33 阅读
  5. 如何隐藏一个元素的滚动

    2024-05-03 06:02:04       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-03 06:02:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-03 06:02:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-03 06:02:04       82 阅读
  4. Python语言-面向对象

    2024-05-03 06:02:04       91 阅读

热门阅读

  1. 【Python】异常

    2024-05-03 06:02:04       29 阅读
  2. GESP一级 - 第三章 - 第1节 - 标准输入输出

    2024-05-03 06:02:04       25 阅读
  3. 目标跟踪难点及算法介绍

    2024-05-03 06:02:04       32 阅读
  4. 数据结构与算法——栈和队列

    2024-05-03 06:02:04       32 阅读
  5. 图文、视频处理等自媒体工具

    2024-05-03 06:02:04       27 阅读
  6. centos部署前后端项目

    2024-05-03 06:02:04       30 阅读
  7. Docker 虚拟机 WSL

    2024-05-03 06:02:04       35 阅读
  8. Android 当存在双卡时,移动网络默认为SIM卡1

    2024-05-03 06:02:04       38 阅读
  9. 用智慧树理解spring原理

    2024-05-03 06:02:04       30 阅读