将Div放置在中间的JavaScript数学(算法)

将Div放置在中间的JavaScript数学(算法),javascript,math,mobile,responsive-design,Javascript,Math,Mobile,Responsive Design,我试图将一个具有一定高度和宽度的div框定位到具有一定高度和宽度的屏幕中心 检测宽度和高度。我正在努力成为一名更好的程序员,数学和编程并不总是我最好的品质。我意识到我需要进行除法和百分比修正,但需要一个指针: <script type="text/javascript"> var w=document.width; var h=document.height; var img_w=200; var img_h=300; var css=; /* Example w=600

我试图将一个具有一定高度和宽度的div框定位到具有一定高度和宽度的屏幕中心

检测宽度和高度。我正在努力成为一名更好的程序员,数学和编程并不总是我最好的品质。我意识到我需要进行除法和百分比修正,但需要一个指针:

<script type="text/javascript">

var w=document.width;
var h=document.height;

var img_w=200;
var img_h=300;

var css=;

/*

Example

w=600;
h=400;

img_w=200;
img h=300;


therefore css will be

position:absolute;
top:150px;
left:100px;
right:100px;
bottom:100px;

see my image.

*/

document.writeln(css);
</script>

var w=文件宽度;
var h=文件高度;
var img_w=200;
var img_h=300;
var css=;
/*
例子
w=600;
h=400;
img_w=200;
img h=300;
因此,css将是
位置:绝对位置;
顶部:150px;
左:100px;
右:100px;
底部:100px;
看看我的形象。
*/
书面文件(css);


因此,基本上,无论
img_w
img_h
是什么,框的位置都应该在中间留有边距。

您要寻找的公式如下:

left = ( width - img_width ) / 2
top = ( height - img_height ) / 2

PS:在您的示例中,您反转了
h
w

您要查找的公式如下:

left = ( width - img_width ) / 2
top = ( height - img_height ) / 2

PS:在你的例子中,你把
h
w
倒过来,我首先认为数学是(正如其他人所指出的)

数学是:(总宽度-图像宽度)/2。身高也是如此


但是,为什么在你的例子中顶部的150px和底部的100px是?

我首先认为数学是正确的(正如其他人指出的那样)

Block css:{
    left: 50%;
    top: 50%;
}

JS (useing jQuery): {
    block.css({
        'margin-left': '-' + (block.outerWidth() / 2), // margin-left: -1/2 of block width
        'margin-top': '-' + (block.outerHeight() / 2), // margin-top: -1/2 of block height
    })
}
数学是:(总宽度-图像宽度)/2。身高也是如此


但是,为什么在您的示例中顶部为150px,底部为100px?

请让我建议一种替代方法

Block css:{
    left: 50%;
    top: 50%;
}

JS (useing jQuery): {
    block.css({
        'margin-left': '-' + (block.outerWidth() / 2), // margin-left: -1/2 of block width
        'margin-top': '-' + (block.outerHeight() / 2), // margin-top: -1/2 of block height
    })
}
CSS不是一种过程性语言,而是一种声明性语言。让浏览器为您计算并使用
margin:auto
。我知道这不是你想要的,但是CSS就是这样


请参见示例。

请让我建议一种替代方法

CSS不是一种过程性语言,而是一种声明性语言。让浏览器为您计算并使用
margin:auto
。我知道这不是你想要的,但是CSS就是这样


请参阅以获取示例。

如果在调整窗口大小期间需要使其居中,则还必须检查窗口大小调整事件

我创建了这个JSFIDLE:

javascript:

function updateBoxPosition($box) {
    var window_width = $(window).width();
    var window_height = $(window).height();
    var left_pos = (window_width - $box.width()) / 2;
    var top_pos = (window_height - $box.height()) / 2;

    $box.css('left', left_pos);
    $box.css('top', top_pos);
}

$(function() {
    var $box = $("#box");
    var window_width = 0;
    var window_height = 0;

    var resizeTimer;
    $(window).on('resize', function () {
        clearTimeout(resizeTimer);
        resizeTimer = setTimeout(function () {
            updateBoxPosition($box);
        }, 25);
    });

    updateBoxPosition($box);          
});​
top函数处理定位,定位绑定在onload事件期间完成。它确实需要jQuery,但我建议在这种情况下使用jQuery,因为浏览器处理窗口宽度的方式有很多不同


为调整大小事件添加的计时器是为了提高性能,调整大小事件在调整大小期间会频繁触发。

如果在调整窗口大小期间需要将其居中,则还必须检查窗口调整大小事件

我创建了这个JSFIDLE:

javascript:

function updateBoxPosition($box) {
    var window_width = $(window).width();
    var window_height = $(window).height();
    var left_pos = (window_width - $box.width()) / 2;
    var top_pos = (window_height - $box.height()) / 2;

    $box.css('left', left_pos);
    $box.css('top', top_pos);
}

$(function() {
    var $box = $("#box");
    var window_width = 0;
    var window_height = 0;

    var resizeTimer;
    $(window).on('resize', function () {
        clearTimeout(resizeTimer);
        resizeTimer = setTimeout(function () {
            updateBoxPosition($box);
        }, 25);
    });

    updateBoxPosition($box);          
});​
top函数处理定位,定位绑定在onload事件期间完成。它确实需要jQuery,但我建议在这种情况下使用jQuery,因为浏览器处理窗口宽度的方式有很多不同


为调整大小事件添加的计时器是为了提高性能,调整大小事件在调整大小期间会频繁触发。

实际图像比例将发生变化,因此上面的我的图像居中。实际图像比例将发生变化,因此上面的我的图像居中。