vue3使用iframe全屏展示pdf效果

最近的一个功能是编写一个pdf展示的组件,外部传入pdf的链接,根据这个功能,可以使用iframe去展示pdf,目前主要是将预览pdf的样式调整成与屏幕宽高一样

1.pdf使用iframe

2.使用translate将这个组件,使用to属性将目标元素传到body中(to就是目标元素。 接收一个 to prop 来指定传送的目标。

3.显示,隐藏的动画效果可以使用transition组件对iframe进行包裹

具体代码如下:

javascript 复制代码
<template>
  <!-- 使用 teleport 将遮罩层和 iframe 渲染到 body 中 -->
  <teleport to="body">
    <!-- 遮罩层动画 -->
    <transition name="fade">
      <div v-if="showOverlay" class="overlay">
        <!-- 取消按钮 -->
        <button class="close-button" @click="closeOverlay">取消</button>
      </div>
    </transition>

    <!-- iframe 动画 -->
    <transition name="scale">
      <iframe
        v-if="showOverlay && pdfUrl"
        :src="pdfUrl"
        width="100%"
        height="100%"
        frameborder="0"
        class="pdf-iframe"
      ></iframe>
    </transition>
  </teleport>
</template>

<script setup>
import { ref, watch } from 'vue';

// 定义 props
const props = defineProps({
  pdfUrl: {
    type: String,
    required: true,
  },
  show: {
    type: Boolean,
    required: true,
  },
});

// 定义 emits
const emit = defineEmits(['close']);

// 响应式数据
const showOverlay = ref(props.show);

// 监听 props.show 的变化
watch(
  () => props.show,
  (newVal) => {
    showOverlay.value = newVal;
  }
);

// 关闭遮罩的方法
const closeOverlay = () => {
  showOverlay.value = false;
  emit('close'); // 通知父组件关闭
};
</script>

<style scoped>
/* 遮罩层样式 */
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5); /* 半透明黑色遮罩 */
  z-index: 1000; /* 确保遮罩层在最上层 */
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 取消按钮样式 */
.close-button {
  position: absolute;
  top: 20px;
  right: 20px;
  padding: 10px 20px;
  background-color: #ff4d4f; /* 红色背景 */
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
}

.close-button:hover {
  background-color: #ff7875; /* 鼠标悬停时的颜色 */
}

/* iframe 样式 */
.pdf-iframe {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 80%;
  height: 80%;
  z-index: 1001; /* iframe 在遮罩层之上 */
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

/* 遮罩层淡入淡出动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* iframe 缩放动画 */
.scale-enter-active,
.scale-leave-active {
  transition: all 0.3s ease;
}

.scale-enter-from,
.scale-leave-to {
  opacity: 0;
  transform: translate(-50%, -50%) scale(0.8);
}
</style>

如果在父组件使用,如下代码

javascript 复制代码
<template>
  <div>
    <!-- 打开 PDF 的按钮 -->
    <button @click="openPdf">打开 PDF</button>

    <!-- 使用 PdfViewer 组件 -->
    <PdfViewer
      :pdfUrl="pdfUrl"
      :show="showPdfViewer"
      @close="closePdfViewer"
    />
  </div>
</template>

<script setup>
import { ref } from 'vue';
import PdfViewer from './components/PdfViewer.vue'; // 引入 PdfViewer 组件

// 定义响应式数据
const pdfUrl = ref('');
const showPdfViewer = ref(false);

// 打开 PDF 的方法
const openPdf = () => {
  pdfUrl.value = '/path/to/your/pdf/file.pdf'; // 可以是本地或远程 PDF 文件路径
  showPdfViewer.value = true;
};

// 关闭 PDF 的方法
const closePdfViewer = () => {
  showPdfViewer.value = false;
  pdfUrl.value = '';
};
</script>

<style>
/* 父组件的样式 */
</style>
相关推荐
LaughingZhu6 小时前
Product Hunt 每日热榜 | 2026-05-21
前端·人工智能·经验分享·chatgpt·html
怕浪猫7 小时前
Electron 开发实战(一):从零入门核心基础与环境搭建
前端·electron·ai编程
小鹏linux7 小时前
Ubuntu 22.04 部署开源免费具有精美现代web页面的Casdoor账号管理系统
linux·前端·ubuntu·开源·堡垒机
前端若水8 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
Bigger8 小时前
mini-cc:一个轻量级 AI 编程助手的诞生
前端·ai编程·claude
涵涵(互关)9 小时前
Naive-ui树型选择器只显示根节点
前端·ui·vue
BY组态9 小时前
Ricon组态系统最佳实践:从零开始构建物联网监控平台
前端·物联网·iot·web组态·组态
BY组态9 小时前
Ricon组态系统vs传统组态软件:为什么选择新一代Web组态平台
前端·物联网·iot·web组态·组态
SoaringHeart9 小时前
Flutter进阶:OverlayEntry 插入图层管理器 NOverlayZIndexManager
前端·flutter
放下华子我只抽RuiKe59 小时前
React 从入门到生产(四):自定义 Hook
前端·javascript·人工智能·深度学习·react.js·自然语言处理·前端框架