As you can see in the example below, when the links are clicked a div appears and when you click the browser's back button, it hides. I would like that by clicking the "close" button inside the open page, you can also hide it. I need to reproduce the same system that happens when using the back of the browser, because with a simple hide function ( $('div').hide();
) ends up bugging this system, I mean, the browser. Can you help me?
link in codepen: link
$(document).ready(function() {
$("#paginaUm").click(function(event){
$('.pagina.um').fadeIn();
});
$("#paginaDois").click(function(event){
$('.pagina.dois').fadeIn();
});
});
window.onhashchange = function(e) {
var oldURL = e.oldURL.split('#')[1];
var newURL = e.newURL.split('#')[1];
if (oldURL == 'paginaUm') {
$('.pagina.um').fadeOut();
e.preventDefault();
return false;
}
if (oldURL == 'paginaDois') {
$('.pagina.dois').fadeOut();
e.preventDefault();
return false;
}
//console.log('old:'+oldURL+' new:'+newURL);
}
body {
background-image: url(https://unsplash.it/1000/1010);
background-size:cover;
height:100%;
}
.pagina{position:fixed; display:none; top:0; left:0; width:100%; height:100%; background:gray; color:white; padding:20px;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><ahref="#paginaUm" id="paginaUm">Pagina 1</a>
<div class="pagina um">
<h1>Popup 1</h1>
<button class="fechar">fechar.</button>
</div>
<a href="#paginaDois" id="paginaDois">Pagina 2</a>
<div class="pagina dois">
<h1>Popup 2</h1>
<button class="fechar">fechar</button>
</div>