Utilizing media queries to customize the appearance of a jQuery accordion widget

Hello, I'm struggling with implementing a jQuery accordion for mobile platforms that destroys itself on larger screens. While my code is mostly working, I've encountered two issues:

  1. The accordion only gets destroyed when the window is resized, not when the document loads.

  2. Once the accordion is destroyed, I can't get it to work again when resizing back to mobile sizes.

Here's the code snippet in question:

<div class="content-wrap">
            <div class="container">
                <div class="row">
                    <div class="content-fill">
                        <div class="content">
                            <div class="testimonials col-md-6 col-lg-6">
                                <h1>section 1</h1>
                                <p>this is a paragraph</p>
                            </div>
                            <div class="social col-sm-6 col-md-3 col-lg-3">
                                <h1>section 2</h1>
                                <div class="social-wrap">
                                    <a class="twitter-timeline" width="100%" data-chrome="transparent noscrollbar" href="https://twitter.com/xxxxxx" data-widget-id="383311602641952769">Tweets by @xxxxxx</a>
                                </div>
                            </div>
                            <div class="news col-sm-6 col-md-3 col-lg-3">
                                <h1>section 3</h1>
                                <p>this is a paragraph</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

  // <![CDATA[
     var $ = jQuery.noConflict(); $(document).ready(function()
    { 

        $('.content').addClass('accordion');

        $(window).resize(function() {
            if ($('.social').css('float') == 'left')    {
                $('.content').accordion('destroy');
            } else if($('.social').css('float') == 'none')   {
                $('.content').accordion('enable');
            }
        });

         $(".accordion").accordion({
            collapsible: true,
            header: "h1",
            heightStyle: "fill",
            });

            //getter variables
            var collapsible = $(".accordion").accordion("option","collapsible");
            var header = $(".accordion").accordion("option","header");
            var heightStyle = $(".accordion").accordion("option","heightStyle");

            $('.carousel').carousel({ interval: 4000, cycle: true }); 

        }); // ]]>

Answer №1

Swap destroy with disable and include a listener for the load event:

function accordionSwitch() {
    if ($('.social').css('float') == 'left')    {
        $('.content').accordion('disable');
    } else if($('.social').css('float') == 'none')   {
        $('.content').accordion('enable');
    }
}

$(window).on('resize load', accordionSwitch);

According to the Accordion documentation, using the destroy method

completely removes the accordion functionality, bringing the element back to its pre-init state.

Answer №2

Have you considered utilizing media queries to show the accordion on smaller screens and hide it on larger screens?

@media (max-width: 797px) {
    .accordion {
        display: block;
    }
}

Your default styling should be:

.accordion {
    display: none;
}

Answer №3

Avoid enabling and disabling your accordion component. Instead, switch between functions based on the screen size. This approach offers a simpler solution that eliminates the need to constantly trigger a function during screen resizing events.

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

Using JQuery, identify cells located in the first column of a table excluding those in the header section

In the past, I had code that looked like this: $(elem).parents('li').find(...) I used this code when elem was an item in a list, making it easy to reference all items in the list. But now, I've made some changes and decided to use a table ...

Elements in Bootstrap Container class failing to align properly

Hello Everyone, I am attempting to achieve the same layout as demonstrated in [original image] where the "browse collection" div and the "request brochure" div are aligned with the four items above them. My approach involves using Bootstrap, and I assume ...

What could be causing this code to malfunction on a mobile device?

I am struggling to make this drag and drop function work on mobile devices. Despite implementing the code, it doesn't seem to function properly when accessed on my mobile phones. Here is the snippet of the code: copy = 1; $('.dragArea img&apo ...

the three.js code runs smoothly on JSfiddle, but it does not seem to be working properly

Check out this basic three.js code snippet for generating custom geometry http://jsfiddle.net/67Lgzjpb/. After attempting to run this code directly in my browser (not via JSFiddle), an error message pops up: TypeError: geom.computeCentroids is not a funct ...

Retrieving specific HTML data without the need to download the entire webpage

