Container containing sliding content underneath

I've designed a grid with separate boxes that reveal related content when clicked. The display of this content is achieved using jQuery's slideToggle feature.

The setup functions smoothly, with each row containing clickable boxes that unveil their respective content below them:

https://i.sstatic.net/ULLtz.jpg

// JavaScript code snippet

$(document).ready(function() {

    $('.info-content-box').hide();

    var openedId = '';

    $('.info-box .info-header').click(function(e) {
        var id = '#' + $(this).data('name');

        if (openedId ==='') {
            $(id).slideToggle(400, function() {
                openedId = id;
            });
        } else {

            if (openedId == id) {
                $(openedId).slideToggle(400, null);
            } else {
                $(openedId).hide(10);
                $(id).fadeToggle(400, function() {
                    openedId = id;
                });
            }
        }
    })
})
/* CSS styles */

.info-box {
    text-align: center;
    height: 200px;
    color: white;
}

.info-box .info-header {
    background-color: #3178b9;
    height: 90%;
    border: 1px solid #f5f0e7;
    display: flex;
    justify-content: center;
    align-items: center;
    -webkit-transition: all 150ms ease-out;
    -moz-transition: all 150ms ease-out;
    -o-transition: all 150ms ease-out;
    transition: all 150ms ease-out;
}
...
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8cac7c7dcdbdcdac9d8e89b869b869f">[email protected]</a>" data-semver="3.3.7" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1d3dedec5c2c5c3d0c1f1829f829f86">[email protected]</a>" data-semver="3.3.7" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container" style="padding-top:50px;">
      <div class="row">
        ...
    </div>

However, I aim to make my code snippet responsive so that the content box appears directly beneath its corresponding box in mobile view. Currently, due to placing the content divs in a row below the box rows, the content displays at the end of all the boxes.

If anyone could assist me in achieving this effect, it would be greatly appreciated.

The code can be accessed on Plunker here: Plunkr

My goal is similar to the functionality found on this webpage: Example Link

On that site, the opened content shifts other boxes downward and operates seamlessly even on smaller screens.

Edit:

In response to user StefanBob's suggestion, I have updated my JavaScript as shown below:

$(document).ready(function() {

    $('.info-content-box').hide();

    var openedId = '';

    $('.info-box .info-header').click(function(e) {
        var id = '#' + $(this).data('name');

        if (openedId === '') {
            if ($(window).width() < 992) {
                $(id).insertAfter($(this).parent());
                $(id).slideToggle(400, function() {
                    openedId = id;
                });
            } else {
                $(id).slideToggle(400, function() {
                    openedId = id;
                });
            }
        } else {

            if (openedId == id) {
                if ($(window).width() < 992) {
                    $(id).insertAfter($(this).parent());
                    $(openedId).slideToggle(400, null);
                } else {
                    $(openedId).slideToggle(400, null);
                }
            } else {
                $(openedId).hide(10);
                if ($(window).width() < 992) {
                    $(id).insertAfter($(this).parent());
                    $(id).fadeToggle(400, function() {
                        openedId = id;
                    });
                } else {
                    $(id).fadeToggle(400, function() {
                        openedId = id;
                    });
                }
            }
        }
    })
})

This solution checks the window width to determine whether content should be inserted after the parent box or simply toggled. While functional, I am open to suggestions for a more elegant implementation of this behavior rather than what seems like a workaround.

Answer №1

In your JavaScript click functions, simply select the element with the class of .info-content-box and use the insertAfter() method to position it after the `.info-box` element.

// Implementing code functionalities

$(document).ready(function() {

    $('.info-content-box').hide();

    var openedId = '';

    $('.info-box .info-header').click(function(e) {
        var id = '#' + $(this).data('name');

        if (openedId ==='') {
            $(id).insertAfter($(this).parent());
            $(id).slideToggle(400, function() {
                openedId = id;
            });
        } else {

            if (openedId == id) {
            $(id).insertAfter($(this).parent());
                $(openedId).slideToggle(400, null);
            } else {
                $(openedId).hide(10);
                $(id).fadeToggle(400, function() {
                    openedId = id;
                });
            }
        }
    })
})
/* Styling for the elements */

.info-box {
    text-align: center;
    height: 200px;
    color: white;
}

.info-box .info-header {
    background-color: #3178b9;
    height: 90%;
    border: 1px solid #f5f0e7;
    display: flex;
    justify-content: center;
    align-items: center;
    -webkit-transition: all 150ms ease-out;
    -moz-transition: all 150ms ease-out;
    -o-transition: all 150ms ease-out;
    transition: all 150ms ease-out;
}

.info-box .info-header:hover {
    background-color: #b4a28f;
    border: 5px solid #f5f0e7;
    -webkit-transition: all 150ms ease-in;
    -moz-transition: all 150ms ease-in;
    -o-transition: all 150ms ease-in;
    transition: all 150ms ease-in;
}

.info-box .info-header .info-line {
float: left;
    background-color: white;
    height: 2px;
    width: 0%;
    -webkit-transition: all 150ms ease-out;
    -moz-transition: all 150ms ease-out;
    -o-transition: all 150ms ease-out;
    transition: all 150ms ease-out;
}

.info-box .info-header:hover .info-line {
float: left;
    background-color: white;
    height: 2px;
    width: 30%;
    -webkit-transition: all 150ms ease-in;
    -moz-transition: all 150ms ease-in;
    -o-transition: all 150ms ease-in;
    transition: all 150ms ease-in;
}

