问题
I am generating a select option box dynamically. There can be any number of options. When the user scrolls down to the end of the box, I need to trigger an event, (where I will call the server and populate the select with more options). Its like creating a pagination, but in dropdown box.
Can anybody tell me how can this be done.
So the only thing I need is to trigger an event, when user scrolls to the end of the dropdown. Thats all
<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>
Please don't worry about how I am gonna populate the select box with option, as I am using meteorJs, that I will achieve it easily.
The only requirement is to fire an event.
回答1:
you can try this one, i have created a demo regarding this and generate a popup while scroll at the bottom of list.
Working fiddle demo: 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;
}
回答2:
There is no way to detect scrolling on an actual select
element, so this isn't possible as asked. But you can try creating a custom select-box looking control which is just a scrolling div and apply the feature to it.
UPDATE
DEMO HERE
For the chosen-jquery-plugin
you make events listen as below:
This is how the html
will be generated from chosen
plugin for your select box:
<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>
Write a style for .chosen-drop
and modify your styles as below and remove inline
style from select
.scroll
{
line-height:30px;
width:100px;
}
.chosen-drop
{
overflow-y:scroll;
max-height: 90px;
}
Your JS would be
$('.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');
}
}
来源:https://stackoverflow.com/questions/30797244/detecting-the-end-of-the-scrollable-drop-down