Change the direction of the div animation from horizontal to vertical

I have a bunch of divs with fixed dimensions that float horizontally, but I want them to animate vertically once one of them is clicked. Looking for a simple plugin or some code help!

Example:

|-------| |-------| |-------| 
|   1   | |   2   | |   3   | <-- When Box 1 is clicked, boxes 2 and 3 should animate
|_______| |_______| |_______|     vertically instead of their current horizontal alignment.
              |                   The animation does not need to be straight - they can move
|-------|     V         |         over or on top of each other as long as it goes from
|   2   |   <--         |         horizontal to vertical orientation.
|_______|               |
                        |
|-------|               V
|   3   |      <----
|_______|

Answer №1

Creating scripts like this is always a fun challenge. For those experiencing issues with JSFiddle, feel free to check out the codepen version:

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

Here's the code for future reference:

HTML:

<div id="boxcontainer">
  <div class="box box1"></div>
  <div class="box box2"></div> 
  <div class="box box3"></div>
</div>

CSS:

#boxcontainer {
  position: relative;
}
.box {
  width: 182px;
  height: 182px;
  border: 5px solid black;
  margin: 5px;
  float: left;
  position: relative;
}

JS:

$(function(){
  $(".box1").click(function(){
    $(".box").each(function(){
      var width = $(this).outerWidth(true),
          height = $(this).outerHeight(true),
          nrTop = Math.floor($(this).position().top / height),
            nrLeft = Math.floor($(this).position().left / width),
          top = (nrLeft-nrTop)*height,
          left = (nrTop-nrLeft)*width;
      console.log(width, height, nrTop, nrLeft, top, left);
      $(this).animate({
        "top": "+="+top+"px",
        "left": "+="+left+"px"
      }, "slow");
    });
  });
});

The concept behind it is to calculate the position of a box from the left and place it that distance from the top (and vice versa).

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

Using pre-existing CSS state background colors in GTK3

I am currently in the process of adapting a PyGTK2 application. This particular application performs tasks such as: self.normal_color = editor.style.base [Gtk.STATE_NORMAL] self.insensitive_color = editor.style.base [Gtk.STATE_INSENSITIVE ...

Expanding a website banner slide to fill the entire width of the page

Is there a way to ensure that the banner images in the website below flow seamlessly? Currently, it seems like the current image is cut off on the left and part of the previous image shows up in its place. I'd like the current image to occupy the ent ...

Comparison Between Object and Array

When inputting the values {2,4,45,50} in the console, 50 is displayed. However, assigning these values to a variable results in an error: var a = {2,4,45,50} Uncaught SyntaxError: Unexpected number Can you explain this concept? ...

ways to retrieve information from a JavaScript array in jade

I am currently working with the twitter-js-client and vis timeline libraries to create a timeline feature. In my index.js file, I parse a JSON object containing tweets using JSON.parse() and then pass this data to a Jade template to be displayed on the tim ...

Learn how to dynamically populate a select2 dropdown with values retrieved from a database using JavaScript or jQuery

I am currently working on an edit form where I need to retrieve data from a MySql database using Ajax. The form includes two select2 dropdowns and I want the fetched data to be displayed inside those dropdowns. For example: Let's say I have created a ...

Preventing window resize from affecting print screen content in Angular 8

I'm struggling with a print button feature in my Angular 8 project. When the window is resized, the content within the print window also resizes, which is not the behavior I want. I've tried a couple of solutions, but nothing has worked so far: 1 ...

Enhancing unique designs

After searching for a background pattern, I stumbled upon this unique design. Currently, I have it as the backdrop for my website. The issue arises from the fact that this pattern doesn't seamlessly repeat. When I set it as the body background usin ...

Execute a function multiple times within a loop

My task involves passing objects from an array to a function one at a time with a 1-second pause in between each item: async function CallingLabels() { if(id) { if(LabelNumbers) { LabelNumbers.forEach(async (Label) => { await ...

sending an index parameter to a namespaced array in JavaScript

Within my Joomla site, I am managing multiple lengthy arrays of English sentences stored in various files. These arrays contain around 50 elements each and will be showcased through jquery click events to demonstrate proper English word usage. The function ...

The line break is being disregarded by jQuery's insertBefore function

In my simple HTML, I have a div with a limited width. Inside are several span tags styled with nowrap. Here is the CSS: .content { width: 50%; margin: 10px; padding: 10px; border: 1px solid black; } .adder { color: blue; cursor: po ...

Experiencing issues with this.$refs returning undefined within a Nuxt project when attempting to retrieve the height of a div element

I am struggling with setting the same height for a div containing Component2 as another div (modelDiv) containing Component1. <template> <div> <div v-if="showMe"> <div ref="modelDiv"> <Comp ...

Calculating the position of an element in an array passed from a separate file with ReactJS and Ant Design

I'm currently working with a file that contains some JavaScript code featuring an array with multiple objects containing key-value pairs. In my main file (App.jsx), I initialize a State Variable and assign it the array from another JS file. My goal no ...

Having trouble uploading images on Wordpress?

There have been persistent errors in the admin area for several days now: An unexpected SyntaxError: expected expression, but received '<'. These errors are appearing in files moxie.min.js, plupload.min.js, wp-plupload.min.js As a result, w ...

There is a misconception about how javascript .map() can alter the original array rather than simply creating a modified version of it

While working on some code, I noticed that the previous person was modifying the original array by reference. I thought that the .map() function in JavaScript works by returning a new copy with the modifications made within the map. The following code dem ...

Leveraging reframe.js to enhance the functionality of HTML5 video playback

I'm struggling with using the reframe.js plugin on a page that includes HTML5 embedded video. When I try to use my own video file from the same folder, it doesn't work as expected. While everything seems to be working in terms of the page loadin ...

What steps can be taken to customize the default keyboard shortcuts functionality in Swiper.js?

I am trying to customize the functionality for left/right keys in Swiper.js but I am unable to find a way to do this through the API () It seems that the API only allows you to disable/enable default actions: mySwiper.keyboard.enabled // Whether th ...

Automatically expand a child node in the tree menu

Hey there! I've got a tree menu that I'm working with. I'm looking to have a specific node in the tree menu automatically open by default. Essentially, I want a particular node to be open when the page is refreshed. For instance, in this e ...

Is there a way to change the fill color of an SVG circle using Thymeleaf?

I am struggling to change the background color of a circle in an SVG image on my HTML page using Thymeleaf. I attempted to use inline style tags, but it resulted in the circle's background color turning black. <svg id="svg" height="50" width=" ...

Discovering the location within a polygon utilizing a geojson file through the use of jQuery

How can I determine which of my polygons in the GeoJSON file contains the geolocation (latlng) using jQuery? My geolocation is [35.7040022,51.403697] and my GeoJSON file is structured as follows: { "type":"FeatureCollection", "crs":{ "typ ...

The challenge of div placement was causing distress

FireFox 3.5 seems to have an issue where the following div does not push the page content down, instead it overlaps. However, in IE 8 this is not a problem. Can anyone suggest a quick solution? <div style="position:fixed;top:0;width:100%;">blah bl ...