Skip to content

Instantly share code, notes, and snippets.

@guilhermemarconi
Last active August 29, 2015 14:07
Show Gist options
  • Save guilhermemarconi/82ecd6f2be3b1803c780 to your computer and use it in GitHub Desktop.
Save guilhermemarconi/82ecd6f2be3b1803c780 to your computer and use it in GitHub Desktop.
Custom JavaScript Alert
/*
* Basic style to the custom alert.
* Customize it to your personal use.
*/
.alert-wrap {
position: fixed;
top: 0;
left: 0;
z-index: 2147483647; /* maximum value for a 32bit integer */
display: none;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
}
.alert-box {
position: fixed;
top: 20%;
left: calc(50% - 150px);
width: 300px;
}
.alert-box p {
text-align: center;
}
.alert-box .close {
-webkit-appearance: none;
}
<button id="test" onclick="alert('Test alert');">Click me</button>
<!-- add the following in the end of html document -->
<div class="alert-wrap">
<div class="alert-box">
<!-- do not remove the p tag. the alert message goes inside it -->
<p></p>
<button class="close">Close</button>
</div>
</div>
(function($) {
var alertWrap = $('.alert-wrap'),
closeAlert = alertWrap.find('.close');
window.alert = function(message) {
alertWrap.find('p').text(message);
alertWrap.fadeIn();
}
/*
* the function below makes the .close button useless
* closing the alert after clicking on any place
*/
alertWrap.on('click', function() {
$(this).fadeOut(function() {
$(this).find('p').text('');
});
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment