Home > Enterprise >  How to translateX to a position without removing transition property?
How to translateX to a position without removing transition property?

Time:04-17

I want to translateX to a position if change is more than -150, but due to having transition property in container it shows the animation of travelling to the new translate value. I want it to have directly jump to the -400px translateX value without showing the animation to going to it and still have the transition property in place for future scrolls

const config = {
    individualItem: '.slide', // class of individual item
    carouselWidth: 400, // in px
    carouselId: '#slideshow', // carousel selector
    carouselHolderId: '#slide-wrapper', // carousel should be <div id="carouselId"><div id="carouselHolderId">{items}</div></div>
   
}




document.addEventListener("DOMContentLoaded", function(e) {
    

    // Get items
    const el = document.querySelector(config.individualItem);
    const elWidth = parseFloat(window.getComputedStyle(el).width)   parseFloat(window.getComputedStyle(el).marginLeft)   parseFloat(window.getComputedStyle(el).marginRight);
    
    // Track carousel
    let mousedown = false;
    let movement = false;
    let initialPosition = 0;
    let selectedItem;
    let currentDelta = 0;

    document.querySelectorAll(config.carouselId).forEach(function(item) { 
        item.style.width = `${config.carouselWidth}px`;
    });
    
    document.querySelectorAll(config.carouselId).forEach(function(item) {
        item.addEventListener('pointerdown', function(e) {
            mousedown = true;
            selectedItem = item;
            initialPosition = e.pageX;
            currentDelta = parseFloat(item.querySelector(config.carouselHolderId).style.transform.split('translateX(')[1]) || 0;
        }); 
    });
    
    const scrollCarousel = function(change, currentDelta, selectedItem) {
        let numberThatFit = Math.floor(config.carouselWidth / elWidth);
        let newDelta = currentDelta   change;
        let elLength = selectedItem.querySelectorAll(config.individualItem).length - numberThatFit;
        if(newDelta <= 0 && newDelta >= -elWidth * elLength) {
         
 selectedItem.querySelector(config.carouselHolderId).style.transform = `translateX(${newDelta}px)`;
 // IMPORTANT LINE
                if(newDelta <= 0 && newDelta <= -150) {
     selectedItem.querySelector(config.carouselHolderId).style.transform = `translateX(-1000px)`;
              }
        }
    }

    document.body.addEventListener('pointermove', function(e) {
        if(mousedown == true && typeof selectedItem !== "undefined") {
            let change = -(initialPosition - e.pageX);
            scrollCarousel(change, currentDelta, document.body);
            
            movement = true;
        }
    });
    
    ['pointerup', 'mouseleave'].forEach(function(item) {
        document.body.addEventListener(item, function(e) {
            selectedItem = undefined;
            movement = false;
            
        });
    });

});
.slide-wrapper {
  transition: 400ms ease;
  transform: translateX(0px);
  width: 400px;
  height: 400px;
}

.slide-number {
  pointer-events: none;
}
<!DOCTYPE html>
<html>
<head>
    <title>HTML and CSS Slideshow</title>
    <style>
    body {
        font-family: Helvetica, sans-serif;
        padding: 5%;
        text-align: center;
        font-size: 50;
    overflow-x: hidden;
    }
    
    /* Styling the area of the slides */
    
    #slideshow {
        overflow: hidden;
        height: 400px;
        width: 400px;
        margin: 0 auto;
    }
    
    /* Style each of the sides
    with a fixed width and height */
    
    .slide {
        float: left;
        height: 400px;
        width: 400px;
    }
    
    /* Add animation to the slides */
    
    .slide-wrapper {
        
        /* Calculate the total width on the
    basis of number of slides */
        width: calc(728px * 4);
        
        /* Specify the animation with the
    duration and speed */
        /* animation: slide 10s ease infinite; */
    }
    
    /* Set the background color
    of each of the slides */
    
    .slide:nth-child(1) {
        background: green;
    }
    
    .slide:nth-child(2) {
        background: pink;
    }
    
    .slide:nth-child(3) {
        background: red;
    }
    
    .slide:nth-child(4) {
        background: yellow;
    }
    
    /* Define the animation
    for the slideshow */
    
    @keyframes slide {
        
        /* Calculate the margin-left for
    each of the slides */
        20% {
            margin-left: 0px;
        }
        40% {
            margin-left: calc(-728px * 1);
        }
        60% {
            margin-left: calc(-728px * 2);
        }
        80% {
            margin-left: calc(-728px * 3);
        }
    }
    </style>
</head>

<body>
    
    <!-- Define the slideshow container -->
    <div id="slideshow">
        <div id="slide-wrapper" >
            
            <!-- Define each of the slides
        and write the content -->
            <div >
                <h1 >
                    1
                </h1>
            </div>
            <div >
                <h1 >
                    2
                </h1>
            </div>
            <div >
                <h1 >
                    3
                </h1>
            </div>
            <div >
                <h1 >
                    4
                </h1>
            </div>
        </div>
    </div>
</body>
</html>

CodePudding user response:

If I understood your question properly, I think you would have to remove the transition property before changing the value and then apply it again once the transition is done.

