Hi guys please advise if this is the correct way of running the code on certain window width.
$(window).resize(function(){
var width = $(window).width();
if(width <= 780){
jQuery(".answers").hide();
jQuery(".container h4").click(function(){
jQuery(this).next(".answers").siblings(".answers:visible").slideToggle();
jQuery(this).next(".answers").slideToggle();
});
}
else{
jQuery(".answers").show();
}
})
on width bigger than 700px if I click on the question.. the whole thing keeps wobbling. Please advise --- the Jsfiddle link https://jsfiddle.net/bw6k874b/28/
asked Apr 19, 2017 at 8:27
The Naga Tanker
4411 gold badge4 silver badges18 bronze badges
-
1Do you need to do it via JS? Why not do it via CSS? Way better performance-wise. Wobbling is caused by elements being initially hidden and each time the window is resized, if the width is bigger than 780px, they will get shown.l.varga– l.varga2017年04月19日 08:30:01 +00:00Commented Apr 19, 2017 at 8:30
-
1Here is some help, without the wobbling: jsfiddle.net/e4g4a4m2Mazz– Mazz2017年04月19日 08:30:10 +00:00Commented Apr 19, 2017 at 8:30
-
I'm doing through Js because I want the answer div if the user clicks on some other div.The Naga Tanker– The Naga Tanker2017年04月19日 08:32:03 +00:00Commented Apr 19, 2017 at 8:32
-
thanks @Mazz .. That was exactly what I wantedThe Naga Tanker– The Naga Tanker2017年04月19日 08:37:28 +00:00Commented Apr 19, 2017 at 8:37
-
Nice, i added it as answerMazz– Mazz2017年04月19日 08:41:10 +00:00Commented Apr 19, 2017 at 8:41
1 Answer 1
You just have to check with jQuery(".answers").is(':hidden') if .answers is already hidden.
$(window).resize(function() {
var width = $(window).width();
if (width <= 780) {
if (jQuery(".answers").is(':hidden')) return; // This is the fix
jQuery(".answers").hide();
jQuery(".container h4").click(function() {
jQuery(this).next(".answers").siblings(".answers:visible").slideToggle();
jQuery(this).next(".answers").slideToggle();
});
} else {
jQuery(".answers").show();
}
})
Here is yours without the wobbling: https://jsfiddle.net/e4g4a4m2
Mark
2,0433 gold badges22 silver badges43 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js