Simpledev
Simpledev

Reputation: 647

Twitter bootstrap modal listen when scrollbar at bottom

I'm using Twitter bootstrap modal and I don't know how listen when the scrollbar of the modal reach the bottom of the modal in Javascript or with jQuery.

enter image description here

I'm listening the scroll event for the modal like that :

$('.modal-body').scroll(function() {
    //do something when scrollbar reach the modal's bottom      
});

Thank's for help.

Upvotes: 0

Views: 176

Answers (1)

gaetanoM
gaetanoM

Reputation: 42054

Adding .scrollTop() to .innerHeight() you get where should be the modal body. Compare this value with scrollHeight and that's all:

$('#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/[email protected]/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">&times;</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>

Upvotes: 1

Related Questions