Animating content to slide into view in the same direction using CSS transitions

My goal is to smoothly slide one of two 'pages', represented as <divs>, into view with a gentle transition that allows the user to see one page sliding out while the other slides in. Eventually, this transition will be triggered from the backend, most likely via a WebSocket message.

I want the pages to slide in from the left and for the effect to continue in a loop; meaning when returning to the first page, it should still slide in from the left.

To achieve this, I have created a snippet below showcasing my current plan which involves using a CSS3 transition. However, I find the implementation to be somewhat clunky.

The process works as follows:

#page1 starts in view, while #page2's horizontal position is offset by its width through the use of the in-frame and out-of-frame classes respectively.

For the transition from #page1 to #page2, I simply apply the .slide class which uses transform:translateX() to reposition both 'pages' by their width, and the transition property of the page class handles the transition effect.

In order to create a loop-like behavior, I wait for the transition to finish and then toggle which page has the in-frame or out-of-frame class to bring #page1 back off-screen to the left of #page2. However, to prevent this position change from running as a transition, I have to use the .notransition class, which feels like a workaround.

My Question: Is there an improved way to achieve this behavior using CSS transitions?

I am aware that Bootstrap's carousel can accomplish this for me as shown here, but I would like to know if and where I might be going wrong.

// add transition finished handler to each page
$('.page').on('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function() {
  // toggle in/out of frame
  $(this).toggleClass('in-frame')
    .toggleClass('out-of-frame')
    .addClass('notransition')
    .removeClass('slide')
})

$('body').click(function() {
  // start transform
  $('.page').removeClass('notransition').addClass('slide')
})
html,
body {
  height: 100%;
  overflow: hidden;
  text-align: center;
  font-size: 2em;
}

#content {
  position: relative;
  height: 100%;
}

.page {
  position: absolute;
  top: 0;
  height: 100%;
  width: 100%;
  -webkit-transition: transform 2s cubic-bezier(0.19, 1, 0.22, 1);
  -moz-transition: transform 2s cubic-bezier(0.19, 1, 0.22, 1);
  -o-transition: transform 2s cubic-bezier(0.19, 1, 0.22, 1);
  transition: transform 2s cubic-bezier(0.19, 1, 0.22, 1);
}

.slide {
  -webkit-transform: translateX(100%);
  -moz-transform: translateX(100%);
  -o-transform: translateX(100%);
  transform: translateX(100%);
}

.in-frame {
  left: 0%;
}

.out-of-frame {
  left: -100%;
}

.notransition {
  -webkit-transition: none !important;
  -moz-transition: none !important;
  -o-transition: none !important;
  transition: none !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content">
  <div id="page1" class="page in-frame" style="background: #d67e56;">
    Main Page <br> (Click Me)
  </div>
  <div id="page2" class="page out-of-frame" style="background: #d94e4e;">
    Other Page <br> (Click Me)
  </div>
</div>

Answer №1

I think your approach is solid, but here's another idea using CSS transitions in combination with a setTimeout() function. You can create a variable to control the click events and ensure that one transition finishes before allowing another click.

//initialize a variable
var finish = true;
$('body').click(function() {

    //check if previous transition has completed
    if(finish == true){ 

      //set variable to false
      finish = false;
        // initiate transform
        $('.page').removeClass('notransition').addClass('slide');

        setTimeout(function(){ 
             finish = true;
             $('.page').toggleClass('in-frame').toggleClass('out-of-frame').addClass('notransition').removeClass('slide')
              }
        ,2000);
    }
});

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

Utilizing Smart Table for Data Binding with JSON Dataset

I need help binding a JSON file to a smart table. How can I use the loop function for iteration? The design of the smart table is displaying, but the data from the JSON file is not binding. Here is the JSON file: [ { "year": 2013, "id ...

Passing a variable to a modal in AngularJS

In my project, I am utilizing https://github.com/simpulton/angularjs-wizard and have made some modifications to it (specifically changed var app to $scope). It is functioning well, however, I am facing an issue where I need to pass a variable to the open f ...

Using the mpdf addPage function generates new empty pages

Hey there, I'm facing an issue where using $mpdf->addPage() within a for loop results in creating blank pages instead of adding content. My goal is to generate a new page when a certain condition is met and then populate it with the required conten ...

Utilizing the :after pseudo-element for placing text in a specific location

Here's the code I'm working with: div#myImg{ background: url('myimage.png') left top no-repeat; } div#myImg:after{ content: 'TEXT UNDER IMAGE'; margin:0 auto; vertical-align:text-b ...

There seems to be an issue with your SQL syntax that is preventing the data from being entered correctly into the database using Node.js, MySQL, and Express

After successfully displaying data from my database, I attempted to add data to it using the following code snippet: exports.tambahData = (req, res) => { var keyy = req.body.keyy; var valuee = req.body.valuee; var brand = req.body.brand; ...

Is it possible to transfer data between the beforeSend and success functions within the jQuery .ajax method?

The Issue When utilizing AJAX to request data from a remote API, the asynchronous nature of the request means that responses come back whenever they are ready. The challenge arises when making multiple calls to the same API with different parameters, as i ...

directive in Angular ordering

After exploring this link, my understanding deepened: http://plnkr.co/edit/k5fHMU?p=preview Upon analyzing the code snippet provided in the link above, I noticed that the Angular app consists of both a controller and a directive. However, there seems to ...

Are there alternative methods for inserting data into an array in JavaScript?

I'm having trouble with my code that is supposed to push data to an array variable, but instead I'm getting a blank array. Can someone help me figure out what I'm doing wrong? let users = [] // Here is where I'm looping through an aja ...

React components to separate static PDF pages

I have successfully implemented a feature in my React App that allows me to export a long page in PDF format using html2canvas and jsPDF. The code snippet for exporting the page is as follows: html2canvas(document.body).then((canvas) => { var img ...

Referencing an object by using a variable containing its name

I need a way to reference an object using a variable with its name in it. I've figured out how to do this for properties and sub-properties: var req = {body: {jobID: 12}}; console.log(req.body.jobID); //12 var subProperty = "jobID"; cons ...

Creating a custom loading spinner featuring your logo

Hello, I am looking to create a custom loading spinner using CSS, JS or any other method with my logo. I have the logo in PNG, GIF, and SVG formats and would like to use it for long site loads, image loads, or any other loading processes within my Vue3 pro ...

Numerous textareas fail to function properly according to JQuery's standards

Need help with resizing multiple textarea elements dynamically as the user types in them (on the Y-axis). Currently, I have code that successfully resizes a single textarea, but it does not work when there are multiple textareas on the page. Here is the c ...

Having difficulty managing asynchronous functions with the useState hook in React

import React from "react"; import { UserContext } from "./../contexts"; import { removeStoredAuthData, storedAuthIsValid, storeNewAuthData, } from "./../utils/auth"; import { getUserInfos } from "./../api/userAuthen ...

Event in jQuery that is fired when clicking outside of a list

How can I trigger a click event on every item in a nested list and return the text of the clicked item? The issue I am facing is that when I click outside the list, it still triggers the click event and returns the text of the list item in the correspondin ...

Tips for applying stroke and shadow effects specifically when the mouse is moving over a canvas:

How can we create a shadow and stroke effect for circles drawn using HTML canvas only during mouse hover? I have two circles and would like to add a shadow and stroke effect when the mouse hovers over them. However, the issue is that once the mouse moves ...

Resize a group of images to match the parent's width and height dimensions

I am working with a div that contains variously-sized images and is nested inside a parent container. <div id="parentContainer"> <div id="boxToScale"> <img src="http://placehold.it/350x150" /> <img src="http://placehold.it/150 ...

creation of objects using constructors in Node.js

As I delve into the world of Node.js, a burning question has arisen - how can I make a function accept multiple strings in the form of an array? Picture this scenario: export default (config: Config) => { return { target: 'https://google.com ...

Utilizing state in React for CRUD operations is a fundamental practice

Currently, I am developing a React application using altjs as my Flux implementation. I have encountered an issue where when attempting to create or delete an item from the front end, regardless of the parameter passed to the function, the entire state is ...

What is the most effective way to ensure a user's email address is not already in my database before submitting a form?

Welcome to my first question post. I'm in the process of creating a test where users can sign in and retake it if they wish. However, I'm facing a challenge in checking whether the user's email already exists in my mariaDB database when they ...

Convert the list items into JSON format

<div class="col-xs-4 no-padding contenteditable-color" style="background-color: transparent;"> <ul class="ul-product"> <li class="li-product-page cars contenteditable" contenteditable="false">qweryt</li> </ul&g ...