First of all, your variable removeClass
is not defined, this can end up mixing with variables of a higher level or global, so always use var ...
Or thing, avoid creating the events in the "first level" of javascript (it would be the global level), something like:
<script>
var oi = 1;
</script>
Prefer something like:
<script>
(function () {
var oi = 1;
}());
</script>
So you can avoid mixing the variables
Following up with this something you could do would be to use the event .on
of Jquery instead of .click
(equivalent to .bind("click",
)
The events .on
verify changes in the DOM
, if a new object is added it automatically applies the event. I say this because I believe you are trying to add the event .click
, when the object has not yet been rendered.
An example of .on
would be:
$(document).on("click", "SELETOR", function () { console.log("Test..."); });
In your case, your script could work like this:
(function () { //"Isola" as variáveis
var removeClass = false;//Define a variável
//Configura o click para o elemento .toggle-canvas, assim que este "existir"
$(document).on("click", ".toggle-canvas", function () {
$("#menu-canvas").toggleClass('menu-canvas-active');
removeClass = false;
});
//Configura o click para o elemento #menu-canvas, assim que este "existir"
$(document).on("click", "#menu-canvas", function() {
removeClass = false;
});
//Não é necessário .on aqui, pois o document é um dos primeiros elementos "dentro do javascript" a existirem
$(document).click(function () {
if (removeClass) {
$("#menu-canvas").removeClass("menu-canvas-active");
}
removeClass = true;
});
}());
Are you sure the troublesome part is the code part you put in? I find it much more likely to be something of CSS that the mobile browser does not support and ends up not rendering the menu. Can you make a minimal example that plays the problem? (full, with html, css and js)
– Guilherme Bernal
Have you tried it with
$(document)
instead of$("html")
?– Sergio
In response to
addClass
, any jQuery method works on any device, including smartphones.– Zuul
Should I not add
touchend
next to theclick
?– Wakim
Could you tell me if my answer helped? Or if not, what was the problem?
– Guilherme Nascimento