4
I would like to replace if/Else with Case because I believe it will be better for understanding the code since it will be a little long, but I cannot access the Array that will have the conditions to activate the actions. Error is in code or logic?
ex:
window.onscroll = function animaMenu() {
var menu = [pageYOffset > 70 , pageYOffset > 800 ];
switch (menu) {
case 0:
var x = document.getElementById("nav").className = "navegacao-movel"
break
case 1:
var x = document.getElementById("ball").className = "movimento"
alert("teste")
break
}
}
window.onload = animaMenu();
That code is what I intend to change
window.onscroll = function animaMenu() {
if (pageYOffset > 70) {
document.getElementById("nav").className = "navegacao-movel"
} else if (pageYOffset > 800) {
document.getElementById("nav").className = "navegacao"
} else if (pageYOffset > 1600) {
document.getElementById("section").className = "hover"
}
}
window.onload = animaMenu();
I thought that using Number in the case of the example I would take the position of the Array and with that the condition calculation, I will test its response.
– HeltonSS
@Heltonss hm, I get it, but in fact that’s not how the
switch
works. But it might be possible to do something like what you want using a bitset library. I’ll do a quick search, if it works out I’ll come back here.– mgibsonbr
for sure the decision of if is better and cleaner, I believed that with case would leave more readable using array, but my time does not let me do a wider search, even so thank you.
– HeltonSS
@Heltonss I updated my answer. You can do this using a native
Array
.– mgibsonbr
Fantastic I knew I was not so crazy rsrs, but did not imagine the need of the indexof would not think of it so soon, Thanks again and it worked correctly. pg: is not pageXOffset is pageYOffset I think I was wrong when typing the question.
– HeltonSS