accordions that are supposed to adapt to different screen sizes are

My custom responsive accordions are not functioning as expected. The requirement is to display headings and content for desktop view, but switch to accordions on smaller devices. I have managed to do this, however, when resizing the window, the functionality becomes glitchy with weird animations and sometimes doesn't work at all.

Can someone please assist me in identifying what could be causing the issue in my code?

Here is the URL to jsBin:

http://jsbin.com/zarak/5/edit

And here is the JavaScript code snippet:

if($(window).width() < 360){
   $('h2').on('click', function() {

                var _target = $(this).next('.responsiveTap');
                if(_target.css('display') == 'none'){
                    _target.slideDown();
                    $(this).addClass('active');
                }else{
                    _target.slideUp();
                    $(this).removeClass('active');
                }               

            });  
  }

else if($(window).width() > 359){
    $('h2').on('click', function() {

      return false;

    });

  }

$(window).resize(function(){

  if($(window).width() < 360){
   $('h2').on('click', function() {

                var _target = $(this).next('.responsiveTap');
                if(_target.css('display') == 'none'){
                    _target.slideDown();
                    $(this).addClass('active');
                }else{
                    _target.slideUp();
                    $(this).removeClass('active');
                }               

            });  
  }

else if($(window).width() > 359){
    $('h2').on('click', function() {

      return false;

    });

  }

});

Thank you in advance for your help, Mufeed Ahmad

Answer №1

Give this a shot, check out the DEMO

$(window).resize(function() {
  if($(this).width() < 360) {
     $('.responsiveToggle').slideUp(400, 'linear');

     $('.in-line h2').on('click', function() {
       $('.responsiveToggle').stop().slideUp(400, 'linear').removeClass('active');
       $(this).next('.responsiveToggle').stop().slideToggle(400, 'linear').addClass('active');
     });
  }
  else {
     $('.responsiveToggle').slideDown(400, 'linear');
     $('.in-line h2').off('click');
  }
});

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

Only show a single li element in CSS at a time

I'm working with Angular 2 and I have a list: <ul> <li *ngFor="let show of (ann)"> {{show}} </li> </ul> I want to show one item from the list every 3 seconds in an infinite loop. Here&apos ...

Utilizing ngModel within a nested ngFor loop in Angular to capture changing values dynamically

I have been working on developing a screen using Angular and I'm facing an issue with binding values using ngModel. https://i.sstatic.net/DCJ3T.png Here is my implementation. Any help would be appreciated. The model entity I am using for binding the ...

Bootstrap Slider Not Cycling Through Database Items

I have a notification list that is currently displaying all messages at once instead of one at a time through a carousel. How can I modify the code to loop through the list and display only one message at a time within the carousel? View <div id=" ...

The usage of uppercase letters in HTML attributes

I'm struggling to get animation working on my chart. I have been using jQuery's append function to add an animation tag to my HTML page, but for some reason the animation isn't displaying properly. The issue seems to be related to the capita ...

The "Auto Load More" feature is only loading the first two pages when scrolling down

I have set up an auto load more feature that triggers on page scroll down. Although my jQuery/ajax code is functioning properly, it only loads the first 2 pages automatically as I scroll down. There are more pages/records available but the loading process ...

Steps for sending a request to the root resource

I've encountered a problem that stems from my limited knowledge of Express. Despite creating a project with Express, I'm unable to make calls to the root, only to the routes. I suspect the issue lies in my usage of app.use(...). app.js var inde ...

Save a newly uploaded image to Github utilizing NodeJS and the Github API

Struggling to upload an image to my Github repo using NodeJS and Github API has been a challenge for me. I have managed to create the SHA, Commit Tree, and all necessary steps, but the final hurdle is passing the image to the API and saving it as an actual ...

New Article: "Implementing Modal Form Dialog Boxes with Bootstrap 3 post-AJAX Submission"

I am facing an issue with my boostrap panel which has been converted into a modal. On the same page, there is a form that contains a modal which resembles more of a panel based on the theme I have implemented. I am unsure how to trigger the modal to load ...

What is the best way to distinguish between a button click and a li click within the same li element

In my angular and css GUI, each line is represented as an li inside a ul with a span. The functionality includes a pop-up opening when clicking on the li background and another action occurring when pressing the button (as intended). The issue arises when ...

How can I retrieve the Google Maps URL containing a 'placeid' using AJAX?

I have a specific URL that I can access through my browser to see JSON data. The URL appears as follows: https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJZeH1eyl344kRA3v52Jl3kHo&key=API_KEY_HERE However, when I attempt to use jQuer ...

Insert a fresh item into the existing unordered list

Every time I enter something in the prompt, it shows up as "undefined". What I actually want is for whatever I type into the prompt to be added as a new list item. For instance, if I type "Food" in the prompt, I expect to see "Food" appear on the second li ...

Using ant-design with react-draggable for modals: A step-by-step guide

Trying to implement a modal window using ant-design with the react-draggable npm package has been quite challenging. For those unfamiliar, react-draggable is a simple component for enabling drag functionality on elements. However, I encountered an issue wh ...

ReactJS encountered an error: [function] is not defined, July 2017

Attempting to convert a JSON file into an array and then randomly selecting 5 items from it. I suspect the issue lies in my render/return statement at the end of ImageContainer.js, but as a newbie in ReactJS, it could be anything. Any assistance or guida ...

Tips for Removing Padding in Material UI Container Component

I'm currently working on creating a hero banner using the material-ui framework and I've encountered an issue. Here's what I have so far: https://i.stack.imgur.com/UXTDY.png However, I'm facing an irritating problem with left and rig ...

Is there a way to link to mouseover and mouseleave actions while utilizing ng-options?

I am trying to create a directive that will handle the mouseover and mouseleave events for the option elements within this select element: <select class="form-control" custom-directive="" ng-model="vm.selectedPersonalInfo" ng-options="personalInfo as p ...

The importance of adding blank spaces in email formatting

A Visual Basic 6 application is sending a HTML email. The issue is that when the email is received, all blank spaces are removed. For example, in the code snippet below, the email sent says "this is a test" instead of displaying the actual spaces on line ...

Where can I locate the Socket.IO server within the local area network (LAN)?

I am currently in the process of developing an application that facilitates connections between devices within the same network. In my design, any device can act as a server, and I aim for clients to automatically detect the server without requiring users ...

Performing multiple service calls in a synchronized way using Ajax

Currently, I am working on a project where I need to make multiple REST service calls to accomplish the required functionality. Furthermore, in order to complete this task, I have to use the response from ServiceCall-ONE as the input for ServiceCall-TWO. S ...

aligning columns within a flexible element that has a maximum height

Within this flex element, I have set flex: column wrap; and max-height: 150px;. The issue I'm facing is that justify-content doesn't seem to be working. However, when I switch to using height: 150px, the alignment works fine. This problem arises ...

404 error occurs when Spring-Boot fails to include CSS file through ResourceLocations

My spring-boot application is currently running smoothly on a local computer, but I have encountered an issue. When I run mvn package, none of my CSS or JavaScript files located in /src/main/wepapp/css are included in the jar file created in the target d ...