滚动到列表中的某个项目

发布于 2024-09-14 09:50:56 字数 581 浏览 8 评论 0原文

我有一个元素列表,我希望能够在用户选择项目时成为焦点。我尝试在这些对象上使用 Jquery 调用 .focus() 但无济于事。我有什么遗漏的吗? :)

编辑: 我有一个元素列表 (ul)。我按住 Shift 键并单击起始项,然后按箭头键继续选择。上下。我真的希望班次选择功能就像在 Finder 或 Windows 资源管理器中一样。当选择不可见的项目时向上滚动(在相同情况下向下滚动)。

有没有方便的 jQuery 插件可以帮我处理这个问题?

编辑2: 想通了一些事情,又确定了一个更大的问题。

现在我正在使用名为scrollTo 的jQuery 插件。我可以使用此插件滚动到列表中的某个元素。但是,“滚动”到某个项目意味着它现在是列表中的第一项,这是不可取的行为。在 Finder 或资源管理器中选择任意数量的项目,您会发现当您使用 Shift-箭头键选择屏幕外的项目时,它只会向下滚动一个项目的空间,而不会使其成为顶部元素。

当然很容易修复。 ;) 我目前正在跟踪顶部屏幕外的当前元素和底部屏幕外的列表中的当前元素。然而,当用户在完成一些滚动后从列表中选择一个项目时,我目前无法判断哪些元素只是在视线之外,从而造成了僵局。

有什么想法吗? :P

I have a list of elements I would like to be able to have brought into focus when the user is selecting items. I've tried calling .focus() with Jquery on these objects to no avail. Is there anything I'm missing? :)

EDIT:
I have a list (ul) of elements. I shift click on a starting item and and press the arrow keys to continue selecting. Up and down. I would really like for the shift selection to function as if it was in Finder or Windows Explorer. Scrolling up when an item is selected that's not visible (and down for the same case).

Are there any handy jQuery plugins to handle this for me?

EDIT2:
Figured out some things and determined a larger problem.

Right now I am using the jQuery plugin called scrollTo. I can scroll to an element of my list using this plugin. However, "scrolling" to an item means it is now the first item in the list, which is NOT desirable behavior. Select any number of items in Finder or Explorer and see that when you shift-arrow select an item that's offscreen, it only scrolls down one item's worth of space and does NOT make it the top element.

Easy to fix of course. ;) I am currently keeping track of the current element just off screen on the top and the current element in the list that's just offscreen on the bottom. However, when the user selects an item out of the list after they have done some scrolling, I currently have no way to tell what elements are just out of sight, creating an impasse.

Any ideas? :P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

緦唸λ蓇 2024-09-21 09:50:56

您的问题尚不清楚,但如果您正在谈论自动滚动页面(或元素)以便特定元素位于视口中,您可以尝试 scrollTo 插件

$.scrollTo('#something'); // scrolls the whole page so #something is in view
$('#mydiv').scrollTo('#somethingelse'); // scrolls the (scrollable) div

编辑:您的问题更新可以让您更深入地了解您正在寻找的内容。我不清楚您使用什么方法来实现轮班选择(或者您是否实际上正在扩展问题以询问如何做到这一点)。但假设您仍然只是询问滚动部分,我会在附加到当您将另一个列表项添加到您的选择时触发的事件(自定义?)的函数中添加滚动调用。

例如,假设您每次选择

  • 时都会触发一个名为 list_expanded 的自定义事件,您可以尝试如下操作:
  • $(document).ready( function(){
      $('ul#mylist').delegate('li','list_expanded', function(){
        $(this).scrollTo();
      });
    });
    

    显然您也可以使用“常规”事件,例如点击,具体取决于多选解决方案的工作方式。

    It's not clear from your question, but if you're talking about automatically scrolling the page (or element) so that a particular element is in the viewport, you might try the scrollTo plugin.

    $.scrollTo('#something'); // scrolls the whole page so #something is in view
    $('#mydiv').scrollTo('#somethingelse'); // scrolls the (scrollable) div
    

    Edit: Your question update provides a little more insight into what you're looking for. I'm not clear on what method you're using to achieve the shift-selection (or if you're actually expanding the question to ask how to do that). But assuming you're still just asking about the scrolling piece, I would add the scrolling call in a function attached to whichever event (custom?) is fired when you add another list item to your selection.

    For example, assuming you're triggering a custom event called list_expanded each time an <li> is selected, you could try something like this:

    $(document).ready( function(){
      $('ul#mylist').delegate('li','list_expanded', function(){
        $(this).scrollTo();
      });
    });
    

    Obviously you can also use a "regular" event like click, depending on how your multiple-select solution works.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文