Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 16:37:33
Original
1448 people have browsed it
var tipsWidth = $(".Loading").css("width").replace("px", "") //先获取 
var windowWidth = $(window).width(); 
$(".Loading").css({ "display": "block" }); //再显示 
$(".Loading").css({ "left": (windowWidth - tipsWidth) / 2 + "px" }); 
Copy after login

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" });
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!