JavaScript模擬alert彈出框

alert在不同瀏覽器的樣式不一,要統一樣式,只能模擬實現。

html

<div class="mask" id="mask"></div>
<div class="alert" id="alert"></div>

css

.alert{
    width: 60%;
    position: fixed;
    top: 40%;
    left: 0;
    right: 0;
    margin: auto;
    padding: 10px;
    box-sizing: border-box;
    background: #fcfcfc;
    border-radius: 4px;
    box-shadow: 0 0 4px;
    text-align: center;
    display: none;
}
.mask{
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, .7);
    position: fixed;
    top: 0;
    left: 0;
    display: none;
}

javascript

(function(exports, document, undefined){
	var alert = document.getElementById('alert'),
		mask = document.getElementById('mask');
	exports.show_alert = function(message){
		alert.innerHTML = message || '';
		alert.style.display = 'block';
		mask.style.display = 'block';
		mask.addEventListener('touchstart', hide_alert, false);
	};
	exports.hide_alert = function(){
		alert.style.display = 'none';
		mask.style.display = 'none';
		mask.removeEventListener('touchstart', hide_alert, false);
	};
})(window, document);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章