【问题标题】:get center of width in all browsers with jquery使用 jquery 在所有浏览器中获取宽度中心
【发布时间】:2010-12-29 23:40:13
【问题描述】:

我有以下代码来获取宽度中心

  function alertSize() {
     var myWidth = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    } else if( document.documentElement && ( document.documentElement.clientWidth ||        document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
   } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
   //IE 4 compatible
   myWidth = document.body.clientWidth;
  }

return myWidth/2;
}

但它没有返回正确的屏幕中心。

我需要得到正确的水平中心。

谢谢

【问题讨论】:

  • 你的意思可能是“javascript”而不是“java”标签
  • 您是指“屏幕中心”还是“浏览器窗口中心”?为什么提到 jQuery 而您的代码与它无关?我错过了什么吗?

标签: javascript jquery window center


【解决方案1】:

试试这个:

function alertSize() {
 return $(window).width()/2
}

jQuery 已经为你完成了所有额外的工作。

【讨论】:

    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2012-02-06
      • 1970-01-01
      • 2013-06-15
      • 2013-08-10
      相关资源
      最近更新 更多