首页 > 其他分享 >Vue中优雅的更改iframe嵌入页面的样式

Vue中优雅的更改iframe嵌入页面的样式

时间:2022-12-04 16:45:42浏览次数:50  
标签:box Vue const linkTag link iframe css 页面

通过外部引入css文件来控制嵌入页面的样式

公共iframe组件封装

传入属性:

嵌入页面路径
css文件名称(默认放在/static/css/下),默认css文件名可以自己定义,在确定嵌入页面不多,相互之间的css属性不会冲突的情况下,可以使用一个css文件

<template>
    <div class="wrapper-c">
        <div style="height: 100%" v-bkloading="{ isLoading: loading, opacity: 1, zIndex: 10 }">
            <iframe id="fram_box"
                @load="loadFrame"
                :src="url"
            >
            </iframe>
        </div>
    </div>
</template>

<script>
    export default {
        name: 'common-iframe',
        props: {
            url: String,
            link: {
                type: String,
                default: 'frame_reset'
            }
        },
        data() {
            return {
                loading: true
            }
        },
        mounted() {
            const close = this.closeLoading
            $('#fram_box').load(() => {
                close()
            })
        },
        methods: {
            closeLoading() {
                this.loading = false
            },
            // loadFrame函数是iframe加载完成后回调函数
            loadFrame() {
                // 获取iframe节点
                const iframeBox = document.getElementById('fram_box')
                // 获取iframe html文件
                const doc = iframeBox.contentWindow.document
                // 获取iframe html文件head
                const head = doc.getElementsByTagName('head')
                // 新建link标签
                const linkTag = document.createElement('link')
                // 设置link标签id
                linkTag.id = 'newstyle'
                // link标签引入css文件的地址 ;window.STATIC_URL 是全局变量,指向静态资源目录,需要自己指定
                linkTag.href = `${window.STATIC_URL}css/${this.link}.css`
                // 设置link标签属性
                linkTag.setAttribute('rel', 'stylesheet')
                // 设置link标签属性
                linkTag.setAttribute('type', 'text/css')
                // 将link标签添加进iframe html文件的head里
                head[0].append(linkTag)
            }
        }
    }
</script>

<style lang="scss" scoped>
.wrapper-c {
    width: calc(100%);
    height: 100%;
    margin-left: 0;

    #fram_box {
        height: 100%;
        width: 100%;
        border-width: 0px;
    }
}
</style>

父组件使用

<template>
    <div id="wraper-p">
        <common-iframe :url="infoUrl" :link="'health_adviser'"></common-iframe>
    </div>
</template>

<script>
    import commonIframe from '../components/commonIframe'

    export default {
        name: 'overview',
        components: {
            commonIframe 
        },
        data() {
            return {
                infoUrl: '【页面路径】'
            }
        }
    }
</script>

<style lang="scss" scoped>
#wraper-p{
    width: 100%;
    height: calc(100vh - 92px);
    box-sizing: border-box;
    overflow: hidden;
}
</style>

参考:
https://blog.csdn.net/sinat_32546159/article/details/119738108
https://juejin.cn/post/6844904180792967182

标签:box,Vue,const,linkTag,link,iframe,css,页面
From: https://www.cnblogs.com/lhongsen/p/16950106.html

相关文章

  • 【vue-router 4.x】使用addRoute加载动态路由时,刷新页面后出现空白页和控制台报错 [Vu
    "vue-router":"^4.1.6"遇到的问题动态路由刷新后,出现空白页动态路由刷新后,控制报错[VueRouterwarn]:Nomatchfoundforlocationwithpath"/***/index"1.动态......
  • vue中父元素动态获取iframe高度
    背景:在vue开发中(不论是vue2还是vue3),我在页面中打印都可以拿到document.getElementById('iframe')的DOM节点,但就是在页面中不显示,需要等待iframe挂载上去,再进行响应的DOM操......
  • 修改iframe里的css样式及跨域问题
    前段时间给一个客户制作网站在线客服的时候遇到一个问题,客户想尽量美化一下客服的框,而我们研究发现,客服框其实是iframe引用的外部资源。开始我们也天真地以为,自己写段代码,......
  • 网站域名被QQ拦截提示:当前网页非官方页面拦截的解决办法
    今天网友提醒,星空站长​网的链接被QQ屏蔽拦截了。提示:当前页面非官方页面,请复制到浏览器打开。如图:原因:这是因为QQ方面的诈骗信息特别多,所以腾讯官方索性就直接屏蔽了所有的......
  • MUI关闭指定页面之外的其余页面
    有兴趣可以关注公众号:图计算与算法问题:最近在写一个类似微信的APP,基于mui来在android端以及ios端呈现,但是有个问题是在退出登录切换账号的时候发现app会加载之前的缓存......
  • vue2PC端和移动端适配
    之前ui给pc端的设计稿是1920*1080,并没有提过要写移动端网站,所以之前一直做的是pc端。1,安装flexible和postcss-px2rem(命令行安装)npminstalllib-flexible--savenp......
  • Vue 中plugins插件的使用
    Vue中plugins插件的使用1:说明/*##插件1.功能:用于增强Vue2.本质:包含install方法的一个对象,install的第一个参数是Vue,第二个以后的参数是插件使用者传递的数据。3......
  • Vue 中 的 mixin 混入(合)
    Vue中的mixin混入(合)1:说明/*##mixin(混入)1.功能:可以把多个组件共用的配置提取成一个混入对象2.使用方式:第一步定义混合:```{dat......
  • JS_登录页面
    <!DOCTYPEhtml><html> <head> <metacharset="UTF-8"> <title>登录界面</title> <scripttype="text/javascript"src="js/login.js"></script> </head> <body>......
  • vue_登录
    <template><!--根节点--><divclass="login-container"><divclass="login-form-wrap"><!--el-form表单组件每个表单项都必须......