Следующее не будет работать из-за его синтаксиса:
$("#popUp").css("margin-top", "-" (popupH / 2 40) "px");
$("#popUp").css("margin-left", "-" popupH / 2 "px");
Это должно быть что-то вроде:
$("#popUp").css("margin-top", "-" + (popupH / 2 + 40) + "px");
$("#popUp").css("margin-left", "-" + popupH / 2 + "px");
Вот почему
$("#grayBack").css('opacity', 0).fadeTo(300, 0.5, function() {
$("#popUp").fadeIn(500);
});
никогда не вызывается.
(обратите внимание на +
)
Рабочий пример с моим собственным CSS:
function showPopup() {
$("#popUp").before('<div id=grayBack></div>');
var popupH = $("#popUp").height();
var popupW = $("#popUp").width();
//$("#popUp").css("margin-top", "-" + (popupH / 2 + 40) + "px");
//$("#popUp").css("margin-left", "-" + popupH / 2 + "px");
$("#grayBack").css('opacity', 0).fadeTo(300, 0.5, function() {
$("#popUp").fadeIn(500);
});
}
function hidePopup() {
$("#grayBack").fadeOut('fast', function() {
$(this).remove()
});
$("#popUp").fadeOut('fast', function() {
$(this).hide()
});
}
#popUp {
display: none;
width: 200px;
height: 200px;
z-index: 100;
position: absolute;
top: 0;
left: 0;
background-color: #fff;
}
#grayBack {
position: absolute;
top: 0;
left: 0;
height: 100vh;
width: 100vw;
background-color: rgba(0, 0, 0, .4);
z-index: 10;
opacity: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="buttons">
<input id="buttonHoroquartz" type="button" value="Button" onclick="showPopup();">
</div>
<div id="popUp">
<h3>Pop up</h3>
<p>Hello !</p>
<p></p>
<input type="button" value="Ok" onclick="hidePopup();">
</div>