【问题标题】:detecting the end of the scrollable drop down检测可滚动下拉列表的结尾
【发布时间】:2015-08-28 02:52:32
【问题描述】:

我正在动态生成一个选择选项框。可以有任意数量的选项。当用户向下滚动到框的末尾时,我需要触发一个事件(我将在其中调用服务器并使用更多选项填充选择)。这就像创建一个分页,但在下拉框中。

谁能告诉我如何做到这一点。

所以我唯一需要做的就是在用户滚动到下拉列表末尾时触发一个事件。就这些

<select style="height: 30px;line-height:30px;" class="scroll">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
</select>

请不要担心我将如何使用选项填充选择框,因为我使用的是 meteorJs,我会轻松实现它。

唯一的要求是触发一个事件。

【问题讨论】:

    标签: javascript jquery meteor jquery-chosen


    【解决方案1】:

    无法检测到实际 select 元素上的滚动,因此无法按要求执行此操作。但是您可以尝试创建一个自定义的选择框外观控件,该控件只是一个滚动 div,并将该功能应用于它。

    更新

    DEMO HERE

    对于chosen-jquery-plugin,您让事件监听如下:

    这就是html 将如何从chosen 插件为您的选择框生成:

    <div class="chosen-container chosen-container-single chosen-container-active" style="width: 100px;" title="">
        <a class="chosen-single" tabindex="-1">
            <span>1</span>
            <div><b></b></div>
        </a>
        <div class="chosen-drop">
            <div class="chosen-search">
                <input type="text" autocomplete="off"/>
            </div>
            <ul class="chosen-results">
                <li class="active-result result-selected" data-option-array-index="0">1</li>
                <li class="active-result" data-option-array-index="1">2</li>
                <li class="active-result" data-option-array-index="2">3</li>
                <li class="active-result" data-option-array-index="3">4</li>
                <li class="active-result" data-option-array-index="4">5</li>
                <li class="active-result" data-option-array-index="5">6</li>
                <li class="active-result" data-option-array-index="6">7</li>
                <li class="active-result" data-option-array-index="7">8</li>
            </ul>
        </div>
    </div>
    

    .chosen-drop 编写一个样式并修改您的样式如下并从select 中删除inline 样式

    .scroll
    {
        line-height:30px;
        width:100px;
    }
    
    .chosen-drop
    {
        overflow-y:scroll;
        max-height: 90px;
    }
    

    你的 JS 会是

    $('.chosen-drop').on('scroll',chk_scroll);
    
    function chk_scroll(e)
    {
        var elem = $(e.currentTarget);
        console.log(elem[0].scrollHeight-elem.scrollTop());
        console.log(elem.outerHeight());
        if (elem[0].scrollHeight - elem.scrollTop() <= elem.outerHeight()) 
        {
            alert('bottom');
        }
    }
    

    【讨论】:

    • 正如我所说,您需要创建一个自定义的selectbox,将其替换为divs,就像selectbox 的大多数插件一样,或者只是选择任何插件......就像其中一些here
    • 在上面的 html 代码中,你的类在哪里被称为“滚动”。它只是在css中提到但没有在html中显示?
    • 它是你选择的班级伙伴.. :)
    【解决方案2】:

    你可以试试这个,我已经创建了一个关于这个的演示,并在列表底部滚动时生成一个弹出窗口。

    工作小提琴演示:https://jsfiddle.net/j68o44Ld/

    <div class="subtask-li">
    <span class="main-tlist"> 
    <span class="icon-taskListing"></span> 
    <span class="subselectedList">Default</span> 
    <span class="icon-subcaret"></span> </span>
    <ul class="subtask-pick">
    <li><a href="javascript:;">General issues</a></li>
    <li><a href="javascript:;">Default</a></li>
    <li><a href="javascript:;">Android Games Issues</a></li>
    <li><a href="javascript:;">pt issues</a></li>
    <li><a href="javascript:;">Server Development</a></li>
    <li><a href="javascript:;">Resource Integration</a></li>
    <li><a href="javascript:;">Server Infrastructure</a></li>
    </ul>
        </div>
    
        $(document).on("click",".main-tlist",function(){
        $('.subtask-pick').toggle();
        });
    
        $('.subtask-pick').scroll(function () {
              if ($(this)[0].scrollHeight - $(this).scrollTop() <=  $(this).outerHeight()) {
                    alert("end of scroll");
                  // You can perform as you want
    
              }
        });
    
    
    
      .subtask-li {
            border: 1px solid #dfe8f1;
            cursor: pointer;
            position: relative;
            background: rgba(0, 0, 0, 0) linear-gradient(to bottom, rgba(255, 255, 255, 1) 0%, rgba(246, 246, 246, 1) 100%) repeat scroll 0 0;
            border-radius: 3px;
            float: left;
    
            left: 5px;
            padding: 5px;
            top: 6px;
        }
    
    
        .subtask-pick{
           background-clip: padding-box;
            background-color: #fff;
            border: 1px solid #dfe8f1;
            border-radius: 3px;
            box-shadow: 0 1px 7px 2px rgba(135, 158, 171, 0.2);
            display: none;
            list-style: outside none none;
            padding: 0 0 10px;
            position: absolute;
            z-index: 9; 
            float: left;
           width: 220px;
            list-style: outside none none; height:220px; overflow:auto;
        }
        .icon-taskListing {
            display: inline-block;
            vertical-align: middle;
        }
        .subselectedList {
            overflow: hidden;
            padding: 0 5px;
            text-overflow: ellipsis;
            white-space: nowrap;
            width: 116px;
        }
        ul.subtask-pick li {
          float: none;
          display: block;
          clear: both;
    
          position: relative;
        }
        ul.subtask-pick li a {
          padding: .9em 1em .9em .8em;
          position: relative;
            clear: both;
          cursor: pointer;
          display: block;
    
          white-space: nowrap;
           text-overflow: ellipsis;
          overflow: hidden;
        }
        ul.subtask-pick li a:hover {
          background: none repeat scroll 0 0 #eff4f6;
          cursor: pointer;
        }
        a {
          text-decoration: none;
          outline: 0;
          color: #4c4c4c;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多