Reach multiple divs that are layered on top of each other

I currently have 4 overlapping divs arranged in a cover flow style. You can view an example HERE.

Although I have successfully stacked them one over another, I am facing three main issues with the z-index:

  1. Only the first div (#product-image) responds to click events while the other 3 do not.
  2. My attempt to toggle the perspective view smoothly is not working as expected.
  3. Despite specifying the wrapper's width and setting margin:0 auto, the entire cover flow is not centered.

I tried following this relevant SO answer CSS: Overlapping DIVs issue, but it only solved the problem for up to 3 divs, which does not apply to my scenario.

Below is the structure:

HTML
<div class="product-download">
    <div id="product-image">
        <img src="http://placehold.it/300x216" />
    </div>
    <div id="in-situ-image">
        <img src="http://placehold.it/300x216" />
    </div>
    <div id="product-flyer">
        <img src="http://placehold.it/300x216" />
    </div>
    <div id="data-sheet">
        <img src="http://placehold.it/300x216" />
    </div>
</div>
CSS
body {
    overflow:hidden;
    width:100%;
    margin:0 auto;
}
.product-download {
    position:relative;
    width:934px;
    height:397px;
}
.product-download > div {
    position:absolute;
    display:inline-block;
}
#product-image {
    z-index:6;
}
#in-situ-image {
    -webkit-transform: perspective(600px) rotateY(-30deg) translateZ(-100px);
    z-index:5;
    left:120px;
}
#product-flyer {
    -webkit-transform: perspective(600px) rotateY(-30deg) translateZ(-100px);
    z-index:4;
    left:265px;
}
#data-sheet {
    -webkit-transform: perspective(600px) rotateY(-30deg) translateZ(-100px);
    z-index:3;
    left:410px;
}
JQuery
$(".product-download div").click(function () {
    alert(this.id);
    $(this).fadeTo('slow').css('-webkit-transform', 'perspective( 0px ) rotateY( 0deg ) translateZ(0px)');
    $(this).prevAll().fadeTo('slow').css('-webkit-transform', 'perspective( 600px ) rotateY( 30deg ) translateZ(-100px)');
    $(this).nextAll().fadeTo('slow').css('-webkit-transform', 'perspective( 600px ) rotateY( -30deg ) translateZ(-100px)');
});

Answer №1

After experimenting with the code, I discovered a way to make the elements clickable by adjusting the z-index. Setting the transform property on the parent element seemed to resolve the issue of other elements being clickable again, although I'm not entirely sure how it works.

CSS:

.product-download {
    position:relative;
    width:934px;
    height:397px;
    -webkit-transform: perspective(600px) rotateY(0deg) translateZ(0px);
}

VIEW DEMO HERE

Note: Please test this solution and let me know if it resolves the issue for you. It appeared to stop working briefly for me, so feedback is appreciated.


To address the z-index issue, you will also need to make changes to prevent overlapping.

I simply added:

.css('z-index','1');

At the end of both prevAll() and nextAll().

Then within fadeTo(), include:

.css('z-index','9999');

This is a basic example and can be refined further. I hope this explanation helps!

VIEW UPDATED DEMO HERE


The z-index issue has been successfully resolved.

jQuery:

$(this).nextAll().each(function (index) {
    $(this).fadeTo('slow').css('-webkit-transform', 'perspective( 600px ) rotateY( -30deg ) translateZ(-100px)').css('z-index', '-' + index);
});

VIEW IMPROVED DEMO HERE

Answer №2

You are facing two issues: one related to the z-index property and another with the translateZ property

To address the z-index problem, you can implement the following code:

$(".product-download div").click(function() {
    // Determine the total number of items and establish maximum and minimum z-index values
    var max_zindex = $(".product-download div").length;
    var min_zindex = 1;
    // Retrieve the index of the item    
    var index = $(".product-download div").index(this);
    // Adjust the z-index for all divs
    $(this).css('z-index', max_zindex);
    var id = $(this).attr('id');
    $(".product-download div:not('" + id + "')").css('z-index', min_zindex);
});

The primary concern lies within the translateZ property.

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

Manipulate CSS using jQuery's ID selector

