JavaScript 用DIV模拟弹出窗口并跟随窗体滚动

复制以下代码直接写到记事本里保存成html文件即可看到效果 IE6、IE7、FF2兼容,其他浏览器未测试

function getPosition() {
var top    = document.documentElement.scrollTop;
var left   = document.documentElement.scrollLeft;
var height = document.documentElement.clientHeight;
var width  = document.documentElement.clientWidth;
return {top:top,left:left,height:height,width:width};
}
function showPop(){
var width  = 300;  //弹出框的宽度
var height = 160;  //弹出框的高度
var obj    = document.getElementById("pop");
obj.style.display  = "block";
obj.style.position = "absolute";
obj.style.zindex   = "999";
obj.style.width    = width + "px";
obj.style.height   = height + "px";
var Position = getPosition();
leftadd = (Position.width-width)/2;
topadd  = (Position.height-height)/2;
obj.style.top  = (Position.top  + topadd)  + "px";
obj.style.left = (Position.left + leftadd) + "px";
window.onscroll = function (){
var Position   = getPosition();
obj.style.top  = (Position.top  + topadd)  +"px";
obj.style.left = (Position.left + leftadd) +"px";
};
}
function hidePop(){
document.getElementById("pop").style.display = "none";
}

testhide

show
转载:http://www.cnblogs.com/dxef/archive/2007/09/03/879654.html

Original: https://www.cnblogs.com/fineboy/archive/2007/10/12/922477.html
Author: 振河
Title: JavaScript 用DIV模拟弹出窗口并跟随窗体滚动

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/535968/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球