CSS/jQuery animation alignment problem

Exploring an animation using CSS transitions and jQuery has been my recent project. The concept involves presenting the user with clickable divs to load a new page. When a div is clicked, it expands to cover the entire screen and transition to the next page.

A visual representation of my goal is displayed below:

Initially, I arranged the divs with relative positioning and floated them left to align them. However, upon clicking a div, it would snap to the left side before expanding if it wasn't the first one (blue or green). This behavior was a result of the relative positioning.

position: relative;

You can view an example of this behavior here: http://codepen.io/anon/pen/eKynL

To address this issue, I switched to absolute positioning for the divs. The animation functioned flawlessly as intended, but the downside is that I now have to manually set the left and top positions for each div. Additionally, the divs do not automatically reposition when the window is resized.

position: absolute;

You can see an example of this here: http://codepen.io/anon/pen/jIqcL

I am uncertain if there is a better way to enhance my implementation and make it more maintainable, especially regarding the inline styles for the divs.

Answer №1

I referenced your position:relative; from the source link provided in this CodePen: http://codepen.io/anon/pen/eKynL

$(document).ready(function() {
  $('.mydiv').click(function() {
    if(!$(this).hasClass('clicked')) {
      var clicked_div = $(this);
      clicked_div.addClass('clicked');
      clicked_div.find('h1').fadeOut(150);

      $('.mydiv').not('.clicked').addClass('hide');
      clicked_div.addClass('animate');

      $('.loading').css('opacity', 1);

      setTimeout(function() {
        $('.mydiv').not('.clicked').css('display','none');
        $('.loading').css('opacity', 0);
        clicked_div.addClass('blog-post');
        clicked_div.find('h1').fadeIn(150);
        $('.blog-content').addClass('active');
      }, 1500);
    }
  });
});
*,*:before,*:after {
  box-sizing: border-box;
}
html, body {
  width: 100%;
  height: 100%;
  position: relative;
  padding: 0;
  margin: 0;
  overflow: hidden;
}
.mydiv {
  width: 200px;
  height: 200px;
  background-color: #bada55;
  position: relative;
  float: left;
  left: 0;
  top: 0;
  transition: width 0.5s, height 0.5s, left 0.5s, top 0.5s, opacity 0.25s;
}
.mydiv h1 {
  position: absolute;
  bottom: 0;
  left: 10px;
  color: #fff;
  font-family: sans-serif;
}
.teal {
  background-color: teal;
}
.orange {
  background-color: orange;
}

.mydiv.hide {
  width: 0px;
  height: 0px;
  opacity: 0;
}

.mydiv.animate {
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
}

.mydiv.blog-post {
  width: 50%;
}

.blog-content {
  background-color: #efefef;
  width: 50%;
  height: 100%;
  position: absolute;
  right: 0;
  top: 100%;
  transition: top 0.5s;
  font-family: sans-serif;
  color: #3c3c3c;
  font-size: 30px;
  padding: 20px;
}

.blog-content.active {
  top: 0%;
}