Is there a way to extract data from an HTML table on a webpage without downloading the entire HTML content? In my Delphi XE2 application, I am using TWebBrowser and TEmbeddedWB to load the webpage and then parse the Table element. However, due to the heav ...

What is the best way to switch from using the three.js JSONLoader to the ObjectLoader?

I successfully loaded multiple files using the code below for three.js JSONLoader: <!DOCTYPE html> <html> <head> <title>Rotating Sphere</title> <meta charset="utf-8"> <!-- https://cdnjs.com/libra ...

Having numerous calls to isNaN within a JavaScript if-else statement

I've created a JavaScript function that generates div elements and styles them based on user input values. However, I'm facing an issue when trying to display a warning message if the input is not a number. Currently, I'm unable to create th ...

The jQuery .load function doesn't seem to be functioning properly when used within a partial view displayed inside a modal popup

I am facing an issue with loading a partial view via ajax call in my main view. The modal popup is displayed when the "AddSkillBtn" button is clicked, but I need to trigger an ajax call when the partial view is loaded. However, using .on 'load' d ...

Retrieve information from an external JSON file and present it in a table format - Obtain the most recent data available

I am attempting to extract the data from On the website, there is a dropdown menu that displays two different screenshots. I want to modify my code so that it shows the most recent screenshot data in a table. Here is my current code: http://jsfiddle.net ...

Encountering problems with ajax technology

As a newcomer to Laravel and Ajax, I am facing an issue with displaying the state of the selected country in a dropdown list. Although the data is successfully fetched from Laravel and received through Ajax, I am struggling to dynamically add it to the H ...

Is it possible to replace text on click using CSS instead of jQuery?

My new project involves creating a flashcard system. The idea is that when a question is clicked, it reveals the answer and hides the question. To achieve this, I am currently using the following code: jsFiddle <p id="shown">What is stackoverflow?&l ...

What is the best way to retrieve the color of a WebElement when dealing with a complex CSS structure in Webdriver

I am currently trying to determine if the color of a specific page changes immediately after clicking on a button designed to alter the color of an element within that page. My goal is to extract the RGB value stated as rgb(183,168,168); in this particul ...

Even after diligently assigning a unique key to each child in react, why do I continue to encounter an error?

I've attempted this task previously and have provided a key, but for some reason I keep encountering an error. As I am relatively new to working with React, it's possible that there is a simple mistake hiding somewhere within my code. Update: Th ...

Troubleshooting your Meteor App on Nitrous.io with server-side debugging

I currently have a basic Meteor application up and running on a Nitrous box. I am interested in utilizing node-inspector for debugging the server-side part of my app, but I'm facing difficulty accessing the console as detailed here. The Meteor app is ...

The date format is malfunctioning -> Error: The date provided is not valid

My goal is to convert the date format from 2022-01-17T21:36:04.000Z to January 18th using the npm package, dateFormat. I found success with the following code snippet: const date = dateFormat("2022-01-17T21:36:04.000Z", "mmmm dS"); However, when trying t ...

JQuery ajax fails to trigger the success function

Upon using an ajax request to insert data into the database, I encountered an issue where the button message did not update after the submission was successful. Initially, I set the button text to Please wait... upon click, and intended to change it to Don ...

Why is Django not recognizing my ajax request when using the `is_ajax()` function?

After sending a GET request and processing the data to obtain a redirect link, the function seems to be having trouble entering the is_ajax() block. $(document).on('click', '.top_up_pay', function (e) { var post_data; e.preventDefa ...

Guide on dynamically importing images using the Nuxt.js and jQuery script

<template> <div> <select ref='select'> <slot></slot> </select> </div> </template> <script> import Vue from "vue" export default { name: " ...

Showing post response (XMLHttpRequest) on Chrome extension interface instead of Python console

I am currently developing a Chrome extension that sends a post request with information about the specific URL being browsed by the user to Flask (local host). A web scraping process is then carried out on this URL to determine a category based on the obta ...

Utilizing Jquery tabs for consistent height display

Currently, I am utilizing jquery tabs for showcasing various content. This is what my functions look like: $(function() { $( "#tabs" ).tabs(); }); I attempted to ensure all tabs have the same height by using this approach: var heightStyle = ...