.info-content-box {
    padding-bottom: 30px;
    width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e3818c8c979097918293a3d0cdd0cdd4">[email protected]</a>" data-semver="3.3.7" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e6c61617a7d7a7c6f7e4e3d203d2039">[email protected]</a>" data-semver="3.3.7" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container" style="padding-top:50px;">

Answer №2

Utilize jQuery's animate function to create dynamic effects

I have included documentation from the official jQuery website, and for convenience, I have shared a complete code example on Codepen. Feel free to explore it!

<div class='foo'></div>
<div class='foo'></div>
<div class='foo'></div>
<div class='foo'></div>
<div class='showBox'>
<img class='show'>
<img class='show'>
<img class='show'>
<img class='show'>
</div>

    body {
  margin: 0;
  padding: 0;
  text-align: center;
}

.foo {
  width: 200px;
  height: 200px;
  background-color: black;
  display: inline-block;
  margin: 20px auto;
}

.foo:hover {
  background-color: green;
  cursor: pointer;
}

.show {
/*A bit ironic*/
  display: none;
  background-color: blue;
}

.showBox {
  display:none;
  width:100%;
  height: 200px;
  background-color: red;
}

$(function(){
  $('.foo').on('click', function(){
    $('.showBox').animate({
      height: "toggle"
    }, 1500);
  });
});

Friendly reminder: Consider setting the CSS display property to 'none' instead of using JavaScript to hide elements.

Check out the full example on Codepen here

Explore more about jQuery's animate function here

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

What is the best way to ensure that my transitionend event is triggered?

I'm currently exploring the <progress> element and attempting to incorporate a CSS transition to achieve a smooth progress bar effect as its value increases. Despite my efforts, I can't seem to trigger JS after the transitionend event occur ...

Can we implement a variety of site designs based on the size of the window?

Utilizing media queries allows us to modify the CSS for various screen sizes. My inquiry is: can we switch to a completely different page layout (or View in MVC) when the window size, specifically when the width is less than 500? Assuming that the control ...

What is the best way to change the location of a jQuery Mobile global popup?

I have a jQuery Mobile global popup that is initially empty and generated dynamically. I am utilizing the beforeposition event to detect when the popup opens. At this point, I load a configuration file or content file, create the content, and then add it t ...

Exploring the functionalities of JSON and AJAX with a two-dimensional array

I have created a function to call JSON data, but it's not working. Any suggestions on how to properly call it? function fetchCompanyExpensesType(elementId){ $.ajax({ url: "../../modal/get_companyexpenses_type.php", dataType: ...

What is the method for setting the doctype to HTML in JavaScript or React?

I created a canvas with a height equal to window.innerHeight, but unexpectedly it seems to have 100% screen height plus an extra 4 pixels coming from somewhere. I came across a solution suggesting that I need to declare doctype html, but I'm unsure ho ...

How to maintain row highlight in jQuery datatable even after it is reloaded

I have a datatable set to reload every 10 seconds. When a user clicks on a row, it highlights, but the highlight is lost when the table reloads. Here is a condensed version of my datatable code: $(document).ready(function() { // set up datatable $(& ...

Whenever the selected option in an HTML dropdown menu is modified, a corresponding input field should be automatically adjusted

Within my Rails application, I am facing a challenge related to updating the value of a text_field when a user chooses a different option from a select tag. The select tag is populated from a model which contains a list of countries for users to choose fro ...

Bootstrap sticky footer with adjustable height

Striving to achieve a sticky footer with a custom height on my website has turned out to be more challenging than I initially expected. Presented below is a snapshot of the current state of my footer: https://i.sstatic.net/SXaTA.png The issue arises whe ...

Guidelines for inputting a value into a JavaScript list and choosing a specific item within the list

When it comes to studying Selenium Web, I rely on the website. My current challenge involves inputting 'Ottawa' into the location control and then selecting 'Ottawa, ON'. WebElement el = driver.findElement( By.xpath("//*[contains( ...

The table is hidden and the buttons are unresponsive when inserted using jQuery

Exploring blueimp's jQuery-File-Upload Demo has been occupying my time recently. After studying it for several days, I feel like I've gained a solid grasp of how it functions. However, as someone with limited experience in web development, there ...

Tips for extracting text from a selenium webelement icon that displays hovertext

Let's use Amazon as an example. Suppose we want to extract the star rating for a product. When hovering over the stars, we see a text element that says "4.0 out of 5 stars" and upon inspecting the code, we find: <span class="a-icon-alt">4.0 ou ...

Adjust the position of the xAxis on the Highcharts chart to move it downward or

I recently inherited some code from the previous developer that uses highcharts.js (v3.0.1). I'm having trouble with the xAxis appearing within the graph itself (see screenshot). Despite my efforts to recreate this issue in jsfiddle, I can't seem ...

The rounding of my image is uneven across all sides

I'm attempting to give my image rounded borders using border-radius, but I'm unsure if the overflow property is affecting the image. Image 1 shows my current image, while Image 2 depicts what I am trying to achieve. I am utilizing React, HTML, no ...

Strange rendering in CSS JQuery Datatable

I am encountering an issue with a DataTable from datatables.net. Each row can be selected by clicking on a checkbox, which triggers a click event. In response to the event, I add or remove a CSS class to change the appearance of the selected rows. $(".cbD ...

Execute SQL Server stored procedure passing a collection of dates to disable on a jQuery datepicker

I am currently working on a Classic ASP system that includes a jquery datepicker. My goal is to automatically disable certain days by populating an array with these dates. Currently, the functionality only allows for manual disabling of dates using a funct ...

Is it possible to create two header columns for the same column within a Material UI table design?

In my Material UI table, I am looking to create a unique header setup. The last column's header will actually share the same space as the previous one. Picture it like this: there are 4 headers displayed at the top, but only 3 distinct columns undern ...

Using CSS grid for optimal responsive design

Today I delved into the world of CSS grid and instantly saw its potential in creating stunning layouts. However, one aspect that has me perplexed is how to adapt the grid for mobile devices. Below is an example that functions perfectly on a desktop screen ...

The scroller's height is displayed at 100%

I'm experiencing an issue with the scrolling of a division. I've set the overflow to "scroll" for the division, but it's displaying at 100% height. Please refer to the screenshot provided. Can you advise me on which properties I should adjus ...

The map fails to load on the HTML page

I am struggling to load a map into my HTML page from a file located in the app folder. Despite using the correct code for inserting the map, it still does not load properly. <!-- Contact section start --> <div id="contact" class="contact"> ...

Phonegap Application: Page design gets distorted following keyboard input

After a user enters login details and the next page loads, the layout shifts as shown in the image below. The entire app layout breaks and the view moves down to accommodate the size of the onscreen keyboard. This issue persists unless the orientation is ...