基于jquery的彈出提示框始終處于窗口的居中位置(類似于alert彈出框的效果)
更新時間:2011年09月28日 23:44:01 作者:
之前寫的代碼,都是在當前窗口位于居中,可是一旦窗口縮小或者放大都不是位于居中的位置了,但是一直想寫的一個類似于alert彈出窗口的效果。
原理很簡單:
獲取當前屏幕(窗體)的寬度和高度,因為不同瀏覽器的窗體大小是不一樣的。有了這個,可以計算出來垂直居中的坐標。但是滑動了滾動條怎么依然垂直居中呢?這個時候就要獲取當前窗體距離頁面頂部的高度,加到剛剛的y軸坐標即可。
$(document)是獲取整個網(wǎng)頁的,$(window)是獲取當前窗體的,這個要搞清楚。
最后把獲取的坐標賦給窗體即可,窗體本身是絕對定位的,所以自然可以到窗體中間。
具體代碼:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>彈出確認框始終位于窗口的中間位置的測試</title>
<style type="text/css">
.mask { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: #000; opacity: 0.5; filter: alpha(opacity=50); display: none; z-index: 99; }
.mess { position: absolute; display: none; width: 250px; height: 100px; border: 1px solid #ccc; background: #ececec; text-align: center; z-index: 101; }
</style>
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.btn').click(function() {
$('.mask').css({'display': 'block'});
center($('.mess'));
check($(this).parent(), $('.btn1'), $('.btn2'));
});
// 居中
function center(obj) {
var screenWidth = $(window).width(), screenHeight = $(window).height(); //當前瀏覽器窗口的 寬高
var scrolltop = $(document).scrollTop();(),//獲取當前窗口距離頁面頂部高度
var objLeft = (screenWidth - obj.width())/2 ;
var objTop = (screenHeight - obj.height())/2 + scrolltop;
obj.css({left: objLeft + 'px', top: objTop + 'px','display': 'block'});
//瀏覽器窗口大小改變時
$(window).resize(function() {
screenWidth = $(window).width();
screenHeight = $(window).height();
scrolltop = $(document).scrollTop();
objLeft = (screenWidth - obj.width())/2 ;
objTop = (screenHeight - obj.height())/2 + scrolltop;
obj.css({left: objLeft + 'px', top: objTop + 'px','display': 'block'});
});
//瀏覽器有滾動條時的操作、
$(window).scroll(function() {
screenWidth = $(window).width();
screenHeight = $(widow).height();
scrolltop = $(document).scrollTop();
objLeft = (screenWidth - obj.width())/2 ;
objTop = (screenHeight - obj.height())/2 + scrolltop;
obj.css({left: objLeft + 'px', top: objTop + 'px','display': 'block'});
});
}
//確定取消的操作
function check(obj, obj1, obj2) {
obj1.click(function() {
obj.remove();
closed($('.mask'), $('.mess'));
});
obj2.click(function() {
closed($('.mask'), $('.mess'));
}) ;
}
// 隱藏 的操作
function closed(obj1, obj2) {
obj1.hide();
obj2.hide();
}
});
</script>
</head>
<body>
<input type="button" class="btn" value="btn"/>
<div>彈出確認框始終位于窗口的中間位置的測試</div>
<div class="mask"></div>
<div class="mess">
<p>確定要刪除嗎?</p>
<p><input type="button" value="確定" class="btn1"/>
<input type="button" value="取消"class="btn2"/></p>
</div>
</body>
</html>
獲取當前屏幕(窗體)的寬度和高度,因為不同瀏覽器的窗體大小是不一樣的。有了這個,可以計算出來垂直居中的坐標。但是滑動了滾動條怎么依然垂直居中呢?這個時候就要獲取當前窗體距離頁面頂部的高度,加到剛剛的y軸坐標即可。
$(document)是獲取整個網(wǎng)頁的,$(window)是獲取當前窗體的,這個要搞清楚。
最后把獲取的坐標賦給窗體即可,窗體本身是絕對定位的,所以自然可以到窗體中間。
具體代碼:
復(fù)制代碼 代碼如下:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>彈出確認框始終位于窗口的中間位置的測試</title>
<style type="text/css">
.mask { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: #000; opacity: 0.5; filter: alpha(opacity=50); display: none; z-index: 99; }
.mess { position: absolute; display: none; width: 250px; height: 100px; border: 1px solid #ccc; background: #ececec; text-align: center; z-index: 101; }
</style>
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.btn').click(function() {
$('.mask').css({'display': 'block'});
center($('.mess'));
check($(this).parent(), $('.btn1'), $('.btn2'));
});
// 居中
function center(obj) {
var screenWidth = $(window).width(), screenHeight = $(window).height(); //當前瀏覽器窗口的 寬高
var scrolltop = $(document).scrollTop();(),//獲取當前窗口距離頁面頂部高度
var objLeft = (screenWidth - obj.width())/2 ;
var objTop = (screenHeight - obj.height())/2 + scrolltop;
obj.css({left: objLeft + 'px', top: objTop + 'px','display': 'block'});
//瀏覽器窗口大小改變時
$(window).resize(function() {
screenWidth = $(window).width();
screenHeight = $(window).height();
scrolltop = $(document).scrollTop();
objLeft = (screenWidth - obj.width())/2 ;
objTop = (screenHeight - obj.height())/2 + scrolltop;
obj.css({left: objLeft + 'px', top: objTop + 'px','display': 'block'});
});
//瀏覽器有滾動條時的操作、
$(window).scroll(function() {
screenWidth = $(window).width();
screenHeight = $(widow).height();
scrolltop = $(document).scrollTop();
objLeft = (screenWidth - obj.width())/2 ;
objTop = (screenHeight - obj.height())/2 + scrolltop;
obj.css({left: objLeft + 'px', top: objTop + 'px','display': 'block'});
});
}
//確定取消的操作
function check(obj, obj1, obj2) {
obj1.click(function() {
obj.remove();
closed($('.mask'), $('.mess'));
});
obj2.click(function() {
closed($('.mask'), $('.mess'));
}) ;
}
// 隱藏 的操作
function closed(obj1, obj2) {
obj1.hide();
obj2.hide();
}
});
</script>
</head>
<body>
<input type="button" class="btn" value="btn"/>
<div>彈出確認框始終位于窗口的中間位置的測試</div>
<div class="mask"></div>
<div class="mess">
<p>確定要刪除嗎?</p>
<p><input type="button" value="確定" class="btn1"/>
<input type="button" value="取消"class="btn2"/></p>
</div>
</body>
</html>
相關(guān)文章
jQuery實現(xiàn)新消息在網(wǎng)頁標題閃爍提示
這篇文章主要介紹了jQuery實現(xiàn)新消息在網(wǎng)頁標題閃爍提示的相關(guān)資料,需要的朋友可以參考下2015-06-06
jQuery-serialize()輸出序列化form表單值的方法
jQuery-serialize()輸出序列化表單值在工作中很常見也很實用,于是本人搜集整理了一些,需要了解的朋友可以詳細參考下2012-12-12
jquery實現(xiàn)自定義樹形表格的方法【自定義樹形結(jié)構(gòu)table】
這篇文章主要介紹了jquery實現(xiàn)自定義樹形表格的方法,結(jié)合實例形式分析了jQuery創(chuàng)建自定義樹形結(jié)構(gòu)table的相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
詳談jQuery.load()和Jsp的include的區(qū)別
下面小編就為大家?guī)硪黄斦刯Query.load()和Jsp的include的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
jQuery progressbar通過Ajax請求實現(xiàn)后臺進度實時功能
這篇文章主要介紹了jQuery progressbar通過Ajax請求實現(xiàn)后臺進度實時功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10

