浏览器窗口响应内容 [英] Browser Window Responsive to Content

查看:100
本文介绍了浏览器窗口响应内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其他地方找不到任何与此相关的问题或信息,所以我想我会发布一个问题。有没有办法使 BrowserWindow 响应 index.html 的内容?

I cannot really find any questions or information related to this elsewhere, so I thought I would post a question. Is there a way to make the BrowserWindow responsive to the content of index.html?

我知道您可以通过使用 resizeTo BrowserWindow 的大小来完成此操作c>或远程,但您需要知道显式的大小才能将其设置为。

I know you can likely do this by manually changing the size of the BrowserWindow either with resizeTo or remote, but you would need to know the explicit size to set it to.

是否有一个简单地使窗口对内容作出响应的方法,以便随着文档的 body 的增长,窗口也一样?

Is there a way to simply make the window responsive to the content, so that as the document's body grows, so too does the window?

推荐答案

一种简单的解决方法可能是:使用 setTimeout 创建一个计时器来检查document( document.body )尺寸变化;为此,请使用 getComputedStyle 方法和静态函数存储最后大小的变量。

Maybe a simple approach to this could be: create a timer with setTimeout to check if document(document.body) size change; to this, use getComputedStyle method and a 'static function' variable storing the last size.

function getDocumentWidth(){//returns current document width
  return window.getComputedStyle(document.querySelector('body'), null).getPropertyValue("width");
}

function checkDocumentWidthChanged(callback){//verify by document width change, execute a callback function argument if it happened and resume the verification call
  let ns=getDocumentWidth()
  
  if(this.lastSize!=ns){
    this.lastSize=ns
    callback()
   }
   
   setTimeout(function(){checkDocumentWidthChanged(callback);}, 500);
   
}

checkDocumentWidthChanged.lastSize=getDocumentWidth();//function static var to store the last document size

checkDocumentWidthChanged(function(){//stat the loop
  console.log('New document size:'+checkDocumentWidthChanged.lastSize)
})

document.body.addEventListener('click',function(evt){//Set an event listener to test the proccess
  this.style.width=300+Math.floor(Math.random() * 100)+'px'
  console.log(this.style.width)
})

<body>
Click here to change the document body size or change the browser size.
</body>

这篇关于浏览器窗口响应内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