Skip to content

Instantly share code, notes, and snippets.

@tadeubdev
Last active July 28, 2017 15:51
Show Gist options
  • Select an option

  • Save tadeubdev/f8016af651cd0fd8f58d119468dcbbce to your computer and use it in GitHub Desktop.

Select an option

Save tadeubdev/f8016af651cd0fd8f58d119468dcbbce to your computer and use it in GitHub Desktop.
Apagando a luz da página por um iframe. Não esqueça de alterar o "http://site" pelo seu host.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>a</title>
<style>
.LuzApagada #overlay {
z-index: 8;
display: block;
width: 100%;
height: 100%;
top:0; left:0;
position: fixed;
background: rgba(0,0,0,.7);
}
#iframe {
z-index: 9999;
margin: auto;
left:0; right:0;
display: block;
position: absolute;
border: none;
width: 800px;
height: 500px;
}
</style>
</head>
<body>
<div id="container">
<h1> Hello world! </h1>
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc volutpat mi in malesuada molestie. Aliquam sit amet dignissim nisi, non suscipit arcu. Aenean pharetra consectetur tempor. Sed sollicitudin lobortis quam, sed pharetra sem aliquam ut. Duis justo nunc, mattis in magna id, rhoncus maximus ligula. Aenean sollicitudin ac ipsum in sagittis. Nulla elementum augue vitae mollis gravida. Pellentesque luctus semper turpis feugiat congue. </p>
</div>
<div id="overlay"></div>
<div id="boxmodal">
<iframe id="iframe" width="100%" height="300" src="b.html"></iframe>
</div>
<script>
// Adiciona um watch ao message do window
// https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
window.addEventListener("message", function(evt){
// Se ele não vier da origin "http://sites" ele não executa
if(evt.origin==="http://sites") {
document.body.classList.toggle('LuzApagada');
}
}, false);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>b</title>
<style>
* { box-sizing:border-box; }
body { background:#FFF; }
#apagar
{
display: block;
cursor:pointer;
}
.LuzApagada #overlay {
z-index: 8;
display: block;
width: 100%;
height: 100%;
top:0; left:0;
position: fixed;
background: rgba(0,0,0,.7);
}
#boxmodal {
z-index: 9999;
margin: auto;
left:0; right:0;
display: block;
position: absolute;
padding: 20px;
width: 90%;
height: 90%;
}
</style>
</head>
<body>
<div id="overlay"></div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc volutpat mi in malesuada molestie.</p>
<div id="boxmodal">
<iframe width="560" height="315" src="https://www.youtube.com/embed/wPT-YdNLxCs" frameborder="0" allowfullscreen></iframe>
<button id="apagar">Apagar a luz</button>
</div>
<script>
var LuzAcesa = false;
document.querySelector('#apagar').onclick = function() {
// Apaga/acende a luz da página
LuzAcesa = !LuzAcesa;
// Altera a classe LuzApagada do elemento `body`
// Se ele possuir a classe ele remove, senão adicionar
// https://developer.mozilla.org/pt-BR/docs/Web/API/Element/classList
document.body.classList.toggle('LuzApagada');
// Altera o texto do botão
// Não é necessário deixar
this.innerHTML = (LuzAcesa? 'Acender': 'Apagar') + ' a luz';
// Envia uma mensagem para o pai
window.parent.postMessage(LuzAcesa, "http://sites");
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment