ปัญหา Bootstrap Modal - การเลื่อนถูกปิดใช้งาน


93

ฉันมีโมดอลและภายในโมดอลนั้นมีดรอปดาวน์ที่แสดงเนื้อหาที่ซ่อนอยู่ขนาดใหญ่ซึ่งใช้งานได้

ตอนนี้เมื่อคุณเปิดโมดอลถัดไปวางซ้อนกันด้านบนของโมดอลแรกและปิดการเลื่อนบนโมดอลด้านล่างจะปิดใช้งาน

ฉันได้สร้างตัวอย่างฉบับเต็มรวมถึงขั้นตอนในการจำลองปัญหาที่ฉันกำลังเผชิญอยู่ คุณสามารถดูได้ที่นี่

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
    <title></title>
    <style>

    </style>
</head>
<body>


    <input type="button" data-toggle="modal" data-target="#modal_1" class="btn btn-lg btn-primary" value="Open Modal 1" >

    <div class="modal fade" id="modal_1">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title">First Modal</h4>
                </div>
                <div class="modal-body">



                    <form class="form">

                        <div class="form-group">
                            <label>1) Open This First: </label>
                            <input type="button" data-toggle="modal" data-target="#modal_2" class="btn btn-primary" value="Open Modal 2" >
                        </div>

                        <div class="form-group">
                            <label>2) Change this once you have opened the modal above.</label>
                            <select id="change" class="form-control">
                                <option value="small">Show Small Content</option>
                                <option value="large">Show Large Content</option>
                            </select>
                        </div> 

                        <div id="large" class='content-div'>
                            <label>Large Textarea Content.. Try and scroll to the bottom..</label>
                            <textarea rows="30" class="form-control"></textarea>

                        </div>

                        <div id="small" class='content-div'>
                            <label> Example Text Box</label> 
                            <input type="text" class="form-control">
                        </div>  


                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>


    <div class="modal fade" id="modal_2">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title">Second Modal</h4>
                </div>
                <div class="modal-body">

                    <hp>This is the stacked modal.. Close this modal, then chenge the dropdown menu, you cannot scroll... </h5>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>


</body>
<script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js" type="text/javascript"></script>
<script>

    $(document).ready(function() {


        $(".content-div").hide();
        $("#change").change(function() {
            $(".content-div").hide();
            $("#" + $(this).val()).show();
        });


    });

</script>
</html>

นี่คือ Bootply เพื่อแสดงพฤติกรรมในการดำเนินการ:

Bootply



ดูstackoverflow.com/a/24914782/58241ซึ่งมีการแก้ไขไม่เพียง แต่สำหรับปัญหาแถบเลื่อนเท่านั้น แต่ยังรวมถึงปัญหาการจัดแต่งทรงผมด้วย
benrwb

คำตอบ:


121

นี่ยังเป็นทางออก

.modal {
  overflow-y:auto;
}

http://www.bootply.com/LZBqdq2jl5

อัตโนมัติทำงานได้ดี :) สิ่งนี้จะแก้ไขโมดอลที่ทำงานสูงกว่าขนาดหน้าจอของคุณ


5
วิธีนี้ช่วยแก้ปัญหาของฉันได้ในขณะที่ปิดและเปิด modals จาก jQuery$('.modal').css('overflow-y', 'auto');
Gla

ผู้ช่วยชีวิต) ขอบคุณ
Victor Gorban

67

เนื่องจากเมื่อคุณปิดโมดอลมันจะลบออกmodal-openจาก<body>แท็ก Bootstrap ไม่รองรับหลาย modals ในหน้าเดียวกัน (อย่างน้อยก็จนถึง BS3)

วิธีหนึ่งที่จะทำให้มันใช้งานได้คือใช้hidden.bs.modalเหตุการณ์ที่เรียกโดย BS เมื่อปิดโมดอลจากนั้นตรวจสอบว่ามีโมดอลอื่นเปิดอยู่หรือไม่เพื่อบังคับให้modal-openคลาสในร่างกาย

// Hack to enable multiple modals by making sure the .modal-open class
// is set to the <body> when there is at least one modal open left
$('body').on('hidden.bs.modal', function () {
    if($('.modal.in').length > 0)
    {
        $('body').addClass('modal-open');
    }
});

11
(BS4) คำตอบแรกที่ฉันสามารถทำงานได้เพียงแค่ต้องใช้ ($ ('. modal.show'). length> 0) สำหรับ Bootstrap 4
Shoejep

ขอบคุณ - ปัญหาของฉันไม่ค่อยเหมือนกัน แต่ความคิดเห็นของคุณชี้ให้ฉันไปที่คลาส modal-open ในร่างกายซึ่งเป็นสาเหตุของการเลื่อนหน้าหาย
จอน

