Criando um popup em javascript, css com timer | keyframe – Comunicação Digital, Websites

Blog

Criando um popup em javascript, css com timer

Neste final de ano tive algumas demandas para criação de popup para mensagens de natal ou aviso de recesso, enfim. Neste tutorial irei mostrar como podemos fazer um popup com um determinado tempo para fechar, ou simplesmente clicando no fundo ou no botão fechar, não me preocupei com o design e sim com as funcionalidades. Caso tenham alguma sugestão ou dúvidas, postem seus comentários.

Vejam o arquivo funcionando

[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>popupjs</title>
<style>
#mask {
background-color: #666;
background: url(http://keyframe.com.br/wp-content/uploads/2013/01/bg_pop.png) repeat;
height: 100%;
left: 0;
position: fixed;
top: 0;
width: 100%;
z-index: 9000;
}
#popup {
color: #000066;
height: 500px;
display: none;
left: 50%;
margin-left: -250px;
margin-top: -158px;
padding: 0;
position: fixed;
top: 32%;
width: 500px;
z-index: 9999;
}
#fechar {
position: relative;
}
#fechar a {
position: absolute;
right: -27px;
top: 0;
border: 1px solid #F00;
width: 24px;
height: 24px;
text-indent: -4000px;
overflow: hidden;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}
#fechar a:hover {
opacity: 0.7;
filter: alpha(opacity = 70);
}
</style>
</head>

<body>
<div id="mask" onclick="javascript: fechar();"></div>
<div id="popup" class="popup">
<div id="fechar"><a href="javascript: fechar();">Fechar</a></div>
<div id="img_pop"><a href="https://www.facebook.com/pages/Comunica%C3%A7%C3%A3o-Digital-keyframe/108236579266747" target="_blank"><img src="http://keyframe.com.br/wp-content/uploads/2013/01/keyframe.jpg" /></a></div>
</div>
<script language="javascript">
// Função que fecha o pop-up ao clicar no botao fechar
function fechar(){
document.getElementById(‘popup’).style.display = ‘none’;
document.getElementById(‘mask’).style.display = ‘none’;
}
// Aqui definimos o tempo para fechar o pop-up automaticamente em milissengundos
function abrir(){
document.getElementById(‘popup’).style.display = ‘block’;
setTimeout ("fechar()", 15000);
}
function trocaordem(){
document.getElementById("popup").style.zIndex="1";
}
abrir();
</script>
<p>"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."</p>
<p>"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."</p>
<p>"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."</p>
<p>"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."</p>
<p>"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."</p>
<p>"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."</p>
</body>
</html>
[/code]

Comentei o código javascript para facilitar a manipulação, neste exemplo o popup já abre automaticamente porque chamei a função abrir() no final do código, podemos chamá-la no onload do body por exemplo ou através de um link,  fica a critério:

onclick=”javascript: abrir();”
onclick=”javascript: fechar();”

Dentro da função abrir, temos o setTimeout que passo parâmetro a função fechar e os segundos que o popup ficará visível (em milisegundos) no caso 15000 que são 15 segundos. Espero que o código esteja claro e que usem em seus projetos. Até a próxima

Este post tem 28 comentários

  1. Ailton Sousa says:

    Valew pelo codigo!
    ———————————————-
    Dica: (USAR SEM FECHAR, PARA UM SITE EM MANUTENÇÃO)

    Aumente o numero que tá 15000 na parte:
    ————————————————
    }
    // Aqui definimos o tempo para fechar o pop-up automaticamente em milissengundos
    function abrir(){
    document.getElementById(‘popup’).style.display = ‘block’;
    setTimeout (“fechar()”, 15000);
    }
    ————————————————
    Para 620000 para o popup demorar fechar

    e exclua a parte:
    ————————————————
    // Função que fecha o pop-up ao clicar no botao fechar
    function fechar(){
    document.getElementById(‘popup’).style.display = ‘none’;
    document.getElementById(‘mask’).style.display = ‘none’;
    }
    ————————————————-
    Para desabilitar o botão fechar.

    Retire todo o texto:
    ————————————————-
    “On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same …
    ————————————————
    E pronto vai funcionar coretamente!

    >>>>Isso foi testado<<<<

  2. We absolutely love your blog and find most of your post’s to be
    exactly I’m looking for. Would you offer guest writers to write
    content available for you? I wouldn’t mind publishing a post or
    elaborating on most of the subjects you write about here.
    Again, awesome web log!

Deixe seu comentário