Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

JavaScript Experts

Featured Replies

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/

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.

  • 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).

Capture.PNG

Capture2.PNG

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.

  • 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?

Capture.PNG

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.