Prevent Swiping Action Until Completion of CSS Animation

I have a series of equally sized boxes inside a container with absolute positioning, where the total width of the container equals boxWidth * n. This container is within a relative positioned parent container with overflow hidden, spanning multiple rows. When swiping left or right, the horizontal position of the box container adjusts by 1 boxWidth in the corresponding direction, ensuring it stays within bounds of (0, 0) and ((boxWidth * n), 0).

http://jsfiddle.net/rTe8U/8/

This represents a simplified version of my current setup, which functions well. However, if two swipes occur before the CSS transition completes, the incorrect 'current' position is referenced. How can I address this issue?

HTML

<div class="widget a">
    <div class="overflow">
        ...
    </div>
</div>
...

CSS

* {
    margin:0;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}
.widget {
    width: 200px;
    position: relative;
    height: 100px;
    overflow: hidden;
}
.overflow {
    transition: all 0.5s ease-out;
    -o-transition: all 0.5s ease-out;
    -moz-transition: all 0.5s ease-out;
    -webkit-transition: all 0.5s ease-out;
    height: 100px;
    position:absolute;
    left: 0;
}
.box {
    user-select: none;
    -o-user-select: none;
    -moz-user-select: none;
    -webkit-user-select: none;
    float: left;
    ...
}
.box:hover{
    cursor:pointer;
}

JS

var fullbox = '200',
    overflows = [$('.a .overflow'), $('.b .overflow'), $('.c .overflow'), $('.d .overflow')];

// Set Widths of Overflow Boxes
for (var i=0; i < 4; i++){
    overflows[i].width(overflows[i].children().length * fullbox);   
}

// Swipe Handlers
$('.overflow').on({
    swipeleft: function () {
        if ($(this).position().left != '-' + ($(this).children().length - 1) * fullbox) {
            $(this).css('left', '-=' + fullbox);
        }
    },
    swiperight: function () {
        if ($(this).position().left !== 0) {
            $(this).css('left', '+=' + fullbox);
        }
    }
});

Answer №1

If you want a seamless user experience, it's best not to interrupt the gesture while the boxes are transitioning. Users would likely expect the boxes to move in accordance with their gestures. One approach is to keep track of the index the user is on instead of constantly calculating box positions:

<div class="overflow" data-index="0">
    <div class="box">1</div>
    <div class="box">2</div>
    <div class="box">3</div>
    <div class="box">4</div>
    <div class="box">5</div>
</div>

Adjust swipe gestures to update the index accordingly:

$('.overflow').on({
  swipeleft: function () {
    var currentPosition = parseInt($(this).attr('data-index'));
    var totalBoxes = $(this).children('.box').length - 1;

    var nextPosition = currentPosition + 1;
    if (nextPosition >= totalBoxes) {
        nextPosition = totalBoxes;
    }

    moveBox($(this), nextPosition);
  },
  swiperight: function () {
    var currentPosition = parseInt($(this).attr('data-index'));

    var nextPosition = currentPosition - 1;
    if (nextPosition < 0) {
        nextPosition = 0;
    }

    moveBox($(this), nextPosition);
  }
});

The moveBox function:

function moveBox($ele, position) {
    var movePosition = parseInt(fullbox) * position;

    $ele
        .attr('data-index', position)
        .css('left', '-' + movePosition + 'px');
}

http://jsfiddle.net/rTe8U/11/

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

"Triggering an event after selecting and opening a file with an input of type file

Check out this snippet of HTML code: <input type="file" id="mysignature_upload" onChange="readURL();"/> We also have some Javascript code: function readURL(){ alert("Hello"); } A potential issue with this code is that the function readURL is on ...

How can I load a specific div region using ajax for submitting a form?

I'm not sure if my approach is correct. On my main page, I have some information displayed. When you click a button, the form will load from a separate file: $('#viewport').load('/views/myView.php #targetDiv') This works fine ...

What is the best way to target the final n children using CSS?

Is there a way in CSS to target and style the last N elements of a parent container? The number of elements we want to match is not fixed, but could vary. How can this be achieved using CSS? ...

What could be causing the overflow with the width set to min-content?