.loading {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto 0;
  width: 100%;
  height: 100px;
  text-align: center;
  font-family: sans-serif;
  font-size: 30px;
  font-weight: bold;
  color: #fff;
  opacity: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class="mydiv">
  <h1>Green</h1>
</div>
<div class="mydiv teal">
  <h1>Teal</h1>
</div>
<div class="mydiv orange">
  <h1>Orange</h1>
</div>

<div class="loading">Loading...</div>
<div class="blog-content">
  Content
</div>

codepen: http://codepen.io/anon/pen/lFIhH


In CSS, the only modification made was:

.mydiv {
  ...
  transition: ..., opacity 0.25s;
}

.mydiv.hide {
  width: 0px;
  height: 0px;
  opacity: 0;
}


In JS,

The line was changed from:

$('.mydiv').not('.clicked').fadeOut(150, function() {

to
$('.mydiv').not('.clicked').addClass('hide');

and this line was added inside the setTimeout-function:

$('.mydiv').not('.clicked').css('display','none');



This is how it functions:

  1. When the clicked div receives the .animate class, the unclicked divs receive the .hide class simultaneously.
  2. As the clicked div expands to fill the page, the other divs diminish in size and opacity to create a smooth effect during the transition.
  3. Eventually, in the setTimeout function, the unclicked divs have their display property set to none, completing the fade-out effect.

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

Updating parameter value upon the execution of an internal function in Javascript

How can I log the text from a textbox to the console when a button is clicked in this code snippet? <body> <input type="text" id="ttb_text" /> <script type="text/javascript"> function AppendButton() { var _text = ''; ...

PHP Email Form Data Submission

I've been struggling to get a form set up that will send the information to the admin upon submission, but so far, nothing seems to be working. I double-checked my junk and spam folders, and even made sure that $admin_email = "" is correctly set to th ...

having difficulty grasping the variable's scope within this code

Here we have a code snippet where the variable vid is initialized inside a function. The question arises on how this variable can be used outside the function, specifically in the context of vid.play(). How does the program know that vid was created usin ...

Not all comparisons between two tables are guaranteed to be successful

Looking to compare records within two arrays and apply a style if they are equal. Here is my approach: var json = ["VIP Section","Master Section","Press Section","God Section"]; var sections = ["VIP Section","Master Section"]; if ( sections.length &g ...

Running a <script> tag with an external src attribute in a dynamic manner through the use of eval

Currently, I am utilizing the Genius API to fetch lyrics for a particular song and then embed them within an HTML <div> tag. My interaction with this API is through PHP, employing an AJAX GET request. Upon a successful AJAX request, the following HT ...

Invoke a function using the output of a different function

There is a function whose name is stored in the value of another function, and I need to invoke this function using the other one. The function I need to call is popup() random() = 'popup()' if ($.cookie('optin-page')) { } I attemp ...

Increase the gap between columns in Bootstrap for a better layout

I have implemented a web layout using bootstrap 3. You can view the JSFiddle here. In order to make the layout responsive, I had to structure it in a slightly unconventional way. The final output on small screens includes three information boxes within an ...

How can we initiate the AJAX request right away while also making sure the page is fully loaded before adding

One trick I've discovered is that by submitting an AJAX request in the <head> section of my webpage, I can avoid a minor flicker on page load when loading content via AJAX. Of course, this method still needs some refining to handle longer AJAX r ...

I'm having trouble with accessing an object by index in a knockout observablearray. It seems like the binding process is encountering difficulties

I'm completely new to stackoverflow and knockout, and I have a query. I want to access a JSON object by index. It works fine when I insert dummy data in my code, but it throws an error when I try to add data from JSON. Error Uncaught TypeError: Unab ...

Why is it that this particular line of code sometimes gives me an "undefined" result before returning an actual value?

Why does the method 'equal' always return with an "undefined" followed by a number? And when I try to parse it, it returns me a NaN. <!DOCTYPE> <html> <head> <script> var fnum = 0; var secondNum; var operation; functi ...

Is the JavaScript progress bar dysfunctional when used with object-oriented JavaScript code, but functions properly with arrow functions?

After posting a question about the JavaScript progress bar not working with object-oriented JavaScript code on Stack Overflow, I decided to try rewriting the script using arrow functions. Here is the new code: document.getElementById("barButton").addEve ...

Tips for locating elements based on the final portion of their identifier using jQuery

When using jQuery, I need to locate an element based on its id. The original id is: txtHistoricalPricesDate In a typical situation, I would use the following code to find it easily: var element = $('#txtHistoricalPricesDate'); However, due to ...

Problem with Labels Overlapping and Blocking Text Fields

My issue is that when I interact with the text field, the label "Type your New Task here" moves up and gets covered by what looks like a white overlay. How can I fix this so that the label stays visible at all times? Prior to engagement: https://i.stack.i ...

What is the process for modifying the popups associated with a polygon object?

As of now, every time I click on the map, a popup shows up with the name of the country based on a geoJSON file containing multi-polygon lnglat coordinates that outline the borders of each country. This method saves me from manually inputting each country& ...

Compel a WordPress page to reload

==Current Setup== At the moment, I am utilizing Wordpress to showcase announcements. We have one server hosting Wordpress and four separate PCs that display the announcements. Each PC has its unique page URL for displaying the announcement. For instance: ...

Can a Javascript file be concealed from view?

Imagine a scenario where the localhost root file is served an HTML file using the following code: app.get('/', (req, res) => res.sendfile('index.html')) Is it possible to include JavaScript files in the HTML document that are not a ...

Make sure to load the <img> html tag before implementing js masonry for optimal

I am facing an issue with jQuery Masonry as it arranges my 'bricks' immediately without waiting for images to load in. Additionally, I need the images to be responsive so I cannot define their height and width like this: <img src="asas" heigh ...

"Utilizing Flask to efficiently manage a multitude of buttons and seamlessly update a

I am working on a web application similar to mini-tweets. The content for the posts is retrieved from a database and I want to include an 'up vote' button for each post, similar to the image shown below. https://i.sstatic.net/kJySO.png Each pos ...

Tips for aligning two canvases with different heights at the top in HTML5

My problem involves two canvases housed within a single <div>. Despite their different heights, they are currently aligned at the bottom. +-------+ + + +-------+ + + + + + + +-------+ +-------+ Is the ...

What causes the $_FILES superglobal to consistently appear empty?

Check out my page Take a look at my form: <h2>Upload Photo</h2> <form name="photo" enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"];?>" method="post"> Photo <input type="file" name="image" size="30" /> & ...