SELECT滚动的Javascript控制 [英] Javascript control of SELECT scrolling

查看:112
本文介绍了SELECT滚动的Javascript控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我不是Javascript专家.我想将一个功能添加到我正在使用的SELECT框中,当添加新项目时,该功能会自动滚动到末尾,但前提是用户没有向上滚动.

Firstly, I am no Javascript expert. I'd like to add a feature to a SELECT box I am using which automatically scrolls to the end when a new item is added, but only if the user has not scrolled up.

基本上,当用户查看较早的条目时,我不希望新来的人员将位置捕捉到选择列表的末尾.但是,如果用户使用滚动条向下滚动到底部,则会自动将新到的物品显示在屏幕上.

Basically, when a user is reviewing older entries, I don't want new arrivals to snap the position to the end of the select list. However, if the user scrolls down to the bottom using the scroll bar, new arrivals shoudl automatically be brought into view.

这是我到目前为止所拥有的

Here is what I have so far

<script type="text/javascript">
  function AddItem(s) 
  {
    document.getElementById('content').add(new Option(s));
    // Scroll to end - cant remember why I am using this pair of commands :)
    document.getElementById('content').selectedIndex = document.getElementById('content').length - 1;
    document.getElementById('content').selectedIndex = document.getElementById('content').length + 1;
  }
</script>

我希望必须进行测试,以查看是否可以看到最后一个项目.如果是这样,我们就在最下面,应该执行自动滚动.这是正确的吗?

I am expecting to have to perform a test to see if the last but one item is visible. If so, we are at the bottom and should perform the autoscroll. Is this correct?

感谢JS大师

西蒙(Simon)

推荐答案

无法通过javascript访问选择框的滚动条,所以我认为您将无法做到这一点.

The scroll bar of a select box isn't accessible via javascript so I don't think you'll be able to do this.

相关: HTML选择框的高度(下拉列表)

您可以看到选择了哪些项目并更改了选择,但是我认为没有任何方法可以显示哪些项目可见.

You can see which items are selected and change the selection but I don't think there's any way to tell which items are visible.

这篇关于SELECT滚动的Javascript控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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