《JS位置中心及代码调用》
//位置在屏幕中心JS代码
$.fn.positionAbsoluteCenter = function () {
if ($(this).length != 1) {
return false;
};
var $this = $(this);
$.fn.positionAbsoluteCenter.func($this);
$(window, this).resize(function () {
$.fn.positionAbsoluteCenter.func($this);
});
};
$.fn.positionAbsoluteCenter.func = function (obj) {
obj.css({ "position": "fixed", "top": (50 - obj.height() / $(window).height() * 50) + "%", "left": (50 - obj.width() / $(window).width() * 50) + "%" });
};
如果要调用的话就在相同的文件下用如下代码:
$("#elementId").positionAbsoluteCenter();
前端-语言