vmitsaras/js-offcanvas

Error "Error: Offcanvas Element not found" when Off Canvas not present

guyhamilton opened this issue · 3 comments

Hi everyone,
First of all great plugin - work's so well and is easily customisable.

I have a site that uses the off canvas menu on some pages and not on others. This is moreso a question for anyone else that has experienced it,

  • but when a page loads without an off canvas menu present it throws a 'Error: Offcanvas Element not found'. Thats understandable, but I'm just wondering whether anyone has found a way to initialise only if there is a off canvas on the page to stop this error? I have tried to do this using the below code but no luck.

I know this isn't a problem with the menu, its more about implementation - but I figured someone might have found a solution so I thought I would ask. Again, awesome plugin!

var $ocRight = $('#off-canvas-right');
if ($ocRight.length) {
$('#off-canvas-right').offcanvas({
modifiers: "right,overlay",
triggerButton: '.js-offcanvas-trigger'
});
}

Hi @guyhamilton,

check for the Button instead for the Menu.

Try this:

<!-- Page with Offcanavs-->
 <a href="#offCanvas" id="triggerButton">Menu</a>   
 <aside id="offCanvas">...</aside>

<!-- Page without Offcanavs-->
 <aside id="offCanvas">...</aside>
var $triggerButton= $('#triggerButton');
if ($triggerButton.length) {
  $('#offCanvas').offcanvas({
    modifiers: "left,overlay",
    triggerButton: '#triggerButton'
  });
}

Hi @guyhamilton,

is this still relevant to you?