具有“overflow:auto”的div的滚动位置 [英] Scroll Position of div with "overflow: auto"

查看:827
本文介绍了具有“overflow:auto”的div的滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定此HTML代码段:

Given this HTML snippet:

<div id="box" style="overflow:auto; width:200px; height:200px; border:1px solid black;">
1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>
11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<br>20<br>
21<br>22<br>23<br>24<br>25<br>26<br>27<br>28<br>29<br>30<br>
</div>

你通常会得到一个带有滚动条的黑盒子,其中包含数字1到30,一个新行。

You (usually) get a black box with a scrollbar that contains the numbers 1 to 30, each in a new row.

您可以在该框内向上和向下滚动。

You can scroll up and down inside of that box.

找到的可能性,框在哪个滚动位置。让我们说,每一行都是15像素高,你向下滚动到10,我想得到的结果是数字150(15px * 10行)。

What I need now is a possibility to find out, at which scroll position the box is. Let's just say, each row was 15 pixel high and you scroll down to the number 10, the result I'd like to get is the number 150 (15px * 10 lines).

我在哪里可以找到这个号码?

Where do I find this number?

我手上有纯粹的JavaScript和jQuery。

I have plain JavaScript and jQuery at my hands.

推荐答案

您需要使用 scrollTop 属性。

You need to use the scrollTop property.

document.getElementById('box')。scrollTop

这篇关于具有“overflow:auto”的div的滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