aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/globaleventhandlers/onscroll
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/globaleventhandlers/onscroll
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/globaleventhandlers/onscroll')
-rw-r--r--files/zh-cn/web/api/globaleventhandlers/onscroll/index.html126
1 files changed, 126 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/globaleventhandlers/onscroll/index.html b/files/zh-cn/web/api/globaleventhandlers/onscroll/index.html
new file mode 100644
index 0000000000..83038201e3
--- /dev/null
+++ b/files/zh-cn/web/api/globaleventhandlers/onscroll/index.html
@@ -0,0 +1,126 @@
+---
+title: GlobalEventHandlers.onscroll
+slug: Web/API/GlobalEventHandlers/onscroll
+tags:
+ - API
+ - HTML DOM
+ - 属性
+translation_of: Web/API/GlobalEventHandlers/onscroll
+---
+<div>{{ ApiRef("HTML DOM") }}</div>
+
+<p>元素的 <code>scroll </code>事件处理函数。</p>
+
+<h3 id="语法">语法</h3>
+
+<pre class="syntaxbox">element.onscroll = <var>functionReference</var></pre>
+
+<h3 id="参数">参数</h3>
+
+<p>functionReference 是一个函数的引用。当该元素滚动时,会执行该函数。</p>
+
+<div class="blockIndicator note">
+<p>注意:不要将onscroll 与 {{domxref("GlobalEventHandlers.onwheel", "onwheel")}}<font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">混淆。</span></font>onwheel<font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">是</span></font>鼠标滚轮旋转, 而onscroll 处理的是对象内部内容区的滚动事件。</p>
+</div>
+
+<p> </p>
+
+<h2 id="示例">示例</h2>
+
+<pre class="brush: html">&lt;!DOCTYPE html&gt;
+&lt;html lang="en"&gt;
+ &lt;head&gt;
+ &lt;meta charset="UTF-8" /&gt;
+ &lt;style&gt;
+ #container {
+ position: absolute;
+ height: auto;
+ top: 0;
+ bottom: 0;
+ width: auto;
+ left: 0;
+ right: 0;
+ overflow: auto;
+ }
+
+ #foo {
+ height:1000px;
+ width:1000px;
+ background-color: #777;
+ display: block;
+ }
+
+ &lt;/style&gt;
+ &lt;/head&gt;
+ &lt;body&gt;
+ &lt;div id="container"&gt;
+ &lt;div id="foo"&gt;&lt;/div&gt;
+ &lt;/div&gt;
+
+ &lt;script type="text/javascript"&gt;
+ document.getElementById('container').onscroll = function() {
+ console.log("scrolling");
+ };
+ &lt;/script&gt;
+ &lt;/body&gt;
+&lt;/html&gt;
+</pre>
+
+<p>{{ EmbedLiveSample('示例') }}</p>
+
+<h2 id="Example">Example</h2>
+
+<p>这个示例能说明更多问题</p>
+
+<p>This example monitors scrolling on a {{HtmlElement("textarea")}}, and logs the element's vertical scroll position accordingly.</p>
+
+<h3 id="HTML">HTML</h3>
+
+<pre class="brush: html">&lt;textarea&gt;1 2 3 4 5 6 7 8 9&lt;/textarea&gt;
+&lt;p id="log"&gt;&lt;/p&gt;</pre>
+
+<h3 id="CSS">CSS</h3>
+
+<pre class="brush: css">textarea {
+ width: 4rem;
+ height: 8rem;
+ font-size: 3rem;
+}</pre>
+
+<h3 id="JavaScript">JavaScript</h3>
+
+<pre class="brush: js">const textarea = document.querySelector('textarea');
+const log = document.getElementById('log');
+
+textarea.onscroll = logScroll;
+
+function logScroll(e) {
+ log.textContent = `Scroll position: ${e.target.scrollTop}`;
+}</pre>
+
+<h3 id="Result">Result</h3>
+
+<p>{{EmbedLiveSample("Example", 700, 200)}}</p>
+<h2 id="注意">注意</h2>
+
+<p>当用户滚动某个元素的内容时 <code>scroll</code> 事件将会被触发。<code>Element.onscroll 同等于</code> <code>element.addEventListener("scroll" ... )。</code></p>
+<h2 id="规范">规范</h2>
+
+
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName("DOM3 Events", "#event-type-scroll", "onscroll")}}</td>
+ <td>{{Spec2("DOM3 Events")}}</td>
+ <td>Initial definition</td>
+ </tr>
+ </tbody>
+</table>