简体   繁体   English

jQuery计算项目在可滚动窗口中的位置并滚动到它

[英]jQuery calculate position of item in scrollable window and scroll to it

there. 那里。 I have a list of items (users) in a popup. 我在弹出窗口中有一个项目(用户)列表。 This list is in a scrollable div. 此列表位于可滚动的div中。 I want to make a search box that, using jQuery and Javascript, calculates the position of the desired user in this list and then scrolls to it. 我要创建一个搜索框,使用jQuery和Javascript计算所需用户在此列表中的位置,然后滚动到该位置。 Basically, this is what I want: 基本上,这就是我想要的:

function goToUser(userName) {
    var userPosition = getPosition(userName);
    $('#myContainer').scrollTop(userPosition);
}

function getPosition(userName){
    // ?????
}

Anyone had such a problem before ? 有人以前有过这样的问题吗? Thank you. 谢谢。

This is the partial view I use for the popup: 这是我用于弹出窗口的局部视图:

@using (@Html.BeginForm())
{
<div class="popupTitle">
    Choose user(s)
</div>
<div style="height: 400px; overflow: scroll" class="popupNotifications">
    @foreach (var user in Model.Users)
    {
        <div>
            <input id="Users-@user.id" name="targetIds" type="checkbox" value="@user.id" @if(Model.TargetIds != null && Model.TargetIds.Contains(user.id)){<text>checked="checked"</text>} />
            <label for="Users-@user.id" style="cursor: pointer;">@user.name</label>
        </div> 
    }
</div>
<div class="popupButtons">
    <input type="button" class="button" value="Save" onclick="GetValues(Notifications_UsersPopupHolder)" />
    <input type="button" class="button" value="Cancel" onclick="Cancel(Notifications_UsersPopupHolder)" />
</div>

} }

Have you tried $(element).scrollIntoView() ? 您是否尝试过$(element).scrollIntoView()

I think your code would look like this (no need for a getPosition function): 我认为您的代码将如下所示(不需要getPosition函数):

function goToUser(userName){
    $('div.popupNotifications input#' + username).closest('div').scrollIntoView();
}

如果列表中所有项目的高度均相同,则可以通过(索引+ 1)*项目高度来计算。

You can use the offset().top of the specific element. 您可以使用特定元素的offset().top

function goToUser(userName) {
    var userPosition = getPosition(userName);
    $('.popupNotifications').scrollTop(userPosition);
}

function getPosition(userName){
    return $('.popupNotifications label:contains(' + userName + ')').offset().top;
}

Example fiddle 小提琴的例子

You could assign the element containing your user an ID or some class that identifies it (either id="user1" or class="user1") 您可以为包含用户的元素分配一个ID或标识它的某个类(id =“ user1”或class =“ user1”)

Then you can simply use 然后,您可以简单地使用

$('#elementcontainingusers').scrollTop($('#user1').position().top);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 window 不可滚动且滚动条被隐藏时,如何获取元素的滚动 position? - How to get scroll position of an element when window is not scrollable and scrollbar is hidden? jQuery获取窗口位置+滚动-但如果不滚动则不显示 - jquery get window position + scroll - but if not scroll not show JQuery UI可在可滚动容器中排序 - 在排序时滚动位置“跳转” - JQuery UI sortable in a scrollable container - scroll position “jumps” when sorting jQuery-窗口滚动后可拖动位置问题 - jQuery - draggable position issue after window scroll 在 Jquery 中的 window 滚动上将元素 Position 调整为 0? - Adjust Element Position to 0 on window scroll in Jquery? 如何计算将窗口滚动到Java中图标中心位置的坐标? - How to calculate coordinates to scroll window to icon center position in Javascript? 固定标题可滚动表-如何在页面加载时使用CSS jQuery保留水平滚动位置 - Fixed Header Scrollable Table - How to preserve horizontal scroll position using css jquery on page loads 向下滚动jQuery时如何获取浏览器屏幕窗口的位置 - How to get the position of screen window of browser when scroll down jquery jQuery单击内部链接后获取窗口滚动位置 - jquery get window scroll position after clicking on internal link 如何在可滚动容器中滚动 position - How to get scroll position in scrollable container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM