June 22, 20188 yr Hi, I'm trying to create a mobile hamburger nav, where the drop-downs toggle when the parent is clicked. I have tried writing the following script for the products <li> item: $( ".subnav" ).hide(); $( "#products" ).click(function() { $( ".subnav" ).slideToggle( "slow", function() { }); }); $( "#products" ).click(function() { $( ".subnav" ).slideToggle( "slow", function() { $( ".subnav" ).hide(); }); }); The drop-down is only showing for around half a second when clicked. Where am I going wrong? Also, I only want this to affect the subnav when below a certain screen size. How would I achieve this? http://www.regalgaming.co.uk/
June 22, 20188 yr Are you after something like this https://codepen.io/jackpallot/pen/aKGKBP? The dropdowns can be animated with CSS if you want animation. Edited June 22, 20188 yr by Jack
June 22, 20188 yr What Jack says. In most cases you'd want to use CSS for animation. JavaScript isn't slow with animation per se. But jQuery is because it uses `setInterval` and doesn't make use of `requestAnimationFrame` which can result in poor performing animations that appear janky.
June 22, 20188 yr Author 3 hours ago, Jack said: Are you after something like this https://codepen.io/jackpallot/pen/aKGKBP? The dropdowns can be animated with CSS if you want animation. I am trying to use your pen. I have the exact JS, and the following in my HTML/CSS. Please advise me as to why nothing happens when I click on the parent button (e.g. Products).
June 22, 20188 yr Your CSS looks to be slightly different, so it's probably not referencing the right element to add and remove the active class on. There's no need to target .menu and ul because you're applying the class directly to the element anyway.
June 22, 20188 yr Author I have the exact CSS from your pen now, and nothing displays when I click the parent at Mobile or Desktop size now. I targeted .menu so it only affected the Mobile menu because it is a separate menu from the Desktop one. Could the CSS Above be clashing with the pen CSS?
June 22, 20188 yr Wordpress doesn't like the $. In your JS change this to jQuery. You should also enqueue the JS. The theme probably already has a JS file so you can append the code to this. https://developer.wordpress.org/reference/functions/wp_enqueue_script/
June 22, 20188 yr 1 hour ago, fisicx said: Wordpress doesn't like the $. In your JS change this to jQuery. You should also enqueue the JS. The theme probably already has a JS file so you can append the code to this. https://developer.wordpress.org/reference/functions/wp_enqueue_script/ This isn't a problem if you assign jQuery to a global $ variable.
Create an account or sign in to comment