const item = selectedItem.querySelector(config.carouselHolderId)
item.style.cssText = `transform: translateX(${newDelta}px); transition: none`;
// Restore the transition
item.style.transition = '';

CodePudding user response:

You could temporarily disable the transition:

const config = {
    individualItem: '.slide', // class of individual item
    carouselWidth: 400, // in px
    carouselId: '#slideshow', // carousel selector
    carouselHolderId: '#slide-wrapper', // carousel should be <div id="carouselId"><div id="carouselHolderId">{items}</div></div>
   
}




document.addEventListener("DOMContentLoaded", function(e) {
    

    // Get items
    const el = document.querySelector(config.individualItem);
    const elWidth = parseFloat(window.getComputedStyle(el).width)   parseFloat(window.getComputedStyle(el).marginLeft)   parseFloat(window.getComputedStyle(el).marginRight);
    
    // Track carousel
    let mousedown = false;
    let movement = false;
    let initialPosition = 0;
    let selectedItem;
    let currentDelta = 0;

    document.querySelectorAll(config.carouselId).forEach(function(item) { 
        item.style.width = `${config.carouselWidth}px`;
    });
    
    document.querySelectorAll(config.carouselId).forEach(function(item) {
        item.addEventListener('pointerdown', function(e) {
            mousedown = true;
            selectedItem = item;
            initialPosition = e.pageX;
            currentDelta = parseFloat(item.querySelector(config.carouselHolderId).style.transform.split('translateX(')[1]) || 0;
        }); 
    });
    
    const scrollCarousel = function(change, currentDelta, selectedItem) {
        let numberThatFit = Math.floor(config.carouselWidth / elWidth);
        let newDelta = currentDelta   change;
        let elLength = selectedItem.querySelectorAll(config.individualItem).length - numberThatFit;
        if(newDelta <= 0 && newDelta >= -elWidth * elLength) {
         
 selectedItem.querySelector(config.carouselHolderId).style.transform = `translateX(${newDelta}px)`;
 // IMPORTANT LINE
                if(newDelta <= 0 && newDelta <= -150) {
                  const el = selectedItem.querySelector(config.carouselHolderId);
                  el.classList.add("jump");
                  el.style.transform = `translateX(-1000px)`;
                  setTimeout(() => el.classList.remove("jump"), 10);
              }
        }
    }

    document.body.addEventListener('pointermove', function(e) {
        if(mousedown == true && typeof selectedItem !== "undefined") {
            let change = -(initialPosition - e.pageX);
            scrollCarousel(change, currentDelta, document.body);
            
            movement = true;
        }
    });
    
    ['pointerup', 'mouseleave'].forEach(function(item) {
        document.body.addEventListener(item, function(e) {
            selectedItem = undefined;
            movement = false;
            
        });
    });

});
.slide-wrapper {
  transform: translateX(0px);
  width: 400px;
  height: 400px;
  transition: 400ms ease;
  user-select: none;
}

.slide-number {
  pointer-events: none;
}

.slide-wrapper.jump
{
  transition-duration: 10ms;
}
<!DOCTYPE html>
<html>
<head>
    <title>HTML and CSS Slideshow</title>
    <style>
    body {
        font-family: Helvetica, sans-serif;
        padding: 5%;
        text-align: center;
        font-size: 50;
    overflow-x: hidden;
    }
    
    /* Styling the area of the slides */
    
    #slideshow {
        overflow: hidden;
        height: 400px;
        width: 400px;
        margin: 0 auto;
    }
    
    /* Style each of the sides
    with a fixed width and height */
    
    .slide {
        float: left;
        height: 400px;
        width: 400px;
    }
    
    /* Add animation to the slides */
    
    .slide-wrapper {
        
        /* Calculate the total width on the
    basis of number of slides */
        width: calc(728px * 4);
        
        /* Specify the animation with the
    duration and speed */
        /* animation: slide 10s ease infinite; */
    }
    
    /* Set the background color
    of each of the slides */
    
    .slide:nth-child(1) {
        background: green;
    }
    
    .slide:nth-child(2) {
        background: pink;
    }
    
    .slide:nth-child(3) {
        background: red;
    }
    
    .slide:nth-child(4) {
        background: yellow;
    }
    
    /* Define the animation
    for the slideshow */
    
    @keyframes slide {
        
        /* Calculate the margin-left for
    each of the slides */
        20% {
            margin-left: 0px;
        }
        40% {
            margin-left: calc(-728px * 1);
        }
        60% {
            margin-left: calc(-728px * 2);
        }
        80% {
            margin-left: calc(-728px * 3);
        }
    }
    </style>
</head>

<body>
    
    <!-- Define the slideshow container -->
    <div id="slideshow">
        <div id="slide-wrapper" >
            
            <!-- Define each of the slides
        and write the content -->
            <div >
                <h1 >
                    1
                </h1>
            </div>
            <div >
                <h1 >
                    2
                </h1>
            </div>
            <div >
                <h1 >
                    3
                </h1>
            </div>
            <div >
                <h1 >
                    4
                </h1>
            </div>
        </div>
    </div>
</body>
</html>

  • Related