首页 > 编程 > JavaScript > 正文

始终在屏幕中间显示Div的代码(css+js)

2019-11-20 23:54:35
字体:
来源:转载
供稿:网友
一、在中间显示;(参考:sky100articles1790515)
复制代码 代码如下:

.ordersearchDivCss
{
position: absolute;
z-index: 100;
display: block;
background-color: #6ec1df;
}
<div class="ordersearchDivCss" id="DivMain" style="width: 400px; height:200px" align="center"></div>

Js code
调用:<input type="button" id="Button1" onclick="sc1(‘DivMain')" />
// JScript 文件 通过元素id得到对象的函数
function $(id)
{
return document.getElementById(id);
}
复制代码 代码如下:

function sc1(DivId) {
var Div = $(DivId);
$(DivId).style.top = (document.documentElement.scrollTop + (document.documentElement.clientHeight - $(DivId).offsetHeight) / 2) + "px";
$(DivId).style.left = (document.documentElement.scrollLeft + (document.documentElement.clientWidth - $(DivId).offsetWidth) / 2) + "px";
//alert($(DivId).style.top);
}

二、始终在中间显示,滚动时,在Js中增加以下代码:
复制代码 代码如下:

function scall() {
sc1("DivMain");
}
window.onscroll = scall;
window.onresize = scall;
window.onload = scall;
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表