简体   繁体   English

在 Vue.js 中插入响应式 iframe 的 javascript 代码

[英]Insert javascript code for responsive iframe in Vue.js

I need to integrate some iframes created on a chart platform in my Vue.js app.我需要在我的 Vue.js 应用程序中集成一些在图表平台上创建的 iframe。 The code for the responsive iframe is composed of some html code, that I put in the template and some javascript that comes between a pair of script tags.响应式 iframe 的代码由我放在模板中的一些 html 代码和一些位于一对脚本标签之间的 javascript 组成。

Here is the html part:这是html部分:

<iframe
  id="datawrapper-chart-8dfPN"
  src="//datawrapper.dwcdn.net/8dfPN/4/"
  scrolling="no"
  frameborder="0"
  allowtransparency="true"
  style="width: 0; min-width: 100% !important;" height="400"
>
</iframe>

and the javascript part:和 javascript 部分:

<script type="text/javascript">
  if("undefined"==typeof window.datawrapper)window.datawrapper={};
  window.datawrapper["8dfPN"]={},
  window.datawrapper["8dfPN"].embedDeltas={"100":481,"200":427,"300":400,"400":400,"500":400,"700":400,"800":400,"900":400,"1000":400},
  window.datawrapper["8dfPN"].iframe=document.getElementById("datawrapper-chart-8dfPN"),
  window.datawrapper["8dfPN"].iframe.style.height=window.datawrapper["8dfPN"].embedDeltas[Math.min(1e3,Math.max(100*Math.floor(window.datawrapper["8dfPN"].iframe.offsetWidth/100),100))]+"px",
  window.addEventListener("message",function(a){if("undefined"!=typeof a.data["datawrapper-height"])for(var b in a.data["datawrapper-height"])if("8dfPN"==b)window.datawrapper["8dfPN"].iframe.style.height=a.data["datawrapper-height"][b]+"px"});
</script> 

I don't understand how I have to manage this code.我不明白我必须如何管理这段代码。 If I put it in the template (as it has html elements) Views tells me I can't use scripts in templates.如果我把它放在模板中(因为它有 html 元素),视图告诉我我不能在模板中使用脚本。 But embed the code in the portion of my Vue file doesn't work either.但是将代码嵌入我的 Vue 文件部分也不起作用。 I tried to get rid of the elements.我试图摆脱这些元素。 But it's not working.但它不起作用。

Is there a way to refactor this code so I can fully use it with Vue.js?有没有办法重构这段代码,以便我可以在 Vue.js 中充分使用它?

If you look at the provided JavaScript, there really isn't much to it.如果您查看所提供的 JavaScript,您会发现它并没有什么特别之处。 It's setting an initial height and then setting up an event listener to manage the iframe's height.它设置了一个初始高度,然后设置了一个事件监听器来管理 iframe 的高度。

To start we'll bind the height to a data property iframeHeight .首先,我们将height绑定到数据属性iframeHeight

<iframe
  ref="chartiframe"
  id="datawrapper-chart-8dfPN" 
  src="//datawrapper.dwcdn.net/8dfPN/4/"
  scrolling="no"
  frameborder="0"
  allowtransparency="true"
  style="width: 0; min-width: 100% !important;"
  :height="iframeHeight"></iframe>

Then然后

  1. Create const variable for the embeddedDeltasembeddedDeltas创建const变量
  2. Set the initial height in the mounted hook.mounted挂钩中设置初始高度。
  3. Setup the message event listener in the mounted hookmounted钩子中设置message事件监听mounted
  4. Create a method to handle the resize创建一个方法来处理调整大小
  5. Remove the event listener in the beforeDestroy hook移除beforeDestroy钩子中的事件监听器

ddd滴滴

const DELTAS = {
        "100":481,
        "200":427,
        "300":400,
        "400":400,
        "500":400,
        "700":400,
        "800":400,
        "900":400,
        "1000":400,
};

export default {
  data() {
    return {
      iframeHeight: '0px', // initial height
    };
  },
  computed: {
    // Necessary for initial iframe height
    iframeOffsetWidth() {      
      return this.$refs['chartiframe'].offsetWidth;
    },
  },
  mounted() {
    // set initial iframe height
    this.iframeHeight = `{DELTAS[Math.min(1e3,Math.max(100*Math.floor(this.iframeOffsetWidth/100),100))]}px`;
    // setup event listener
    window.addEventListener('message', this.handleIframeResize);
  },
  beforeDestroy() {
    // destroy event listener
    window.removeEventListener('message', this.handleIframeResize);
  },
  methods: {
    handleIframeResize(e) {
      for(var b in e.data['datawrapper-height']) {
        if ('8dfPN' === b) {
          this.iframeHeight = `${e.data['datawrapper-height'][b]}px`;
        }
      }
    },
  },
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM