4

I followed the sidr documentation at: http://www.berriart.com/sidr/

And I already have my sidr side left menu working fine.

But on my mobile,only on android default browser, when I click in my link "Open Menu" I also click on my menu item "Menu 1", and so it opens my submenu items with my toggle effect. And I dont want this.

I just want to open my submenu items when I click in my Menu items, and not in my link to open the menu.

I found a solution, that is, if I put my sidr menu with some margin top, to not align with my link to open the menu, the problem is solved, like in my second image.

But I dont want to give that margin-top, so Im trying look for other solution.

Somebody there have exprience with this plugin and can give me a help??

(This only happens in mobile and on android browser that cames when you buy the smartphone, but I want to use this on mobile, and many users must use internet explorer which I think is the default browser for android.)

Like this image below, I have the problem, because the "Open Menu is aligned with "Menu 1" and so Im clicking on both!

enter image description here

Like this image below, I dont have the problem, because the "Open Menu is not aligned with "Menu 1" and so I only click on "Open Menu"!

enter image description here

This is my jQuery to start sidr plugin:

 $(document).ready(function() {
      $('#simple-menu').sidr({
           name: 'sidr', 
          speed: 200, 
          side: 'left',
          source: null, 
          renaming: true, 
          body: 'body'

         });
    });

$(document).ready(function() {
    $('.sub-menu-sidr').hide();

    $("#sidr li:has(ul)").click(function(){

    $("ul",this).toggle('fast');
    });
});

And here is my fiddle:

http://jsfiddle.net/y4CX4/1/

Már Örlygsson
  • 14,176
  • 3
  • 42
  • 53
OzzC
  • 815
  • 4
  • 20
  • 36
  • In my computer is Internet explorer 10, in android it is the default browser, I think it is IE. I already tested in two different androids and just on this browser I have problems when I click to open the side menu! – OzzC May 11 '14 at 15:13
  • 2
    IE is not available for Android at all. – Lee Taylor May 13 '14 at 02:34
  • That jsfiddle works fine on my android chrome browser. –  May 13 '14 at 16:28
  • Yes jbyrne2007, in my android chrome browser works also fine, but on the other browser that I think its the default browser of android when you buy one, dont works fine! – OzzC May 13 '14 at 17:39

2 Answers2

3

Easiest way to do that, IMHO is to prevent the first click on that link from happening, that is:

Define a variable to check if link was clicked, at click event check the value and prevent the event from propagating and then set the variable to something else, in order to allow all future clicks to happen naturally, for example:

var click = false;
$('#sidr > ul > li').first().find('a').first().click( function(e) { if ( click == false ) {
    e.stopPropagation();
    click = true;
} });

The next step would be to add a function that resets this variable when the menu gets closed by adding:

      onClose : function() {
          click = false;
      }

An working example can be found here: http://jsfiddle.net/y4CX4/3/

Also make sure you use the same function in order to use the variable click properly ( in the fiddle you posted you used $(document).ready() two times for some reason ).

Mircea Sandu
  • 926
  • 7
  • 21
  • Thank you for your answer, but your jsfiddle works on desktop, on mobile, Im testing, and When I click in "Open Menu" nothing happens :/, the menu is not opening. – OzzC May 14 '14 at 22:17
  • I need to add this: -webkit-overflow-scrolling: touch; to my .sidr, and now its working, but the problem of my questions still remains on android default browser! – OzzC May 14 '14 at 22:29
  • What do you mean about the problem of the question? This is just a simple solution to what seems a problem that should be researched further. It just cancels the first click on the first element when you open the side menu, thus making it feel like it is working properly, you should use some kind of verification in order to use this only on mobile devices ( see http://modernizr.com/ ). – Mircea Sandu May 16 '14 at 06:15
0

My solution to that problem is based on the top answer which helped me find the right way.

So I find all the links and prevent their default behavior until the menu is opened and then disable them again when the menu is closed.

var menuButton = $('.js-side-menu-toggle');
var sideMenuLinks = $('#sidr').find('a');
var canClick = false;

sideMenuLinks.on('click', function(e) {
    if (!canClick) {
        e.preventDefault();
    }
});

menuButton.sidr({
    onOpen: function() {
        canClick = true;
    },
    onClose: function() {
        canClick = false;
    }
});

The tricky part here is that we need to change the plugin itself so that this code can work.

The problem is that the functions onOpen() and onClose() are called after the animation is done but not in it's callback function. That makes the functions to be called with the animation which is async and here is our issue.

Wrong:

// Close menu
if($body.is('body')){
scrollTop = $html.scrollTop();
    $html.removeAttr('style').scrollTop(scrollTop);
}
$body.addClass('sidr-animating').animate(bodyAnimation, speed).removeClass(bodyClass);
$menu.animate(menuAnimation, speed, function() {
    $menu.removeAttr('style').hide();
    $body.removeAttr('style');
    $('html').removeAttr('style');
    sidrMoving = false;
    sidrOpened = false;
    // Callback
    if(typeof callback === 'function') {
        callback(name);
    }
    $body.removeClass('sidr-animating');          
});

// onClose callback
onClose();

We just need to insert the onClose function inside the animation callback function in order to lock the links when the menu is closed and we should do the same with the on open code fragment.

Right:

// Close menu
if($body.is('body')){
scrollTop = $html.scrollTop();
    $html.removeAttr('style').scrollTop(scrollTop);
}
$body.addClass('sidr-animating').animate(bodyAnimation, speed).removeClass(bodyClass);
$menu.animate(menuAnimation, speed, function() {
    $menu.removeAttr('style').hide();
    $body.removeAttr('style');
    $('html').removeAttr('style');
    sidrMoving = false;
    sidrOpened = false;
    // Callback
    if(typeof callback === 'function') {
        callback(name);
    }
    $body.removeClass('sidr-animating');  

    // onClose callback
    onClose();        
});
Hristo Enev
  • 2,421
  • 18
  • 29