Home > Article > Web Front-end > js code for floating div adaptive center display_javascript skills
When the div is floating and the browser window changes, it cannot be displayed in the center (because when floating, the left or right value is usually set)
You can first use jquery to get the width of the surrounding div that changes with the browser (plus listening events)
After, get the width of the div. For example:
var bW = $(".call_man").width();//Peripheral div
var popWidth = (bW-150)/2 // (150 is the width of the div)
$( ".rts").css("left",popWidth); //Assign the obtained value to the div, which is the distance between the div and the left
}
autoWidth();
window.onresize = autoWidth;//Listening events