Last active
August 29, 2015 14:07
-
-
Save guilhermemarconi/82ecd6f2be3b1803c780 to your computer and use it in GitHub Desktop.
Custom JavaScript Alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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