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
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" });