Home  >  Article  >  Web Front-end  >  js code for floating div adaptive center display_javascript skills

js code for floating div adaptive center display_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:07:391150browse

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:

Copy the code The code is as follows:

function autoWidth(){

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

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