Home >Web Front-end >JS Tutorial >jquery implements a simple example of popping up a div and always displaying it in the middle of the screen_jquery

jquery implements a simple example of popping up a div and always displaying it in the middle of the screen_jquery

WBOY
WBOYOriginal
2016-05-16 16:56:251036browse

The following code block will always keep the pop-up div in the center of the screen. I hope it will be helpful to everyone!

Copy code The code is as follows:

//Always display the specified DIV in the middle of the screen
function setDivCenter(divName){
var top = ($(window).height() - $(divName).height())/2;
var left = ($(window).width () - $(divName).width())/2;
var scrollTop = $(document).scrollTop();
var scrollLeft = $(document).scrollLeft();
$( divName).css( { position : 'absolute', 'top' : top scrollTop, left : left scrollLeft } ).show();
}  
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn