jQuery水平和垂直定位脚本在IE8中不起作用

jQuery horizontal and vertical positioning script not working in IE8

本文关键字:IE8 不起作用 脚本 定位 水平 垂直 jQuery      更新时间:2023-09-26

这是设计http://vvcap.net/db/OuxLOYioWmjIAavL5N9U.htp

这是一个演示!http://designobvio.us/GoldGrid2/

我正在尝试让一个垂直和水平的jQuery脚本在IE8上运行:

脚本代码

<script  type="text/javascript">
    $(document).ready(function(){
     $(window).resize(function(){
      $('.padder').css({
       position:'absolute',
       left: ($(window).width() 
         - $('.padder').outerWidth())/2,
       top: ($(window).height() 
         - $('.padder').outerHeight())/2
      });
     });
     // To initially run the function:
     $(window).resize();
    });
</script>

关于为什么这个脚本在IE8中不起作用的任何想法?

您可能希望将高度和宽度声明为数字,以便确定数学可以正常工作,并且由于您要更改 CSS,因此值需要是包含"px"的字符串

left: ((Number($(window).width()) - Number($('.padder').outerWidth()))/2)+'px',
top: ((Number($(window).height()) - Number($('.padder').outerHeight()))/2)+'px'

按照这些思路,我可能会在 CSS 之前设置值。

var leftVal = Number($(window).width()) - Number($('.padder').outerWidth())/2;
var topVal = Number($(window).height()) - Number($('.padder').outerHeight()))/2;

然后:

left: leftVal+'px',
top: topVal+'px'

全功能:

$(window).resize(function(){
    var leftVal = Number($(window).width()) - Number($('.padder').outerWidth())/2;
    var topVal = Number($(window).height()) - Number($('.padder').outerHeight()))/2;
    $('.padder').css({
        position:'absolute',
        left: leftVal+'px',
        top: topVal+'px'
    });
});