I am attempting to modify a particular style of a div by its unique id, but it appears that my code is not functioning properly. The relevant codes are provided below: Jquery: $(document).ready(function () { $(".a_10.08.201223:56:49").hover(function(){ ...

Adjust positioning of navigation when hovered over

Need help creating a cool navigation effect like this. Live example: https://hookandbarrelrestaurant.com/ Here is my code: https://codepen.io/Dhaval182/pen/rQPMoW ...

Yii framework is failing to display a view after an ajax request, leading to it not being shown in the browser

Within a section of my code where this issue arises, the logic is as follows: View1 receives some information with links. When a user clicks on link X, a post request is sent to a controller on the XAMPP web server to open View2 from link X. All of this i ...

Tips for choosing an ID in Jquery to use in an if statement

I'm trying to create a form selector that will display different outputs depending on the selected option. However, I'm having some trouble getting it to work. Below is my current code: <!doctype html> <html> <head> <meta ch ...

Exploring the power of knockoutjs through the checkbox checked binding

I can't seem to get the knockoutjs checked binding to function as intended. I'm not sure if I'm making a mistake or something else is causing the issue. Here is the HTML snippet I'm working with: <ul data-bind="foreach: ListItems" ...

Utilizing Jquery for seamless page transitions in Rails with the help of Ajax calls

I am currently working on a project that involves creating two pages. The first page will display a selection of items that I want to showcase on the second page. On the initial page, I have already picked out the specific items that I plan to show on the ...

AJAX request fails to invoke upload.php script

After selecting an image, adding some text, and clicking submit, the progress bar shows that the upload was successful. However, the upload.php file is not being called as expected. Question: What could be causing the issue in my code that is preventing t ...

Display a DIV next to the mouse cursor when a span is hovered over

Is there a way to make a DIV element appear at the mouse cursor when a user hovers over a SPAN or another DIV? I attempted to create a function for this purpose, but unfortunately, it does not seem to be working properly even though jQuery is correctly lo ...

What is the best way to ensure the second navbar stays at the top when scrolling, after the first one?

Looking for a solution to create a navbar with two distinct sections for contact info and the menu. The goal is to have the contact info disappear when scrolling down, while the menu remains fixed at the top of the page. When scrolling back up, the menu sh ...

Update the content retrieved through ajax periodically

Looking to set up a periodic update for this ajax fetch function, say every 10 seconds. I've tried a few approaches but none seem to work as expected. That's why I'm reaching out here for help. So, any idea how I can refresh the content ev ...

Sending an HTML parameter via AJAX to a C# MVC Controller Method

I am attempting to send an HTML string as a parameter using the following method: $.ajax({ url: sourceUrl, type: "GET", cache: false, success: function (data, status, jqxhr) { etc... } In this case, sourceURL is calling a C# Controller ac ...

Ways to apply Position Fixed to a particular Div element and subsequently eliminate that class while scrolling

Check out my current Jsfiddle project here I am working on a task that involves adding a class to a specific div when scrolling and then removing that class. Below is the JavaScript code I have written for this functionality: var targetDiv = $(".mainwra ...

Stylishly implementing Bootstrap for input elements within labels

Incorporating an input or a textarea within a label and presenting it inline in a Bootstrap 3 styled form has been a challenge for me. Though the code snippet provided below is merely a sample, my original implementation is more intricate. I aim to ensure ...

The color is missing in the custom button of Bootstrap v5.2

Trying to incorporate a unique button class in Bootstrap with an iris-purple color. Utilized Sass for creating the custom button: @import "../node_modules/bootstrap/scss/bootstrap"; $mynewcolor:#5D3FD3; .btn-purple { @include button-variant( ...

Positioning a full-width background-image in the center is causing the page to expand

I've been struggling to find a solution to this issue for hours, scouring the internet with no success. The problem I'm facing is trying to center a background image on the page. However, when using the method shown here, it ends up stretching t ...

"Troubleshooting a Placement Concern within the ExpressionEngine Content Management System

I am facing a positioning problem with my ExpressionEngine. If you click here, you will see that I am looping elements within the <div id="services-container">. The loop works fine on the first row, but misaligns on the second row. I have tried using ...

Loop through the data in a jqGrid subgrid

I am interested in iterating over all the data within my grid object. The grid is defined with a subgrid object created as shown below: var grid = $(gridID); var pager = $(pagerID); grid.jqGrid({ url: GetBaseWSUrl() + 'MyWs.asmx/MyMethod', ...

Error message: The Modelviewer is unable to load the local file, displaying the message "Unauthorized to access local resource."

For my WebAR project, I am utilizing Google's ModelViewer. In my vue.js project, I have a component that loads the model using the <model-viewer> attribute. My goal is to set the src attribute of the model-viewer to the absolute path of the .gl ...

What is the best way to align these boxes so they fit perfectly together?

check it out here code snippets <div id="container"> <div class="box">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus ac magna non augue porttitor scelerisque ac id diam. Mauris elit velit, lobortis sed interdum at, vestibu ...

Designing webpages by superimposing text onto images with customizable positioning

My current project involves developing an HTML document with a floor plan image as the main layer. On top of this image, I need to display employee names extracted from a database and positioned on the map based on a location variable within the database ...