JQuery pop-up layer, always displayed in the middle of the screen

Source: Internet
Author: User

1. Make the layer always appear in the middle of the screen:

Style code:

HTML code
    1. . model{
    2. Position:absolute; z-index:1003;
    3. width:320px; height:320px; Text-align:center;
    4. Background-color: #0066FF; Display:none;
    5. }

jquery Code:

JS Code
  1. Make the specified div always appear in the middle of the screen
  2. function Letdivcenter (divname) {
  3. var top = ($ (window). Height ()-$ (divname). Height ())/2;
  4. var left = ($ (window). Width ()-$ (divname). Width ())/2;
  5. var scrolltop = $ (document). ScrollTop ();
  6. var scrollleft = $ (document). ScrollLeft ();
  7. $ (divname). css ({position: ' absolute ', ' top ': Top + scrolltop, Left:left + scrollleft}). Show ();
  8. }

HTML code:

HTML code
  1. <a href="javascript:;" onclick="letdivcenter (' #model ')"> Point I let div always show in the middle of the screen </a><br / >
  2. <div>
  3. <div id="model" class="model">
  4. This is the content (but not the vertical center display) I hope you master, can add. My little brother thanked me here.
  5. </div>
  6. </div>

Run it and look at the effect.

Next, summarize them and combine them into one. That is, when the div layer pops up, but also to pop up the mask layer, good, no nonsense to say, look at the code:

1. CSS style:

HTML code
  1. <style type="Text/css">
  2. . Mask {
  3. Position:absolute; top:0px;  Filter:alpha (opacity=); Background-color: #777;
  4. z-index:1002; left:0px;
  5. opacity:0.5; -moz-opacity:0.5;
  6. }
  7. . model{
  8. Position:absolute; z-index:1003;
  9. width:320px; height:320px; Text-align:center;
  10. Background-color: #0066FF; Display:none;
  11. }
  12. </style>

2. jquery Code:

HTML code
  1. <script type="Text/javascript">
  2. Compatible with Firefox, IE8
  3. function Showmask () {
  4. $ ("#mask"). CSS ("height", $ (document). Height ());
  5. $ ("#mask"). CSS ("width", $ (document). width ());
  6. $ ("#mask"). Show ();
  7. }
  8. Make the specified div always appear in the middle of the screen
  9. function Letdivcenter (divname) {
  10. var top = ($ (window). Height ()-$ (divname). Height ())/2;
  11. var left = ($ (window). Width ()-$ (divname). Width ())/2;
  12. var scrolltop = $ (document). ScrollTop ();
  13. var scrollleft = $ (document). ScrollLeft ();
  14. $ (divname). css ({position: ' absolute ', ' top ': Top + scrolltop, Left:left + scrollleft}). Show ();
  15. }
  16. function ShowAll (divname) {
  17. Showmask ();
  18. Letdivcenter (Divname);
  19. }
  20. </Script>

3.HTML Code:

HTML code
  1. <div id="Mask" class="Mask"></div>
  2. <a href= "javascript:;"  onclick= "Showmask ()"  > Dot I show mask layer </a>< br />  
  3. <a href= "javascript:;"  onclick= "Letdivcenter (' #model ')" > Dot I let div always show in the middle of the screen </a>< br />  
  4. <a href="javascript:;" onclick="showall (' #model ')"> Point I show All </a><br />
  5. <div>
  6. <div id="model" class="model">
  7. This is the content (but not the vertical center display) I hope you master, can add. My little brother thanked me here.
  8. </div>
  9. </div>

JQuery pop-up layer, always displayed in the middle of the screen

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.