Twitter bootstrap 底部滚动条时模式监听
Twitter bootstrap modal listen when scrollbar at bottom
我正在使用 Twitter bootstrap 模态,但我不知道当模态的滚动条到达 Javascript 或 jQuery 的模态底部时如何收听。
我正在监听模态的滚动事件:
$('.modal-body').scroll(function() {
//do something when scrollbar reach the modal's bottom
});
感谢您的帮助。
添加 .scrollTop() to .innerHeight() you get where should be the modal body. Compare this value with scrollHeight 即可:
$('#exampleModalLong .modal-body').on('scroll', function() {
if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
console.log('end reached');
}
})
.modal-body{
height: 250px;
overflow-y: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
我正在使用 Twitter bootstrap 模态,但我不知道当模态的滚动条到达 Javascript 或 jQuery 的模态底部时如何收听。
我正在监听模态的滚动事件:
$('.modal-body').scroll(function() {
//do something when scrollbar reach the modal's bottom
});
感谢您的帮助。
添加 .scrollTop() to .innerHeight() you get where should be the modal body. Compare this value with scrollHeight 即可:
$('#exampleModalLong .modal-body').on('scroll', function() {
if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
console.log('end reached');
}
})
.modal-body{
height: 250px;
overflow-y: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
<p>..............</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>