Floating div adaptive center display js code

  • 2020-03-30 01:02:22
  • OfStack

When div is floating, the browser window cannot be centered when it changes (because float is usually left or right).

You can start with jquery to get the width of the surrounding div as the browser changes (plus the listening event)

Then get the width of div for example:


function autoWidth(){
     var bW = $(".call_man").width();//The outer div
     var popWidth = (bW-150)/2    //(150 is the width of div)
    $(".rts").css("left",popWidth);  //I'm going to assign div to the left of div
}
autoWidth();
window.onresize = autoWidth;//Listen for an event


Related articles: