JQuery scroll to top of Bootstrap Modal

前端 未结 7 1285
小蘑菇
小蘑菇 2020-12-23 19:37

I\'m currently using the bootstrap modal plugin to display long legal messages on a website I\'m designing, but the problem is that if you open one modal after the other, th

相关标签:
7条回答
  • 2020-12-23 19:46

    Okay I've answered my own question. For anyone else with this issue, simply add this function to your JS!

    $('#modal').on('shown', function () {
        $("#modal-content").scrollTop(0);
    });
    

    I will leave this question up, as there isn't one similar (that I could find) and it may help someone

    0 讨论(0)
  • 2020-12-23 19:46
    1. Scroll up button has a class of: .page-scroll

    enter image description here

    1. Modal with a class of: .modal

    2. JS to make scroll happen with the modal (JS could be simplified):

      $('body').on('click', '.page-scroll', function(event) {
          var $anchor = $(this);
          $('html, body, .modal').stop().animate({
              scrollTop: $($anchor.attr('href')).offset().top
          }, 1500, 'easeInOutExpo');
          event.preventDefault();
      });
      
    0 讨论(0)
  • 2020-12-23 19:47

    On Bootstrap 4 (v4.0.0-alpha.6) the following worked well for me:

    $('#Modal').show().scrollTop(0);
    

    Please note as of bootstrap-4.0.0-beta.1 and Firefox 56.0.1 this does not seem to work correctly;

    I checked IE11, MS Edge and Chrome and this works fine.

    0 讨论(0)
  • 2020-12-23 19:53

    Now with bootstrap 3 the events has change an can be achieved like this (plus a smooth animation to the top)

    $('#modal').on('shown.bs.modal', function () {
        $('#modal').animate({ scrollTop: 0 }, 'slow');
    });
    
    0 讨论(0)
  • 2020-12-23 19:57

    There was change in boostrap where you need to use: on shown.bs.modal

    Call a function when you show the modal window (I opted for this method)

    <button onclick="showSMSSummary(); return false;" 
    data-toggle="tooltip" title="Click To View Summary" 
    class="btn btn-primary btn-sm">SMS Summary</button>
    
    function showSMSSummary()
    {
       $('#HelpScreenModalContent').modal('show');            
       $('#HelpScreenModal').animate({ scrollTop: 0 }, 'fast');
    }
    
    0 讨论(0)
  • 2020-12-23 20:02

    ScrollTop bootbox modal on fadeIn The answer is in this issue.

    When setting up bootbox dialog add .off("shown.bs.modal");this at the end.

    bootbox.dialog({ ... }).off("shown.bs.modal");

    It will scroll be at top when open dialog.

    0 讨论(0)
提交回复
热议问题