Always display the Div code (css + js) in the middle of the screen)

Source: Internet
Author: User

1. display in the middle. (See: sky100articles1790515)
Copy codeThe Code is 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')"/>
// Functions of the object obtained by element id in the JScript File
Function $ (id)
{
Return document. getElementById (id );
}Copy codeThe Code is as follows: function sc1 (DivId ){
Var Div =$ (DivId );
$ (DivId). style. top = (document.doc umentElement. scrollTop + (document.doc umentElement. clientHeight-$ (DivId). offsetHeight)/2) + "px ";
$ (DivId). style. left = (document.doc umentElement. scrollLeft + (document.doc umentElement. clientWidth-$ (DivId). offsetWidth)/2) + "px ";
// Alert ($ (DivId). style. top );
}

2. Always display in the middle. When scrolling, add the following code in Js:
Copy codeThe Code is 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.