简体   繁体   English

javascript $(window).width()属性在Internet Explorer中不起作用

[英]javascript $(window).width() property not working in in Internet Explorer

I've following Div with absolute position: 我以绝对位置关注Div:

<div id="target" style="height: 300px; position: absolute; top: 275px;"></div>

What I am doing is calculating the screen horizontal resolution using javascript and then using that obtained width, I am calculating the width and the left position of my "target" div as follows: 我正在做的是使用javascript计算屏幕水平分辨率 ,然后使用获得的宽度来计算“目标” div的宽度和左侧位置,如下所示:

var windowWidth = $(window).width();
var targetWidth = windowWidth * 0.7;
var targetLeftPos = windowWidth * (15 / 100);
$('#target').css({ 'width': targetWidth });
$('#target').css({ 'left': targetLeftPos });

This code working perfectly in all the browsers but in IE this code is not working at all causing the target div not having width and left position style , causing all my designing to be just destructed. 该代码在所有浏览器中均能正常工作 ,但在IE中,此代码根本无法正常工作 ,从而导致目标div 没有宽度和左侧位置样式 ,从而导致我的所有设计都被破坏了。

Is there any way to detect the clients browser and then do the alternate code if the detected browser is IE. 有什么方法可以检测到客户端浏览器,然后在检测到的浏览器是IE时执行备用代码。

Please tell me how should I detect clients browser and what alternate code should I execute for IE. 请告诉我如何检测客户端浏览器,以及应为IE执行哪些替代代码。

Thanks in advance. 提前致谢。

代替$(window).width ,使用

var windowWidth = window.innerWidth;

I figure out the problem... the external jquery plugin stickyscroll.js was causing the problem. 我发现了问题...外部jquery插件stickyscroll.js导致了问题。 It is stopping the execution of all js on the page (happening only in IE). 它正在停止页面上所有js的执行(仅在IE中发生)。 When I removed it, the above code working all the way accurate for IE also.... 当我删除它时,以上代码对于IE也一直有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM