Despite applying the style to the image, my rotateY function is not functioning properly

I have created a unique slider feature where images rotate either 180deg or 360deg before sliding. However, I've encountered an issue that I can't seem to figure out. The slider works perfectly until the image reaches its initial position. At this point, even though the rotateY style is being added to the images (confirmed in the console), they still do not rotate as expected. Can anyone provide insight into this strange behavior?

const container = document.querySelectorAll('.image-container')
const nextBtn = document.querySelector('.next')
let counter = 0


nextBtn.addEventListener('click', moveSlide)

function moveSlide() {
  counter++
  n =(counter%2) * 180 +180
    if(counter===6){
      console.log(counter)
      image = document.querySelector(`.image-container:nth-child( ${counter}) img `)
      console.log(image)
      image.style.transform = `rotateY(${n}deg)`
        container.forEach((image, index) => {
            let width = image.getBoundingClientRect().width
            console.log(width)
            image.style.transform = `translate(0%) rotateY(0deg)`
            counter=0
        return
        })
    }else{
      console.log({counter})
      console.log({n})
        image = document.querySelector(`.image-container:nth-child( ${counter}) img `)
        console.log(image)
        image.style.transform = `rotateY(${n}deg)`
        container.forEach((image, index) => {
          let width = image.getBoundingClientRect().width
          setTimeout(() => {
            image.style.transform = `translate(-${(counter)*width}px`
          }, 450)
      
        })
    }


}
* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
  }
  
  body {
    display: flex;
    height: 100vh;
    overflow: hidden;
    align-items: center;
    align-content: center;
    justify-content: center;
  }
  
  .wrapper {
    display: flex;
    flex-direction: column;
  }
  
  .slider-container {
    height: 50vh;
    width: 300px;
    display: flex;
    margin: auto;
    flex-direction: row;
    /* overflow: hidden; */
    overflow: auto;
  }
  
  .image-container,
  .image-container img {
    display: block;
    width: 300px;
    transition: transform 450ms ease;
  }
  
  .btn-container {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: row;
    gap: 5px;
  }
  
  .btn-container .btn {
    width: 15vw;
    padding: 5px;
  }
<div class="wrapper">
    <div class="slider-container">
        <div class="image-container">
            <img src="https://picsum.photos/id/237/300/200" alt="">
        </div>
        <div class="image-container">
            <img src="https://picsum.photos/seed/picsum/300/200" alt="">
        </div>
        <div class="image-container">
            <img src="https://picsum.photos/300/200?grayscale" alt="">
        </div>
        <div class="image-container">
            <img src="https://picsum.photos/300/200/?blur" alt="">
        </div>
        <div class="image-container">
            <img src="https://picsum.photos/id/870/300/200?grayscale&blur=2" alt="">
        </div>
        <div class="image-container">
            <img src="https://picsum.photos/id/1/300/200" alt="">
        </div>
    </div>
    <div class="btn-container">
        <button class="btn prev">Previous</button>
        <button class="btn next">Next</button>
    </div>
</div>

Answer №1

After the second round of adjustments to the slides, the styling is now fully integrated into each one.

If you apply a 180deg rotation to an object that already has the same degree of rotation, nothing will change visibly.

To address this issue, one potential solution could involve:

  • Reverting the rotation back to 0deg once it's completed;
  • Dynamically managing the transition, considering timing such as 0ms when not in motion, and 450ms during rotation so that the reset back to 0deg goes unnoticed by the observer.

Answer №2

In order to fix the issue with the image rotation not being visible, you should remove the existing style:rotateY(180deg) and set it back to style:rotateY(0deg) right after the flip. It is recommended to use a setTimeout for better control over the rotation effect. Here is an updated version of the code (note that I changed the name of the image variable to imageNode in the forEach loop for clarity):

const container = document.querySelectorAll('.image-container')
const nextBtn = document.querySelector('.next')
let counter = 0

nextBtn.addEventListener('click', moveSlide)

function moveSlide() {
  counter += 1
  let n = 180
  
  if(counter === 6){
    console.log(counter)
    let image = document.querySelector(`.image-container:nth-child(${counter}) img`)
    console.log(image)
    image.style.transform = `rotateY(${n}deg)`
    
    container.forEach((imageNode, index) => {
      let width = imageNode.getBoundingClientRect().width
      console.log(width)
      imageNode.style.transform = `translate(0%)`
      counter = 0
      setTimeout(() => {image.style.transform = `rotateY(0deg)`}, 300)
      return
    })
  }else{
    console.log({counter})
    console.log({n})
    
    let image = document.querySelector(`.image-container:nth-child(${counter}) img`)
    console.log(image)
    image.style.transform = `rotateY(${n}deg)`
    
    setTimeout(() => {image.style.transform = `rotateY(0deg)`}, 300)
    
    container.forEach((imageNode, index) => {
      let width = imageNode.getBoundingClientRect().width
      
      setTimeout(() => {
        imageNode.style.transform = `translate(-${counter * width}px)`
      }, 450)
    })
  }
}
* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
  }

  body {
    display: flex;
    height: 100vh;
    overflow: hidden;
    align-items: center;
    align-content: center;
    justify-content: center;
  }

  .wrapper {
    display: flex;
    flex-direction: column;
  }

  .slider-container {
    height: 50vh;
    width: 300px;
    display: flex;
    margin: auto;
    flex-direction: row;
    /* overflow: hidden; */
    overflow: auto;
  }

  .image-container,
  .image-container img {
    display: block;
    width: 300px;
    transition: transform 450ms ease;
  }

  .btn-container {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: row;
    gap: 5px;
  }

  .btn-container .btn {
    width: 15vw;
    padding: 5px;
  }
<div class="wrapper">
    <div class="slider-container">
        <div class="image-container">
            <img src="https://picsum.photos/id/237/300/200" alt="">
        </div>
        <div class="image-container">
            <img src="https://picsum.photos/seed/picsum/300/200" alt="">
        </div>
        <div class="image-container">
            <img src="https://picsum.photos/300/200?grayscale" alt="">
        </div>
        <div class="image-container">
            <img src="https://picsum.photos/300/200/?blur" alt="">
        </div>
        <div class="image-container">
            <img src="https://picsum.photos/id/870/300/200?grayscale&blur=2" alt="">
        </div>
        <div class="image-container">
            <img src="https://picsum.photos/id/1/300/200" alt="">
        </div>
    </div>
    <div class="btn-container">
        <button class="btn prev">Previous</button>
        <button class="btn next">Next</button>
    </div>
</div>

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

Increase the identification of HTML element with jQuery

I've encountered an issue while trying to increment the id of 2 HTML elements, hwAddition and itemNumber, upon a button click event. The HTML code in question is as follows: <div id="hwAddition"> <div id="itemNumber" s ...

How can we effectively manage error responses and retry a failed call in NodeJS without getting tangled in callback hell?

I am in search of an effective approach to handle the given situation. I am curious if employing promises would be a helpful solution? Situation Overview: When a call retrieves a callback, and this callback receives an error object as a parameter. My obj ...

Error encountered with Ajax client-side framework while executing HTML code

When I run my project from Visual Studio using an aspx page that utilizes ajax modal popup extender, everything works fine with IE and Firefox as the default browsers. However, when I create an HTML file containing the code and open it by double-clicking, ...

Encountering an issue: Unable to initiate a local server when running `npm start`

Currently diving into the world of React, I successfully set up a React app. However, upon running npm install after typing cd davidsapp, I encountered numerous warnings and errors. Subsequently, when issuing the command npm start, all of the errors are di ...

Is there a way to postpone the collapse functionality in Bootstrap?

Is there a way to delay the display of collapsed elements in Bootstrap 4? For instance, how can you postpone the content of a Link href button from appearing in the example provided below? <p> <a class="btn btn-primary" data-toggle="collapse" ...

AngularJS directive: Cookie value not being applied to data binding operation

I've developed a directive that includes a dropdown list of organizations to which the user is assigned: .directive('orgList', ['$rootScope', '$state', 'Auth', 'ipCookie', function ($rootScope, $state ...

Caution: Refs cannot be assigned to function components

I'm currently using the latest version of Next.js to create my blog website, but I keep encountering an error when trying to implement a form. The error message reads as follows: Warning: Function components cannot be given refs. Attempts to access t ...

Is indexed coloring available for vertices in three.js?

I have recently started exploring the world of three.js and I am aware that there is a way to color vertices in three.js. However, I am currently researching whether it is possible to implement indexed colors for vertices in three.js or WebGL. Specifically ...

The Date.UTC function is not providing the correct output

While attempting to change Unix timestamps into a more understandable format, I used Date.UTC(2017,09,23);. When running this code, it returned 1508716800000. However, upon verifying on the website , the displayed result showed October 23, 2017 instead o ...

When consecutive DOM elements are hidden, a message saying "Hiding N elements" will be displayed

Provided a set of elements (number unknown) where some elements should remain hidden: <div id="root"> <div> 1</div> <div class="hide"> 2</div> <div class="hide"> 3</div> <div class="hide"&g ...

Unable to identify the pdf file using multer in node.js

const multer=require('multer'); var fileStorage = multer.diskStorage({ destination:(req,file,cb)=>{ if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/jpg' || file.mimetype==='image/png') { ...

Can you pass a specific function as a parameter in express.post()?

I have a snippet of code that is functioning correctly. I am wondering if it is possible to pass a pre-defined function as a parameter within express.post(). const exs = require('express'); const exs_r = exs.Router(); router.post('/click&a ...

Displaying the server's feedback upon successfully uploading a file onto the server

I am utilizing an HTML5 input control that allows users to upload a .csv file: <input id="ImportFile" type="file" name="ImportFile" data-val="true" data-val-required="Please select a file" title="Browse for a file to upload" /> This input control i ...

Is there a way for me to collaborate on a footer control with a different website?

Is it possible to seamlessly incorporate a footer from one website into another? Ideally, I want the footer HTML (and styles) to be dynamically inserted into different web pages. The common workaround is using an iframe, but this causes navigation issues ...

Tips on enlarging the header size in ion-action-sheet within the VueJS framework of Ionic

Recently I started using Vue along with the ionic framework. This is a snippet of code from my application: <ion-action-sheet :is-open="isActionSheetOpen" header="Choose Payment" mode="ios" :buttons="buttons&qu ...

View the picture directly on this page

Currently, I am in the process of creating a gallery and I would like the images to open on top of everything in their original size when clicked by the user. My expertise lies in HTML and CSS at the moment, but I am open to learning JavaScript and jQuery ...

In TypeScript, the catch block does not get triggered

I created a custom pipe in Angular that is supposed to format passed parameters to date format. The pipe contains a try-catch block to handle any errors, but surprisingly the catch block never seems to be executed even when an invalid date is passed. impo ...

Are there any CSS hacks available to address the combination of position: sticky and overflow within the parent element?

I've encountered a sticky position issue when the overflow property is set to auto on a parent element. I've tried various solutions like adding an extra wrapper or using clip instead of auto, but none have worked. While I did find a solution usi ...

What is the method to prevent the label from closing in the MUI 5 datepicker?

Is there a method to prevent the Material 5 Datepicker from closing when there's a label but no value? Current Scenario: Current Desired Outcome: Expected Sample Code: <LocalizationProvider dateAdapter={AdapterDayjs}> <DatePicker lab ...

Discovering common elements in various arrays of objects

Details: record1 = [{"site": "The Blue Tiger", "zipcode": "E1 6QE"}, {"site": "Cafe Deluxe", "zipcode": "E6 5FD"}] record2 = [{"site": "Blue Tiger", "zi ...