Toggle the current list item in Jquery to expand to 100% width, while simultaneously shifting any adjacent items on the left and right downwards if present

I am working with a list (ul) that contains multiple items displayed in a tiled format. Each item has a brief introduction and can expand to show more information when clicked on. What I would like to achieve is for the expanded item to take up the entire width of the page, pushing other items on the left and right downwards. While my current CSS code works for the first item of each row, it does not work as expected for the rest. Is there a way to achieve this behavior using jQuery or CSS? Alternatively, is there a way to make the currently expanded item become the first item of its row? Thank you!

<ul class="resutls">
        <li>Item 1</li><li>Item 2</li><li>Item 3</li><li>Item 4</li><li>Item 5</li> ...
    </ul>

CSS

.results li {width:200px; height:140px; margin:7px 14px 7px 0; float:left;}
    .results li.current{width:660px; height:auto;}
    

Jquery

 $('li').toggle(function() {  
               $(this).addClass("current");
               $($(this).find('.js-toggleContent')).toggle("slow");
           }, function() {  
               $(this).removeClass("current");
               $($(this).find('.js-toggleContent')).toggle("slow");   
           });         
    

Answer №1

If you're looking to make a selected cell pop to a row directly above it, expanding to full width, then you'll need to combine CSS and JS while adjusting your HTML structure. I've had experience with this task multiple times and have refined my approach based on client feedback. You can see an example of how I typically handle this in the following fiddle: here

HTML

<ul class="results">
    <div class="detail hide"></div>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>
<ul class="results">
    <div class="detail hide"></div>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>
<ul class="results">
    <div class="detail hide"></div>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>

CSS

.results{
    background-color: yellow;
    margin: 0;
    padding: 0;
    position:relative;
    height:auto;
    overflow: visible;
    width:652px;
    display:block;
    list-style: none;
}
.results li {
    background-color:red;
    padding: 0;
    width:200px;
    height:140px;
    display:inline-block;
    margin:7px;
    list-style-type: none;
    cursor:pointer;
    -webkit-transition: all .5s linear;
    -moz-transition: all .5s linear;
    -ms-transition: all .5s linear;
    -o-transition: all .5s linear;
    transition: all .5s linear;
}
.detail{
    background-color:orange;
    padding: 7px;
    width:624px;
    position:relative;
    top:7;
    visibility:visible;
    left:auto;
    opacity: 1;
    margin:0 7px 7px 7px;
    cursor:pointer;
    -webkit-transition: all .5s ease-out;
    -moz-transition: all .5s ease-out;
    -ms-transition: all .5s ease-out;
    -o-transition: all .5s ease-out;
    transition: all .5s ease-out;
}
.hide{
    visibility:hidden;
    opacity: 0;
    position:absolute;
}
.fade{
    opacity: .2;
}

Jquery

$(document).ready(function () {
    'use strict';
    var $getDetails = $('.results div'),
          $getLis = $('.results li');
    $('.results').on('click', 'li', function() {
        $getLis.removeClass('fade');
        $getDetails.addClass('hide').empty();
        $(this).siblings('div')
            .html($(this).html())
            .addClass('hide')
            .removeClass('hide');
        $(this).addClass('fade');
    });
    $('.results').on('click', '.detail', function() {
        $getDetails.addClass('hide').empty();
        $getLis.removeClass('fade');
    });
});

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

Designing a dynamic wizard layout using Bootstrap 4 tabs, however encountering issues with the button functionality limited to a

My goal is to create a seamless navigation experience between tabs. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/b ...

"Using Ajax in Django results in the retrieval of the entire HTML page

In attempting to create a basic form using the bootstrap modal, I am encountering an issue where upon form submission, the response received is the entire HTML page. The request method being used is POST. Despite trying various solutions found on StackOver ...

Show only child elements of a specific type within the parent div

Looking to identify divs with the class 'test' that contain only buttons in their child nodes. This is the HTML code that needs to be filtered. <div class="test"> <div> <button> <span>Button 1</span></butto ...

Sending a form cancellation submission within a nested function call

I need help preventing my form from submitting in case the confirmation message is cancelled. How can I achieve this within the each() loop? $('#myForm').submit(function() { var inputs = $(this).find('input:checked'); inputs.ea ...

floating point for a list item compared to other elements

nav > ul > li { float: left; } #one { float: left; } <nav> <ul> <li> he has a cow </li> <li > he has a dog </li> <li> he has a mouse </li> </ul> & ...

How can I retrieve the dimensions of an image uploaded via ajax before resizing it to zero using jquery ui resizable?

When using jQuery ui resizable with images from an ajax upload, if the image width and height are not set in CSS (setting them to auto does not work either), the image will default to 0 0, making it invisible. How can this issue be fixed? Is there a way to ...

What could be causing my view to not load the jQuery code from _Layout.cshtml, yet it works when I include it directly in the view file?

Why is the jQuery inherited from Layout.cshtml not loading in my view? When I use a local link to jQuery in the view, it works fine. Otherwise, it doesn't. _Layout.cshtml: <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

How to effectively use flexbox to resize child elements in a CSS layout

My goal is to create a layout where a child element fills the entire flex box of a flex layout. Here is an example of the HTML structure I am using: <div class="parent"> <div class="child1"> should have 100px height </div&g ...

Creating an AJAX form using Jquery 3.3.1: A step-by-step guide

After updating my jQuery from version 1.10.2 to 3.3.1, I encountered a problem. Whenever I fill in the inputs and click the send button, my website simply reloads and the information from the inputs gets added to the URL of my website. The URL looks like t ...

ViewCheck and every

Can IsInView be set to wait for all elements with the same class, instead of calling them individually? For instance, if there are three sections, can they be called like this: $(".section").on('inview', function(event, isInView) { i ...

Ensure that the bottom border of the nav-item is in line with the

Is there a way to make the bottom border of a bootstrap nav item align perfectly with the bottom border of the navbar? My goal is to use the bottom border as an indicator for the selected element. I managed to achieve this with the default height of the na ...

Make the owl carousel slide come alive by having it smoothly move in a vertical motion from

Optimize your Owl carousel by implementing the code below to have the slide transition from right to left: $('.owl-carousel').owlCarousel({ rtl:true, loop:true, margin:10, nav:true, }); Consider expanding the carousel's vis ...

What causes the order of `on` handler calls to be unpredictable in Angular?

Below is the code snippet I have been working on function linkFunc(scope, element, attr){ var clickedElsewhere = false; $document.on('click', function(){ clickedElsewhere = false; console.log('i ...

SwipeJS is not compatible with a JQuery-Mobile web application

I am currently attempting to integrate SwipeJS (www.swipejs.com) into my JQuery-Mobile website. <script src="bin/js/swipe.js"></script> <style> /* Swipe 2 required styles */ .swipe { overflow: hidden; ...

How can I stop the space bar from activating its default action unless I am inside an input field

If we want to stop the default scrolling behavior when pressing the space bar on the document, we can use the following code snippet: $(document).keypress(function(event) { event.preventDefault(); }); However, this approach won't work if there ar ...

The mat-pagination component's mat-select feature is displaying unusual behavior

In a recent project I have created, users need to perform CRUD operations and view the output in a table. Everything is functioning properly, however, there are instances where the "mat-select" within the "mat-paginator" starts behaving strangely. This iss ...

How to save an image to a file using PHP and renaming the image file

To modify the default image name to imgName, it is necessary to send the parameter imgName along with the picture to PHP. var imgName = "30"; file_data = $('#pictureInput').prop('files')[0]; form_data = new FormDat ...

Break up a line within an ionic element by inserting a linebreak tag

Currently, I am constructing an app using ionic framework and facing a challenge in inserting a linebreak within an ionic tag to ensure proper display inside the designated container... <h2>{{caravan.model}}</h2> ...

Having issues with the sidebar malfunctioning and unsure of the cause

<html> <head> <title></title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> I've been working on creating a sidebar for my website, but it's not functioning as expect ...

What is causing my sprite image to be cropped?

I'm currently working on implementing a progress bar handle for my website. While the progress bar functions perfectly, I'm encountering an issue where the handle gets cut off when placed above the bar. Please refer to the attached image for refe ...