aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/window/screenleft/index.html
blob: a3a64a5164a8a45e4af53156aa5ddd1283e2507b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
---
title: Window.screenLeft
slug: Web/API/Window/screenLeft
translation_of: Web/API/Window/screenLeft
---
<div>{{APIRef}}</div>

<p><code><strong>Window.screenLeft</strong></code> 是一个只读属性,它返回浏览器左边框到左边屏幕边缘的 CSS 像素数。</p>

<div class="blockIndicator note">
<p><strong>注意</strong><code>screenLeft</code> 只是 {{domxref("Window.screenX")}}  属性的别名,最初只被 IE 浏览器所支持。现在主流的浏览器都已支持该属性。</p>
</div>

<h2 id="Syntax" name="Syntax">语法</h2>

<pre class="syntaxbox"><em>leftWindowPos</em> = window.screenLeft
</pre>

<h3 id="返回值">返回值</h3>

<p>返回浏览器窗口到屏幕左边缘的 CSS 像素距离数值。</p>

<h2 id="Specifications" name="Specifications">例子</h2>

<p>在 <a href="https://mdn.github.io/dom-examples/screenleft-screentop/">screenleft-screentop</a> 项目中,展示了一个监听浏览器窗口位置变化,动态更新 canvas 的例子。在这个例子中,当你移动浏览器窗口位置,绘制在 canvas 上的圆也会对应移动。我们通过监听 <code>screenLeft</code>/<code>screenTop</code> 的变化,并使用 {{domxref("Window.requestAnimationFrame()")}} 来对 canvas 实时重绘,保证了浏览器窗口移动时,canvas 内部圆的位置也会发生对应的移动。</p>

<pre class="brush: js">initialLeft = window.screenLeft + canvasElem.offsetLeft;
initialTop = window.screenTop + canvasElem.offsetTop;

function positionElem() {
  let newLeft = window.screenLeft + canvasElem.offsetLeft;
  let newTop = window.screenTop + canvasElem.offsetTop;

  let leftUpdate = initialLeft - newLeft;
  let topUpdate = initialTop - newTop;

  ctx.fillStyle = 'rgb(0, 0, 0)';
  ctx.fillRect(0, 0, width, height);
  ctx.fillStyle = 'rgb(0, 0, 255)';
  ctx.beginPath();
  ctx.arc(leftUpdate + (width/2), topUpdate + (height/2) + 35, 50, degToRad(0), degToRad(360), false);
  ctx.fill();

  pElem.textContent = 'Window.screenLeft: ' + window.screenLeft + ', Window.screenTop: ' + window.screenTop;

  window.requestAnimationFrame(positionElem);
}

window.requestAnimationFrame(positionElem);</pre>

<p>如果浏览器不支持 <code>screenLeft</code>属性,我们还在代码中使用了一个 polyfill 来保证演示效果。通过设置 {{domxref("Window.screenX")}}/{{domxref("Window.screenY")}} 为对应别名来实现一样的功能。</p>

<pre class="brush: js">if(!window.screenLeft) {
  window.screenLeft = window.screenX;
  window.screenTop = window.screenY;
}</pre>

<h2 id="Specifications" name="Specifications">说明</h2>

<table class="standard-table" style="height: 49px; width: 1000px;">
 <thead>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">提交</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{ SpecName('CSSOM View', '#dom-window-screenx', 'Window.screenLeft') }}</td>
   <td>{{ Spec2('CSSOM View') }}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("api.Window.screenLeft")}}</p>

<h2 id="参考">参考</h2>

<ul>
 <li>{{domxref("window.screenTop")}}</li>
 <li>{{domxref("Window.screenX")}}</li>
</ul>