关闭

跨浏览器用javascript获取窗口的位置和大小

217人阅读 评论(0) 收藏 举报
分类:

跨浏览器获取位置

<code class="hljs coffeescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-reserved" style="box-sizing: border-box;">var</span> leftX = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typeof</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">window</span>.screenLeft == <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'number'</span> ? <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">window</span>.screenLeft : <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">window</span>.screenX;
<span class="hljs-reserved" style="box-sizing: border-box;">var</span> topY = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typeof</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">window</span>.screenTop == <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'number'</span> ? <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">window</span>.screenTop : <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">window</span>.screenY;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

firefox浏览器不支持screenLeft和scrennTop,但是支持screenX和screenY;ie浏览器支持screenLeft和scrennTop,但是不支持screenX和screenY 
跨浏览器获取大小

<code class="hljs coffeescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-reserved" style="box-sizing: border-box;">var</span> width = <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">window</span>.innerWidth;      <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">window</span>.必须有,因为IE不支持
<span class="hljs-reserved" style="box-sizing: border-box;">var</span> height = <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">window</span>.innerHeight;        <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>如果支持inner的,那么就使用它,

<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>不支持的就是用<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">document</span>对象的方法

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typeof</span> width != <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'number'</span>) {
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">document</span>.compatMode == <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'CSS1Compat'</span>) {
        width = <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">document</span>.documentElement.clientWidth; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>标准ie
        height = <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">document</span>.documentElement.clientHeight;
    } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> {   <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>非标准ie
        width = <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">document</span>.body.clientWidth;    
        height = <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">document</span>.body.clientHeight;
    }
}</code>
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:138143次
    • 积分:3055
    • 等级:
    • 排名:第12107名
    • 原创:155篇
    • 转载:114篇
    • 译文:0篇
    • 评论:16条
    最新评论