diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/globaleventhandlers/onscroll | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-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.html | 126 |
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"><!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="UTF-8" /> + <style> + #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; + } + + </style> + </head> + <body> + <div id="container"> + <div id="foo"></div> + </div> + + <script type="text/javascript"> + document.getElementById('container').onscroll = function() { + console.log("scrolling"); + }; + </script> + </body> +</html> +</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"><textarea>1 2 3 4 5 6 7 8 9</textarea> +<p id="log"></p></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> |