38

ฉันได้พบทางออกสำหรับคุณแล้ว ฉันไม่แน่ใจว่าทำไมมันถึงใช้ไม่ได้ แต่โค้ดเพียงบรรทัดเดียวใน CSS ของคุณจะช่วยแก้ปัญหาได้ หลังจากปิดโมดอลที่สองอันแรกกำลังได้รับoverflow-y:hiddenอย่างใด แม้ว่าจะตั้งค่าเป็นautoจริงก็ตาม

คุณต้องเขียนสิ่งนี้ใหม่และตั้งค่าการประกาศของคุณเองใน CSS:

#modal_1 {
    overflow-y:scroll;
}

ที่นี่คุณมีการสาธิตที่ใช้งานได้

แก้ไข : ลิงค์ผิดขออภัย


ไม่ทำงานทำให้ตู้คอนเทนเนอร์เลื่อนได้ แต่ทำให้ล้อเมาส์ติดด้วย
Kiran Maniya

20
$(document).ready(function () {

 $('.modal').on("hidden.bs.modal", function (e) { //fire on closing modal box
        if ($('.modal:visible').length) { // check whether parent modal is opend after child modal close
            $('body').addClass('modal-open'); // if open mean length is 1 then add a bootstrap css class to body of the page
        }
    });
});
//this code segment will activate parent modal dialog 
//after child modal box close then scroll problem will automatically fixed

3
IMO นี่คือคำตอบที่ดีที่สุด การเพิ่ม css พิเศษสำหรับ overflow-y: auto ก็ใช้งานได้เช่นกัน แต่ตามที่กล่าวไว้ด้านล่างส่งผลให้แถบเลื่อนคู่
Jacob Rutherford

2
ใช้งานได้กับ Bootstrap 4.1 ด้วย ชื่นชม
Willi

2
เห็นด้วย ทดสอบ 2-3 คำตอบและคำตอบนี้ใช้ได้กับ Twitter Bootstrap 4.2.1
Tpojka

11

ติดตามhttps://github.com/nakupanda/bootstrap3-dialog/issues/70แอด

.modal { overflow: auto !important; }

ไปยัง css ของคุณ


2
วิธีนี้จะสร้างแถบเลื่อนสองแถบ
Weijing Jay Lin

มีวิธีแก้ปัญหามากมายที่พวกเขาบอกว่าใช้งานได้ฉันเลือกอันนี้เพราะความเรียบง่าย ทำงานได้อย่างสมบูรณ์แบบสำหรับฉัน โหวตแล้ว
Kapitan

9

สำหรับ bootstrap 4 ฉันต้องเพิ่ม!

.modal {
    overflow-y: auto !important;
}

หากไม่ทำเช่นนี้จะไม่ได้ผลและไม่ได้ใช้

ภาพหน้าจอ


สำหรับฉันแล้วมันเป็นวิธีแก้ปัญหาที่ใช้งานได้ครึ่งหนึ่งเพราะฉันต้องคลิกที่ความสามารถในการเลื่อนของ modal te re-gain โซลูชัน @ashish dwivedi ทำงานได้อย่างสมบูรณ์แบบสำหรับฉัน
Matthieu Charbonnier

6

ก่อนอื่น Bootstrap ไม่รองรับโมดอลแบบเปิดหลายตัว ดูที่นี่: เอกสาร Bootstrap Modal

ไม่รองรับรูปแบบการเปิดหลายรายการ อย่าเปิดโมดอลในขณะที่อีกโมดอลยังมองเห็นได้ การแสดงโมดอลมากกว่าหนึ่งครั้งต้องใช้โค้ดที่กำหนดเอง

แต่ถ้าจำเป็นคุณสามารถเพิ่ม CSS นี้ในสไตล์ชีตที่กำหนดเองได้ตราบใดที่มันรวมอยู่หลังสไตล์ชีต Bootstrap หลัก:

.modal {
    overflow-y:auto;
}

5

Bootstrap ไม่รองรับหลายรูปแบบในเวลาเดียวกัน มันคือ Bootstrap Bug เพียงเพิ่มสคริปต์ด้านล่างสำหรับ BS4

$('body').on('hidden.bs.modal', function () {
if($('.modal.show').length > 0)
{
    $('body').addClass('modal-open');
}
});

ใช้งานได้ดี แต่อย่าลืมใส่รหัสนี้ไว้หลังโมดอลของคุณหรือใน$(document).readyวิธีการ
Matthieu Charbonnier


3

ฉันแก้ไขปัญหาโดยการลบdata-dismiss="modal" และเพิ่ม

setTimeout(function(){ $('#myModal2').modal('show') }, 500);
$('#myModal1').modal('hide');

