aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/window/screentop/index.html
blob: 35067bb48db0f0d01e539215eca95c02a4237413 (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.screenTop
slug: Web/API/Window/screenTop
translation_of: Web/API/Window/screenTop
---
<p>{{APIRef}}</p>

<p><code><strong>Window.screenTop</strong></code> 只读属性返回垂直距离,单位是CSS 像素, 从用户浏览器的上边界到屏幕最顶端。</p>

<div class="blockIndicator note">
<p><strong>Note</strong>: <code>screenTop</code> is an alias of the older {{domxref("Window.screenY")}} property. <code>screenTop</code> was originally supported only in IE but was introduced everywhere due to popularity.</p>
</div>

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

<pre class="syntaxbox"><em>topWindowPos</em> = window.screenTop
</pre>

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

<p>A number equal to the number of CSS pixels from the top edge of the browser viewport to the  top edge of the screen.</p>

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

<p>In our <a href="https://mdn.github.io/dom-examples/screenleft-screentop/">screenleft-screentop</a> example, you'll see a canvas onto which has been drawn a circle. In this example we are using <code>screenLeft</code>/<code>screenTop</code> plus {{domxref("Window.requestAnimationFrame()")}} to constantly redraw the circle in the same physical position on the screen, even if the window position is moved.</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>Also in the code we include a snippet that detects whether <code>screenLeft</code> is supported, and if not, polyfills in <code>screenLeft</code>/<code>screenTop</code> using {{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">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{ SpecName('CSSOM View', '#dom-window-screeny', 'Window.screenTop') }}</td>
   <td>{{ Spec2('CSSOM View') }}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

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



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

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

<ul>
 <li>{{domxref("window.screenLeft")}}</li>
 <li>{{domxref("Window.screenY")}}</li>
</ul>