I am quite confused by the peculiar behavior of min-content in CSS. Let me share a concise example demonstrating this issue using Tailwind. <script src="https://cdn.tailwindcss.com"></script> <div class="h-screen w-screen"> <p c ...

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 ...

The transform operation has no effect whatsoever

Just a quick intro, I created an animated clock using CSS and HTML for homework, but now I want to enhance it by adding JavaScript functionality. I tried to sync the clock hands with the current time using JS, but for some reason, the animation always star ...

What is the best way to make floating images fill the entire space?

I'm looking to create a grid layout with fixed-sized images, but I'm having trouble getting them to fill up the space properly. Here's what I want: Unfortunately, this is what I'm currently getting: Any suggestions on how I can make t ...

The click event that is dynamically added is triggered multiple times

On clicking a button, I am dynamically adding a row to an HTML table. The row contains a cell with a list item (li) element which has a click event assigned to it. However, when I click on the list item, the event fires multiple times and I'm unsure w ...

Gradually make a section disappear and delete it in WordPress

Is there a way to smoothly fade out and remove a section after clicking on it in WordPress? I tried using the code below, which worked but doesn't seem to work for WordPress. I am using the Oceanwp theme, which allows me to easily add CSS and JavaScri ...

There seems to be an issue with Jquery Ajax retrieving information from an ASP.NET WEB API

Recently, I have started delving into APS.NET MVC WEB API programming. My current dilemma involves creating an ASP.NET WEB API project with the following code: public class ValuesController : ApiController { // GET api/values public IEnumerable& ...

Action triggered by clicking a button for every individual table column button

Managing my visitor records is made easier with a visitor table that allows me to add and display visitors. Each entry in the table includes a set of sign-in and sign-out buttons placed in two columns. Whenever I click the sign-in button, the current times ...

What methods can I use to customize the appearance and add animation to a checkbox using jquery?

On my frontend interface, I have an HTML checkbox input. I am curious about the best approach for styling the standard tickbox to make it more visually appealing. Perhaps using a slider-switch? I want to avoid individual browsers styling the checkbox dif ...

Changing the Size of a Youtube Video Based on the Width of a DIV Element

When embedding a Youtube video iframe, it is important to ensure that it scales according to the width of the containing div. The width of the div can vary depending on the layout of the page snippet. The aspect ratio of the Youtube video is 560/315, widt ...

What could be causing the inconsistency in my CSS performance in IE8?

I am encountering an issue with the CSS on my website specifically in IE8. The problem lies within the CSS intended to outline input elements labeled with class="error" with a 2px red border. While this function performs as expected for some input elements ...

Retrieve the ID of a control triggered by the onKeyup event

I am looking to locate the ID of an input control. The following is the HTML CODE: <div id="product"> <div class="inputHolderOrder"> <p style="margin-bottom: 2px;">Product Name</p> <input type="text" id="pro ...

Position two div elements and an hr tag to create a step progress bar

Check out my progress bar design https://i.sstatic.net/u9SRM.png How can I center the 'created' and 'assigned' divs below the circle while ensuring the 'hr' line touches the circle, and it is responsive across different scre ...

Animating SVG elements with the rotation property in SVG.js

Currently, I am utilizing Javascript, Jquery, SVG, and SVG.js in my project. My goal is to rotate a circle around its origin while it's animating. The issue arises when the animation keeps restarting abruptly, causing a jittery motion. Here is the ini ...

Having trouble choosing the correct option using ajax

Having trouble loading a drop-down properly using ajax, possibly due to a conflict with the class(select_wrapper) that loads css, which could be triggered by a js script. I am working with these 2 drop down menus: <div class="select_wrapper"> ...

Creating a dynamic and intricate HTML list item using jQuery within a loop

Currently, I am faced with a situation where I have a large amount of JSON data that needs to be processed. My objective is to output each "data" item on the page as a list item. While I have a working solution in place using jQuery, I am concerned about t ...

transforming a table into a stylized CSS design

Seeking assistance in converting a table into CSS as I am new to CSS: <table dir="ltr" width="500" border="0" align="center"> <caption><font face="Helvetica">Movies</font></caption> <colgroup width="50%" /> ...