Twitter Bootstrap Modal scrolling the page up on show

前端 未结 6 984
日久生厌
日久生厌 2020-12-09 12:12

Clicking on a link which triggers a Twitter Bootstrap Modal to pop up is causing the browser to scroll up the page. This behavior is only occurring in Chrome.

Here

相关标签:
6条回答
  • 2020-12-09 12:33

    I had been having this issue for so long but my solution was to remove href from the anchor tag

    i.e.

    <a onclick="test();return false;">Test Link ###</a>
    

    I get no jump now

    Hope this helps someone in the future

    0 讨论(0)
  • 2020-12-09 12:40

    Same problem. It is caused by "#" in and the anchor is doing to scroll. I fixed that by deleting the anchor tag and place this:

    <span onclick="$('#umisteni_modal').modal('show');" style="cursor:pointer;">Změnit místo dodání</span>
    
    0 讨论(0)
  • 2020-12-09 12:41

    I had a similar issue on my site, which was caused by another jquery plugin (sidr). When the sidebar was present and I opened a modal window, I would be sent back to top.

    In jquery.sidr.js I changed line 102-106 from:

    // Prepare page if container is body
    if($body.is('body')){
    scrollTop = $html.scrollTop();
    $html.css('overflow-x', 'hidden').scrollTop(scrollTop);
    }
    

    To:

    // Prepare page if container is body
    if($body.is('body') && !$('#modal').hasClass('in')){
    scrollTop = $html.scrollTop();
    $html.css('overflow-x', 'hidden').scrollTop(scrollTop);
    }
    

    If you are using this plugin along with twitter bootstrap's modal, maybe this can help you.

    0 讨论(0)
  • 2020-12-09 12:45

    I had the same problem because of these lines of CSS which were thought to permantly display scrollbars in the browser. Removing the two lines solved the problem:

    html,
    body {
        min-height: 100%;
        height: 101%;
    }
    
    0 讨论(0)
  • 2020-12-09 12:52

    Fixed (10/29/2012)

    This issue has been fixed since Bootstrap v2.2.0. See commit e24b46... for details.


    (original answer)

    This is due to a bug in the implementation of the Twitter Bootstrap Modal (as of 2.1.1). The root of the problem is that the focus is set to the modal prior to it completing its transition. This will only occur under the condition that

    1. the browser supports CSS transitions (and you are using bootstrap-transition.js);
    2. the modal has the class fade; and
    3. when focus is set to an element outside the current view, the browser will scroll to move it into view (e.g., Chrome, Safari).

    It should be noted that this affects both the Data API (markup-based) and the Programmatic API (JS-based). However, the reason it is more noticeable when taking the programmatic approach is because, unlike the Data API, it does not automatically restore focus to the triggering element, and, hence, does not scroll the view back down when the modal is hidden.

    More info is available on the Twitter Bootstrap repo under Issue #5336: Modal Scrolls Background Content.

    A fix has been merged into the 2.1.2-wip branch, which should be up for release any day now.

    Here is a demo using the patch bootstrap-modal.js:

    JSFiddle

    0 讨论(0)
  • 2020-12-09 12:56

    to bring the focus back on close

    var triggerOffset=0;
    $('.modal').on('show.bs.modal', function() {
        triggerOffset=$(window).scrollTop();
    });
    $('.modal').on('hidden.bs.modal', function() {
         var animationTime=1;
        $('html, body').animate({
            scrollTop: (triggerOffset)
        }, animationTime);
    });
    
    0 讨论(0)
提交回复
热议问题