The problem is offcanvas3 opens behind offcanvas2.
Like that
$("#open-offcanvas2").on("click", function(){
$("#offcanvas2").offcanvas("show")
})
$("#open-offcanvas1").on("click", function(){
$("#offcanvas1").offcanvas("show")
})
$("#open-offcanvas3").on("click", function(){
$("#offcanvas3").offcanvas("show")
})
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<button class="btn btn-primary" type="button" id="open-offcanvas1">Open Offcanvas 1</button>
<div class="offcanvas offcanvas-start" data-bs-scroll="true" tabindex="-1" id="offcanvas1">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasWithBothOptionsLabel">Offcanvas 1</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<button class="btn btn-primary" type="button" id="open-offcanvas2">Open Offcanvas 2</button>
</div>
</div>
<div class="offcanvas offcanvas-start" data-bs-scroll="true" tabindex="-1" id="offcanvas3">
<div class="offcanvas-header">
<h5 class="offcanvas-title">Offcanvas 3</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas"></button>
</div>
<div class="offcanvas-body">
</div>
</div>
<div class="offcanvas offcanvas-start" data-bs-scroll="true" tabindex="-1" id="offcanvas2">
<div class="offcanvas-header">
<h5 class="offcanvas-title">Offcanvas 2</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas"></button>
</div>
<div class="offcanvas-body">
<button class="btn btn-primary" type="button" id="open-offcanvas3">Open Offcanvas 3</button>
</div>
</div>
I tried swapping offcanvas1 and offcanvas2 html codes. And it resolved my issue.
But also it's don't working when there are many offcanvases. Also i don't know if it's a good solution.
How can i resolve it?
Thanks in advance!
I would remove the id's from the buttons.
Changes:
data-target-canvas="#offcanvas2"for example. This can be any valid jQuery selectoroffcanvas-openinstead of an ID - so as illustrated multiple buttons for one thing all work.EDIT: Added a target to the X button (close) to point to the "prior"
Second example; forgo ANY class selectors so all we need to do is add the data attribute to any button etc. and it simple "works".
THIRD example; target the container class selectors so all we need to do is add the data attribute to any container; then hook it to the button classes and it simply "works".