欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue中嵌入可浮動(dòng)的第三方網(wǎng)頁(yè)窗口的示例詳解

 更新時(shí)間:2025年02月06日 11:43:21   作者:碼農(nóng)研究僧  
本文介紹了在Vue2項(xiàng)目中嵌入可浮動(dòng)的第三方網(wǎng)頁(yè)窗口的實(shí)現(xiàn)方法,包括使用iframe、div+script和dialog元素等方式,并提供了一個(gè)實(shí)戰(zhàn)Demo,展示了如何在Vue組件中動(dòng)態(tài)加載和控制浮窗的顯示,需要的朋友可以參考下

1. 思路Demo

以下Demo提供思路參考,需要結(jié)合實(shí)際自身應(yīng)用代碼

下述URL的鏈接使用百度替代!

方式 1:使用 iframe 浮窗

可以創(chuàng)建一個(gè)固定在頁(yè)面右下角的 iframe,讓它加載該 script 生成的內(nèi)容

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>浮窗嵌入</title>
    <style>
        /* 浮窗樣式 */
        #floating-window {
            position: fixed;
            bottom: 20px;
            right: 20px;
            width: 400px;
            height: 500px;
            border: none;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
            background: white;
            z-index: 9999;
            border-radius: 10px;
        }
        /* 關(guān)閉按鈕 */
        #close-btn {
            position: absolute;
            top: 5px;
            right: 5px;
            background: red;
            color: white;
            border: none;
            width: 25px;
            height: 25px;
            border-radius: 50%;
            cursor: pointer;
            font-size: 14px;
            line-height: 25px;
            text-align: center;
        }
    </style>
</head>
<body>

    <!-- 按鈕觸發(fā)浮窗 -->
    <button id="open-float">打開(kāi)浮窗</button>

    <!-- 浮窗框架 -->
    <div id="floating-container" style="display: none;">
        <button id="close-btn">×</button>
        <iframe id="floating-window" src="https://www.baidu.com/"></iframe>
    </div>

    <script>
        document.getElementById("open-float").addEventListener("click", function() {
            document.getElementById("floating-container").style.display = "block";
        });

        document.getElementById("close-btn").addEventListener("click", function() {
            document.getElementById("floating-container").style.display = "none";
        });
    </script>

</body>
</html>

方式 2:使用 div + script 動(dòng)態(tài)加載

script 代碼是動(dòng)態(tài)生成 HTML 的,可以創(chuàng)建一個(gè)浮窗 div,然后在 div 里動(dòng)態(tài)插入 script

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>浮窗嵌入 Script</title>
    <style>
        #floating-div {
            position: fixed;
            bottom: 20px;
            right: 20px;
            width: 400px;
            height: 500px;
            border: 1px solid #ccc;
            background: white;
            z-index: 9999;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
            display: none;
            border-radius: 10px;
        }
        #close-div-btn {
            position: absolute;
            top: 5px;
            right: 5px;
            background: red;
            color: white;
            border: none;
            width: 25px;
            height: 25px;
            border-radius: 50%;
            cursor: pointer;
            font-size: 14px;
            line-height: 25px;
            text-align: center;
        }
    </style>
</head>
<body>

    <!-- 按鈕觸發(fā)浮窗 -->
    <button id="show-div-btn">打開(kāi)浮窗</button>

    <!-- 浮窗內(nèi)容 -->
    <div id="floating-div">
        <button id="close-div-btn">×</button>
        <div id="script-content"></div>
    </div>

    <script>
        document.getElementById("show-div-btn").addEventListener("click", function() {
            document.getElementById("floating-div").style.display = "block";
            let script = document.createElement("script");
            script.async = true;
            script.defer = true;
            script.src = "https://www.baidu.com/";
            document.getElementById("script-content").appendChild(script);
        });

        document.getElementById("close-div-btn").addEventListener("click", function() {
            document.getElementById("floating-div").style.display = "none";
        });
    </script>

</body>
</html>

方式 3:使用 dialog 元素

想要更現(xiàn)代化的 UI,可以使用 <dialog> 標(biāo)簽

<dialog id="floating-dialog">
    <button onclick="document.getElementById('floating-dialog').close()">關(guān)閉</button>
    <iframe src="https://www.baidu.com/"></iframe>
</dialog>

<button onclick="document.getElementById('floating-dialog').showModal()">打開(kāi)浮窗</button>

2. 實(shí)戰(zhàn)Demo

下述實(shí)戰(zhàn)代碼為Vue2(項(xiàng)目源自bladex)

初始:

集成之后:

在 avue-top 組件里嵌入一個(gè)浮窗 div,然后動(dòng)態(tài)加載 script,確保它能夠嵌入 Vue 組件中

<template>
  <div class="avue-top">

    <div class="top-bar__right">
      <el-tooltip effect="dark" content="打開(kāi)浮窗" placement="bottom">
        <div class="top-bar__item" @click="toggleFloatingWindow">
          <i class="el-icon-monitor"></i> <!-- 你可以換成任意圖標(biāo) -->
        </div>
      </el-tooltip>
    </div>

    <!-- 浮窗 -->
    <div v-if="showFloatingWindow" class="floating-window">
      <button class="close-btn" @click="showFloatingWindow = false">×</button>
      <iframe :src="floatingUrl"></iframe>
    </div>
  </div>
  
</template>

在 methods 里添加 toggleFloatingWindow 方法,控制浮窗的顯示:

<script>
export default {
  data() {
    return {
      showFloatingWindow: false,
      floatingUrl: "http://xxxxx"
    };
  },
  methods: {
    toggleFloatingWindow() {
      this.showFloatingWindow = !this.showFloatingWindow;
    }
  }
};
</script>

添加 <style> 樣式

<style lang="scss" scoped>
.floating-window {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 400px;
  height: 500px;
  background: white;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  z-index: 9999;
  border-radius: 10px;
  border: 1px solid #ddd;
  overflow: hidden;
}

.floating-window iframe {
  width: 100%;
  height: 100%;
  border: none;
}

.close-btn {
  position: absolute;
  top: 5px;
  right: 5px;
  background: red;
  color: white;
  border: none;
  width: 25px;
  height: 25px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 14px;
  line-height: 25px;
  text-align: center;
}
</style>

但這樣會(huì)有個(gè)bug,每次點(diǎn)開(kāi)隱藏都會(huì)刷新下網(wǎng)頁(yè)

優(yōu)化浮窗:防止重復(fù)加載內(nèi)容

可以使用 v-show 而不是 v-if,這樣 iframe 只會(huì)被隱藏,而不會(huì)被銷毀,內(nèi)容不會(huì)丟失

<div v-show="showFloatingWindow" class="floating-window">
  <button class="close-btn" @click="showFloatingWindow = false">×</button>
  <iframe ref="floatingIframe" :src="floatingUrl"></iframe>
</div>

添加樣式

.floating-text {
  font-size: 15px;  /* 調(diào)整字體大小 */
  margin-left: 5px; /* 控制與圖標(biāo)的間距 */
  color: #fff;      /* 文字顏色 */
}

到此這篇關(guān)于Vue中嵌入可浮動(dòng)的第三方網(wǎng)頁(yè)窗口的示例詳解的文章就介紹到這了,更多相關(guān)Vue嵌入第三方網(wǎng)頁(yè)窗口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論