Always display div code in the middle of the screen (CSS+JS) _javascript tips

Source: Internet
Author: User
First, in the middle of the display; (Reference: sky100articles1790515)
Copy Code code as follows:

. 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
Call: <input type= "button" id= "Button1" onclick= "SC1 (' Divmain ')"/>
The JScript file gets the function of the object through the element ID
function $ (ID)
{
return document.getElementById (ID);
}
Copy Code code as follows:

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);
}

second, always in the middle of the display, scrolling, in JS Add the following code:
Copy Code code as follows:

function Scall () {
SC1 ("Divmain");
}
Window.onscroll = Scall;
Window.onresize = Scall;
Window.onload = Scall;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.