Home  >  Article  >  Web Front-end  >  js cannot get the width of the hidden div and can only display it first and then get it_javascript skills

js cannot get the width of the hidden div and can only display it first and then get it_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:37:331455browse
var tipsWidth = $(".Loading").css("width").replace("px", "") //先获取 
var windowWidth = $(window).width(); 
$(".Loading").css({ "display": "block" }); //再显示 
$(".Loading").css({ "left": (windowWidth - tipsWidth) / 2 + "px" }); 

I want to get the actual width of a div whose width is adaptive based on the content. This div is initially hidden. I want to get its actual width and center it. I have been working on it for a long time, but I can't get it. The real width of the div. Later I found that js cannot get the width of the hidden div. The current method is to display it first, then get its width, and calculate the position

$(".Loading").css({ "display": "block" });// 先显示 
var tipsWidth = $(".Loading").css("width").replace("px", "") //再获取 
var windowWidth = $(window).width(); 
$(".Loading").css({ "left": (windowWidth - tipsWidth) / 2 + "px" });
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