Enhance your website with a stylish CSS jQuery menu that lets you toggle, slide

If you're curious about the code, take a look here!

Here are some of the issues I'm facing:

  1. The div doesn't toggle to hide when clicked on itself, only when another one is clicked. (UPDATE: it actually won't toggle at all)

  2. When the page loads, all the divs that are supposed to be hidden briefly appear before disappearing. Is there a way to prevent this? (I've fixed it by using display:none; in css)

  3. Once a div is clicked, it should maintain its highlighted background color until either another div is clicked or itself is clicked again. I can't seem to figure out how to do this.

  4. (on a side note) On the iPad, the menu_row retains its hover background color even after being clicked. It's fine if the color stays while the div is visible, but once it retracts, it should revert back to gray. Any suggestions on how to achieve this?

Answer №1

1.

jQuery(document).ready(function($) {

    //store the `.menuImage` elements as they will be frequently used
    var $menuImages = $(".menuImage").hide('slow');
    $(".menuItem").click(function() {

        //identify the `.menuImage` element that is a child of the clicked element
        var $ele = $(this).children(".menuImage");

        //collapse all other `.menuImage` elements except for the one inside the clicked element
        $menuImages.not($ele).slideUp(500);

        //toggle the visibility of the child element within the clicked element
        $ele.slideToggle(500);
    });
});​

Demo: http://jsfiddle.net/jasper/XcJwW/17/

3.

jQuery(document).ready(function($) {
    var $menuImages = $(".menuImage").hide('slow');
    $(".menuItem").click(function() {
        var $ele = $(this).children(".menuImage");

        //slide out and remove `hover` class from all other `.menuImage` elements
        $menuImages.not($ele).slideUp(500).parent().removeClass('hover');

        //toggle view of clicked `.menuImage` element and toggle the `hover` class
        $ele.slideToggle(500).parent().toggleClass('hover');
    });
});​

In your CSS, make this small adjustment:

#menu_row1 .menuItem:hover, #menu_row1 .menuItem.hover { background:#ff0000; }
#menu_row2 .menuItem:hover, #menu_row2 .menuItem.hover { background:#ffe100; }
#menu_row3 .menuItem:hover, #menu_row3 .menuItem.hover { background:#0033cc; }

Remember, you can either hover over an element or assign it the `hover` class to achieve the same effect.

Demo: http://jsfiddle.net/jasper/XcJwW/20/

Answer №2

Here is an alternative script for you to use:

$(document).ready(function() {
    $(".menuIcon").hide();
    $(".menuItemButton").click(function() {
        $(".menuItemButton").not(this).children(".menuIcon").hide('fast');
        $(this).children(".menuIcon").slideToggle(300);
    });
});

You can remove the unnecessary second part of your existing script.

Answer №3

  1. One reason for this behavior is that you hide it before using slidetoggle, causing the toggle to show. To fix this, hide all elements except the one directly below your current node (consider using filter() or not()). For example:

    var self = $(this);
    // Filter out elements that are descendants of this
    $('menuItem').filter(function () { return ! self.has(this); }).hide();
    
  2. The issue arises from applying .hide('slow') within the .ready function. Removing 'slow' should resolve the problem.

  3. In addition to defining the style for :hover, you also need to define it for a class that is added/removed on click. This will help maintain those styles.
  4. I'm not entirely sure. iPads don't have hover functionality, so it's possible they use :active instead. Providing explicit styling for :active could override the :hover styling in this case.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Extracting the URL of the @font-face declaration in CSS with the use of JavaScript

Currently, my CSS file contains numerous @font-face tags. Each tag specifies a unique font-family and src URL. @font-face{ font-family: Garamond; src: url('ePrintFonts/pcl_91545.ttf'); } @font-face{ font-family: C ...

Issue with Ajax response being undefined when keyup event is triggered

Currently, I have set up an input form field for users to enter a captcha that is generated from an image using Coldfusion's cfimage tag and stored in a session variable. Once the user types in the captcha, jQuery triggers a Coldfusion CFC to verify i ...

Discovering the differences between input values in HTML when using scripts

I have a code snippet here for an HTML project. The code includes an input field for username and password. I am looking to compare the user's input with a specific value using JavaScript. My question is, what code should be included in the button cli ...

Creating dynamic form fields using AngularJS

I have a form that includes an input field, a checkbox, and two buttons - one for adding a new field and one for deleting a field. I want to remove the add button and instead show the next field when the checkbox is checked. How can I achieve this? angu ...

Is async programming synonymous with multi-threading?

Discussing a JavaScript code that utilizes the setInterval function every 2 seconds. There is also an animation event for some control triggered by the onblur event. If the onblur event occurs (along with the animation), there is a possibility of encount ...

Calculating available balance by subtracting the entered amount from the existing balance in Python Django

In the user profile model within my Django project, there are fields for available balance and current balance. I am looking to deduct an input amount from the current balance in order to display the updated available balance on the user's web page. ...

Selecting elements in VueJS and jQuery using query selectors

I'm currently facing an issue with accessing a DOM element inside a .vue component. It seems like the element hasn't rendered yet. One workaround that I found is using a setTimeout function: setTimeout(() => { $('.element').do_ ...

Implement a shadow effect on a customized image using Tailwind CSS

Can someone help me with this code snippet I have written: <script src="https://cdn.tailwindcss.com"></script> <div class="m-6 space-x-3"> <div v-for="m in media" class="w-[200px] h-[200px] inline-block"> <img class=" ...

When using Angular 8 with RxJs, triggering API calls on click events will automatically detach if the API server is offline

I have an Angular 8 application with a form containing a save button that triggers a call to a Spring Boot microservice using RxJs. I decided to test what would happen if the Rest API server were down. When the Rest API is running, clicking the button wor ...

Having trouble with some JS and Jquery codes not functioning properly in IE until the page is refreshed a few times. Any suggestions on how to fix this issue?

My code seems to be experiencing issues in Internet Explorer where some of the JS and jQuery codes are not functioning correctly initially, but start working after refreshing the page a few times. Any thoughts on why this could be happening? Here is the ...

Determine the total width by adding together the widths of all child elements

Does anyone know how to calculate the total width of parent element (#projects) based on the combined widths of all child elements (.project)? I tried using jQuery but couldn't get it to work. Any help would be greatly appreciated! Thank you! var to ...

Displaying Selected DropdownList Value from Database into TextBox in MVC3

Within my MVC3 view, I have a dropdown list that triggers an action when an item is selected, displaying corresponding rows based on the value. The values retrieved need to be shown in a text box within the same view. I've attempted the following: ...

Arranging data into columns using HTML and CSS

I'm currently working on a CSS solution to organize various elements like buttons and text in a tabular column format. My main challenge lies in controlling the layout effectively, especially since CSS columns are not compatible with IE9 and earlier v ...

php modes breaking away from traditional php

My recent inquiry was regarding the distinction between HTML and PHP separation. While tutorials and examples often mention this concept, I have realized that it goes beyond what most people talk about - specifically relating to PHP modes. Is there a need ...

Tips for creating a responsive carousel slider for images

No matter how much I've tried, I can't seem to find a solution on how to make my image responsive along with the caption. Below is my HTML code: <section id="banner"> <div class="banner-bg"> <div class="banner-bg-item ...

Ensure that the div automatically scrolls to the bottom when it is loaded, and also when new data is added - using angular

My goal is to replicate the functionality of the iPhone's "Messages" app on a web application using AngularJS or any other JavaScript framework. Each message will be contained in a div element within a larger container. When a new message is added, I ...

Sending checkbox selections to PHP using AJAX and jQuery

I'm currently working on integrating a contact form with bootstrap alerts (for success), validation, ajax, and php. One challenge I'm facing is passing checkbox values to php. The email submission fails to go through currently. When I disable the ...

Determining if a replacement took place in the Javascript replace function

I've developed a function that scans through a set of strings and replaces specific patterns: var content = 'Here is a sample string 1/23'; var currentDate = new Date(); var currentMonth = currentDate.getMonth()+1; var currentDay = curre ...

Transitioning in Vue.js can be triggered by changing a value up or down

My current transition block component is set up like this: <div v-if="!surveyResultIsReady" class="vh-md-40 position-relative" > <transition name="custom-classes-transition" enter-active-class="animated slideInRight" ...

Using JavaScript to dynamically load Cascading Style Sheets based on the current

I am trying to dynamically load different CSS files based on the current date (season). I attempted to tweak an image script that I found on Stack Overflow, but it didn't yield the desired result. Could someone please guide me on where I might be ma ...