3D Carousel with Dynamic CSS, Perfect for Z-Axis Positioning

I am currently working on creating a 3D carousel using CSS and javascript. To view what I have accomplished so far, you can visit this page:

Upon arriving at the page, please click the "initialize" button to transform the carousel into 3D space. By default, it will start with 5 panels, but this number can be adjusted using the controls.

The issue I am facing involves the size of the panels when increasing their number. As more panels are added, they seem to get larger due to the increased distance from the origin point. What I aim to achieve is for the front panel to always maintain a consistent size, regardless of the total number of panels present.

Instead of pushing every panel out by a certain distance, I want the front panel to remain static in 3D space, while the other panels adjust around it (I hope that explanation makes sense).

I have implemented this using angular, but it could easily be done with plain javascript as well. Below is the relevant code:

HTML

<div id="musicPlayer">
    <section class="container">
        <div id="carousel">
            <figure class="something" ng-repeat="item in items">item {{item.someKey}}</figure>
        </div>
    </section>
</div>

CSS

.container {
    width:300px;
    height:300px;
    position:relative;
    perspective: 1000px;
    margin-left: 400px;
    margin-top:100px;
}

#carousel {
    width: 100%;
    height: 100%;
    position: absolute;
    transform-style: preserve-3d;
}

#carousel figure {
    display: block;
    position: absolute;
    left: 10px;
    top: 10px;
    border: 2px solid black;
    width: 276px;
    height: 276px;
}

Javascript

$scope.items = [];

for (var i = 0; i < 5; i++) {
    var filler = {
        someKey: i
    };
    $scope.items.push(filler);
};

$scope.initializeCarousel = function () {
    var carousel = document.getElementById('carousel');
    var numPanels = $scope.items.length;
    var theta = 360 / numPanels; // rotation between each panel in 3D space
    var radius = Math.round(150 / Math.tan(Math.PI / numPanels)); // how far in Z-axis the panels are pushed out

    //rotate panels by theta
    for (var i = 0; i < $scope.items.length; i++) {
        var panel = carousel.children[i];
        var angle = theta * i;
        panel.style.transform = 'rotateY(' + angle + 'deg) translateZ(' + radius + 'px)';
    };
};

Each time the "initialize" button is clicked, the $scope.initializeCarousel function is triggered, based on the selected number of panels.

I suspect that the issue may lie within the CSS styling rather than the javascript logic, but as a newcomer to CSS animations, I'm not entirely sure. Any assistance or advice on solving this matter would be greatly appreciated. Thank you!

Answer №1

It appears that determining the radius of the sphere is crucial in this situation, as you may need to adjust all panels in the z-direction by at least the length of the radius forward. An alternative approach could involve moving the camera, your viewpoint, further away from the object for a better perspective.

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 display the counter result on the cards at the top of an html webpage?

My latest project involves a comparison script using Groovyscript to analyze two distinct files. Embedded within the Groovyscript is HTML code that utilizes CSS and potentially Bootstrap of any version. I have successfully implemented counters with a titl ...

The issue of "Next.js localStorage not being defined persists, despite attempting to

Despite encountering numerous similar questions, I have not been able to find a solution to my specific issue. This is my first experience utilizing Next.js and TypeScript. I am conducting a mock login with REQRES and storing the token in the localStorage ...

Selenium was unable to find the p tag element on the webpage

I apologize for reaching out to you all for assistance with such a basic task, but I have tried everything in my toolkit and still can't seem to figure it out. I've experimented with partial xpath, full xpath, and various CSS selectors. I am see ...

Implementing an event listener on an anchor element dynamically inserted through Javascript

I made an Ajax call that retrieves a list of movie titles. I am trying to click on a button next to each title in order to add it to my "currently watching" list. However, my "add" link is not responding to the event handler. What steps can I take to suc ...

Apply a common class to all elements sharing the same href attribute

Can someone help me figure out how to add a class to all elements that have the same href as a clicked anchor tag? I understand how to add a class to one element, but I'm unsure about adding a class to multiple elements with the same href. $('. ...

Exploring the power of parent-child functions in Ajax promises

Trying to validate tag input against a database using an ajax call with taggle.js. The taggle documentation states to use return false on the onBeforeTagAdd event in order to prevent a tag from being added. However, due to the asynchronous nature of ajax ...

Troubleshooting a React Node.js Issue Related to API Integration

Recently, I started working on NodeJs and managed to create multiple APIs for my application. Everything was running smoothly until I encountered a strange issue - a new API that I added in the same file as the others is being called twice when accessed fr ...

What is the best way to align a TabPanel component at the center using React Material UI

As I attempt to compile a list of articles while switching to a list of other entities in React + material UI, I have encountered some difficulties. Specifically, I am struggling to center the Card displaying an article in alignment with the centered Tabs. ...

Tips for incorporating tabs using jQuery

Check out this code snippet: <script> $(function() { $("#tabs").tabs({ event: "mouseover" }); $("#tabs").tabs("add","#tab-4","Friends Discussions"); $("#tabs").tabs("add","#tab-5","Announcements"); $("#tab ...

Utilizing the LinkedIn API: Posting a network update using a variable value

Currently, I have a string variable and I am looking to post the value stored in this variable as a network update on LinkedIn. Could someone please provide guidance on how I can modify the code below to make this functionality possible? updateURL = "/pe ...

Learn how to implement a split background effect by simply clicking the SPLIT button

let context = canvas.getContext("2d"); // for canvas size var window_width = window.innerWidth; var window_height = window.innerHeight; canvas.width = window_width; canvas.height = window_height; let hit_counter = 0; // object is created using class clas ...

The CSS styles are failing to be applied to the submit button once it has been

When I have a form with a submit button that triggers a form submission on a PHP page, I want to have an onclick event on the submit button that changes its opacity using JavaScript. The issue I am facing is that after clicking the submit button, the opaci ...

Arranging block-level elements in a horizontal format

In the book "CSS The definitive Guide", it is mentioned that "The values of these seven properties must add up to the width of the element’s containing block, which is usually the value of width for a block element’s parent". However, in the case provi ...

Best practices for organizing Angular components into smaller, reusable parts

Recently, I've encountered a simple system user listing (you can see it in the attached screenshot) where clients have the option to edit existing users or create new ones. Depending on the user's action, a new panel is displayed with relevant in ...

Bootstrap 4's Grid System: Embracing the Square Grid

Is there a way to create a dynamic grid of squares in Bootstrap 4? I understand the concept of creating a grid using <div class="row"> <div class="col-md-3" style="width: 100px; height: 100px; color: red"></div> <div class="col-m ...

Animating Divs with jQuery to Expand their Size

I am currently designing a services page for my portfolio website. The layout consists of three columns, with the central column containing a large box and the left and right columns each containing three smaller boxes. These smaller boxes function as clic ...

Solid Text Overlay on Transparent TextArea in JavaFX/CSS

In my JavaFX CSS style sheet, I currently have the following code for a tag called #myText in my FXML file. This code results in a black textArea with red text, which is exactly what I want. However, I now want to make the background of the textArea tran ...

Reducing the gridview size with the help of the Bootstrap framework's stylesheet

Seeking advice on adjusting the size and alignment of a Gridview in asp.net web forms using bootstrap stylesheets. Here's the code snippet in question: <table class="table table-sm" > <a ...

In JavaScript, the condition can function properly even without the "&&" logical operator, but if the "&&" is included, the condition does not operate

I need the error message to be shown if the character length in the #nameT id is less than 3 or greater than 20. When I use the && logical operator, it does not function correctly, as when the && is not present, it works perfectly when ex ...

A guide on merging Django data into JavaScript

One of the challenges I faced was passing two lists from a Django view to an HTML template. Here is how I achieved it: views.py sale_day = dash[0] sale_amt = dash[1] context_dict = {'sale_day': sale_day, 'sale_amt': sale_amt} return r ...