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

The banner may be cropped when viewing on lower resolutions such as mobile devices or when zoomed in

I've encountered a similar issue on a popular website, www.godaddy.com. When zooming in or accessing the site from a mobile device, the right side of the banner gets cut off. However, when viewed on a full-sized computer screen, there are no issues. M ...

What is the best way to confirm checkbox selection based on MySQL data?

Writing this question feels challenging, but I have a collection of checkboxes with their data stored as JSON in my PHP database. What I'm trying to achieve now is to dynamically load the JSON data on the page without refreshing it, checking specific ...

prismjs plugin for highlighting code displays code in a horizontal format

If you're looking for a way to showcase source code on your website with highlighted syntax, prismjs.com may be just what you need. It offers a similar style to monokai... However, I've encountered an issue where the plugin displays my code in a ...

Revolutionary AJAX technology seamlessly updates and refreshes elements within every row or form on a webpage

<script type="text/javascript"> $(document).ready(function() { $("#submit<?php echo $resultshome['hskid']; ?>").click(function() { $.ajax({ type : "POST", url : "/scores/printPOST.php", data : { "subro ...

Developing a one-of-a-kind jQuery plugin with customized CSS styling

I am trying to create a custom jQuery plugin that will control the CSS on targeted paragraphs. Despite my research, I have not found any articles explaining what I need. I attempted to write the following code snippet, but it did not work. Can someone tell ...

Ways to extract particular items from a JSON array and store them in a JavaScript array

I am dealing with an external JSON-file structured as follows: { "type":"FeatureCollection", "totalFeatures":1, "features": [{ "type":"Feature", "id":"asdf", "geometry":null, "properties": { "PARAM1":"19 16 11", ...

What is the best way to achieve a seamless CSS transition for my sticky navigation bar?

Looking to create a sticky bar with a smooth CSS transition instead of the current rough effect. Any tips or hints would be greatly appreciated! For reference, I found the exact animation I'm aiming for on this website: https://css-tricks.com/ Here i ...

Ensure the content spans the full width of the container without displaying an overflow scrollbar by using the "position: absolute

I'm looking to create a small red div that spans the full width at a fixed top position within another div that has an overflow scroll property. I've provided a jsFiddle link for reference: http://jsfiddle.net/mCYLm/2/. The problem arises when t ...

Struggling with implementing Mobile Navigation menu

I'm struggling to implement a mobile menu for my website. After adding the necessary code, when I view the page in a mobile viewport, all I see are two sets of periods ("..."). However, unlike in the code snippet provided, the list does appear on the ...

Adjust the color of error messages in shiny from red

Currently, I am in the process of developing a Shiny app that includes numerous plots. Whenever I adjust any input parameters, there is a brief moment where the plots do not display before they are rendered, accompanied by a prominently displayed red error ...

The date in the JSON response does not align correctly

Seeking insights into this issue, I am currently utilizing the code below to generate a JSON response through an AJAX request console.log('get_therapist_sessions', response); response.forEach(function(item){ console.log(item); ...

Developing engaging div content using jQuery

Currently, I am in the process of implementing a dynamic div on my website that updates its content based on the link clicked by the user without the need for a page refresh. The data to be displayed is fetched from a MySQL database and is stored in JSON f ...

What is the best way to display multiple "loading images" for each cell of a table's data as it is being fetched asynchronously from a PHP script?

I'm looking to enhance my website functionality by incorporating a box where users can enter a string and press the OK button. Once submitted, this data will be sent to the PHP backend which will then return 20 rows of results. However, each of these ...

Having trouble adding your own styling to bootstrap-4 using a custom CSS file?

Here is the code I used to connect my two stylesheets, with the custom css file being linked second as per what I believe is the correct way to do it. <!doctype html> <html> <head> <link rel="stylesheet" type="text/css" href=" ...

Transform geojson data into an HTML table

As someone new to JavaScript, I am trying to figure out how to populate a HTML table with geojson entries like "ename" etc. Here is my current code: <table id="jsoncontent"></table> <script type="text/javascript"> window.onload = fu ...

Provide a spider with unadulterated HTML rather than setting off AJAX requests

While my website is being crawled, which PHP function should I utilize to ensure that even if ajax isn't activated, my content still gets transmitted? PHP doesn't have the ability to determine if ajax is supported. Objective: Provide the crawle ...

Resolving the Material-UI NextJS Button Styling Dilemma

I've encountered an issue with the styling of a few buttons in JS. When I apply a styling class using className, the formatting works fine on the initial render but loses its styling on subsequent refreshes. This problem is specific to two individual ...

Scroll horizontally through the number field in Chrome

I have configured a number input field with the text aligned to the right. Scenario: While testing, I discovered that selecting the entire text or using the middle mouse button to scroll within the input field allows for a slight leftward scrolling of ab ...

Tips for reducing text in a card design

As a newcomer to codeigniter 4, I recently created an event card with a lengthy description. However, when I attempted to display the event data from the database on the card, it ended up becoming elongated due to the length of the description: https://i. ...

Tips on how to prevent certain classes from being impacted by a hue-rotate filter applied to all elements on a webpage

I am currently in the process of adding a feature that allows users to choose between a dark or light theme, as well as select a specific theme color for the app. The implementation involves using CSS filters such as invert(1) for the dark theme and hue-ro ...