หวังว่าจะช่วยได้


ขอบคุณ @ ซันนี่ฉันไม่รู้ว่าทำไมวิธีแก้ปัญหาอื่นไม่ได้ผลสำหรับฉัน แต่อันนี้ใช้งานได้แน่นอน
dev-masih

@MasihAkbari อาจเป็นปัญหารุ่น :) อันนี้ช่วยฉันด้วย ฉันใช้ BS4 อาจมีปัญหาด้านประสิทธิภาพเล็กน้อยเนื่องจากความล่าช้า แต่เป็นการแก้ไขที่ง่ายอย่างรวดเร็ว
Weijing Jay Lin

2

ฉันคิดว่านี่เป็นเพราะข้อผิดพลาดใน twitter bootstrap ดูเหมือนว่าจะลบmodal-openคลาสออกจากร่างกายแม้ว่าจะเปิดสองโหมด คุณสามารถทดสอบการremoveClassทำงานของ jQuery และข้ามไปได้หากยังมีโมดอลที่ยังเปิดอยู่ (เครดิตสำหรับฟังก์ชันพื้นฐานไปที่คำตอบนี้: https://stackoverflow.com/a/1950199/854246 )

(function(){
    var originalRemoveClassMethod = jQuery.fn.removeClass;

    jQuery.fn.removeClass = function(){
        if (arguments[0] === 'modal-open' && jQuery('.modal.in').length > 1) {
            return this;
        }
        var result = originalRemoveClassMethod.apply( this, arguments );
        return result;
    }
})();


1

รหัสนี้แก้ไขของฉันเมื่อปิดป๊อปอัปครั้งที่สองป๊อปอัปแรกของฉันจะไม่สามารถเลื่อนได้อีกต่อไป

$('body').on('hidden.bs.modal', '#SecondModal', function (e) {
  if ($('#FirstModal').is(':visible')) { // check if first modal open 
    $('body').addClass('modal-open'); // re-add class 'modal-open' to the body because it was removed when we close the second modal
    $('#FirstModal').focus(); // Focus first modal to activate scrollbar
  }
});

1

เป็นแฮ็ก แต่ฉันต้องการเพียงแค่ปิดและเปิดใหม่เมื่อมีกิริยาใกล้เคียงเพื่อกำจัดพฤติกรรมแปลก ๆ / ไม่ต้องการ หากคุณปิดการเลือนหายคุณจะไม่สังเกตเห็นว่ามันปิดและเปิดใหม่:

var $ModalThatWasOnTop= $('#ModalThatWasOnTop')

$ModalThatWasOnTop.on('hidden.bs.modal', function(e) {
     console.log('closing  modal that was on top of the other modal')
     $('#ModalThatWasCovered').modal('hide');
     $('#ModalThatWasCovered').modal('show');

});

0

เพิ่มผ่าน JQuery คลาส 'modal-open' ให้กับร่างกาย ฉันคิดว่าการเลื่อนใช้ได้กับทุกอย่างยกเว้นโมดอล

ตามความคิดเห็นของการตอบกลับก่อนหน้านี้: การเพิ่มคุณสมบัติล้นลงในโมดอล (ผ่าน CSS) ช่วยได้ แต่คุณจะมีแถบเลื่อนสองแถบในหน้า



0

ฉันได้หาวิธีแก้ปัญหานี้แล้ว คุณต้องเปิดใช้งานการเลื่อนสำหรับส่วนเนื้อหาของหน้าหากคุณใช้$('#myModal').hide();เพื่อซ่อนโมเดล เพียงเขียนบรรทัดต่อไปนี้หลัง$('#myModal').hide();:

$('body').attr("style", "overflow:auto")

การดำเนินการนี้จะเปิดใช้งานการเลื่อนอีกครั้ง


0

หากคุณใช้ bootstrap3 หรือ 4 ตรวจสอบให้แน่ใจว่าคุณมี class modal-open ใน html body ฉันไม่แน่ใจว่านี่เป็นข้อบังคับหรือไม่ แต่อาจตรวจสอบให้แน่ใจว่าได้ล้างดัชนี z ของ modal-backdrop แล้ว

    $('.your-second-modal').on('hidden.bs.modal', function (e) {
        $('.your-second-modal').css('z-index', "");
        $('.modal-backdrop').css('z-index', 1040);
        $('body').addClass("modal-open");
    });

0

เพิ่มโค้ด js ด้านล่าง

    $(document).on("click",".modal",function () {
       if(!$("body").hasClass('modal-open'))
           $("body").addClass('modal-open');
    });

0

ง่ายๆถ้าคุณใช้โมดอล bootstrap มากกว่าที่คุณต้องการลบ tabindex = "- 1" แทนสิ่งนี้

** วาง **
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.