Is it possible to have content boxes that fly in when a button is clicked

I am exploring the idea of creating two dynamic boxes that appear when a button is clicked, much like the effect seen when "About Me" is selected on . After struggling to decipher how it was achieved, I opted for utilizing modals. However, I encountered difficulties in synchronizing the animation of both boxes when using separate modal elements. I managed to make it work by incorporating two different divs within a single modal:

<div id="myModal" class="modal col-xs-12 col-lg-6">
  <!-- Modal content -->
  <div class="modal-content1">
    <span class="close">&times;</span>
    <h2>About Me</h2>
    <p>blah blah>
  </div>
  <div class="modal-content2 col-xs-12 col-lg-6">
    <span class="close">&times;</span>
    <h2>Super powers</h2>
    <p>blah blah>
  </div>
</div>

However, this setup results in individual animations and lacks the seamless integration seen in the desired example (where they enter from different directions). Should I stick with modals for this task, or is there a more effective approach? Despite my search for relevant examples, I have yet to come across any demonstrations featuring dual boxes. Any guidance you can provide would be greatly appreciated. Thank you in advance!

Answer №1

Feel free to experiment with this code and try to make it align more closely with your desired outcome. I've made some modifications to provide you with an example and a space for refining your solution. Best of luck on your coding journey!

Check out the updated code snippet below: Javascript Section:

var test = document.getElementById('test');
var test1 = document.getElementById('test1');

function move(elem, x, y) {
  var left = parseInt(getStyle(elem, 'left'), 10),
    top = parseInt(getStyle(elem, 'top'), 10),
    dx = left - x,
    dy = top - y,
    i = 1,
    count = 20,
    delay = 20;

  function animate() {
    if (i >= count) {
      return;
    }
    i += 1;
    elem.style.left = (left - (dx * i / count)).toFixed(0) + 'px';
    elem.style.top = (top - (dy * i / count)).toFixed(0) + 'px';
    setTimeout(animate, delay);
  }

  animate();
}

function getStyle(element, property) {
  return window.getComputedStyle(element, null).getPropertyValue(property);
}

window.onclick = function(e) {
  if (e.target.nodeName === 'BUTTON') {
    test.style.cssText = 'visibility:visible;';
    move(test, +50, +50);
    test1.style.cssText = 'visibility:visible;';
    move(test1, +200, +50);
  }
};

HTML Markup:

<button title="100 100">Translate to (100, 100)</button>

  <!-- Modal content -->
  <div id="test">
    <span class="close">&times;</span>
    <h2>About Me</h2>
    <p>blah blah>
  </div>

  <div id="test1">
    <span class="close">&times;</span>
    <h2>Super powers</h2>
    <p>blah blah>
  </div>

CSS Styling:

#test {
  position: absolute;
  left: 50px;
  top: 500px;
  visibility: hidden;
}
#test1 {
  position: absolute;
  left: 100px;
  top: -500px;
  visibility: hidden;
}

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

The closure in question received an undefined Angular parameter

Here is something similar to what I have: $scope.fetchData = function(path, save_to) { $http({method: 'GET', url: 'http://localhost:8001/www-root/' + path}). success(function(data, status, headers, config) { ...

How do I retrieve the NodesValue from the childNodes in an HTML tag?

<p title="The test paragraph">This example demonstrates some <b>HTML content that may be<br>present</b> in your file</p> txt=document.getElementsByTagName("p")[0].childNodes[0].nodeValue; alert("<p>The text from the in ...

`Moving smoothly with a slider and then reversing direction`

I have implemented a range slider to control the value in an input field. The values can vary greatly, so I needed finer control for lower numbers that gradually expands for larger numbers. To address this issue, I utilized an easing equation based on the ...

Steps to perform a double click on a word within a webpage using JavaScript in the browser's console

HTML: <iframe ..... <p class="textClass"> Some Text............. <span id="unique-id"> Double-Click Me</span> </p> </iframe> Requirement: I am seeking a solution to trigger a double-click programmatical ...

Safari iOS does not properly support responsive images when using srcset and sizes

I am facing an issue with the following code snippet: <img src="/img/footer/logo_white.png?v=2.0" srcset="/img/footer/logo_white.png?v=2.0 1x, /img/footer/logo_white2x.png?v=2.0 2x" > This ...

Create a single CSS style rather than using multiple CSS styles

I currently have multiple CSS styles applied: form#reply_form-c1r1.odgovor div#cmtforms-c1r1 input{color:red} form#reply_form-c2r1.odgovor div#cmtforms-c2r1 input{color:red} form#reply_form-c3r1.odgovor div#cmtforms-c3r1 input{color:red} form#reply_form-c4 ...

Using Javascript to extract the initials of names

I am looking for a way to shorten Person and group names Person : Robert Smith David Smith James Johnson William If I use this code snippet: if(Name.split(",").length === 1) return Name.trim().split(" ").map(x => x[0]).reduce(( ...

Newbie inquiry: How to utilize classes in HTML for CSS implementation?

I'm a beginner in the world of HTML and I have what might be considered as a basic question... Here is the style definition for my header in the CSS file: h1.heading { color: indianred; } I attempted to link it to my HTML file like this, but unfo ...

Verify whether a group of div elements overlaps a fixed div while scrolling

I have a layout with a list of posts and a logo positioned at the bottom of the page. The issue I am facing is that sometimes the title of the posts and the logo overlap, and I want to set the logo's opacity to 0.25 only when the text from .cat-date i ...

How can you sift through an array of objects by looking at the elements within another nested array? Are you utilizing Vanilla JS, lodash, or another tool from the

Hey, I'm currently working on a React app where I have a list of projects that are associated with specific technologies. What I want to achieve is allowing the user to select certain technologies using checkboxes and have the project list automatical ...

Is it possible to add values to a select dropdown using the developer console in a web browser?

Currently, my web application is developed using PHP within the Laravel framework. The main question I have relates to a scenario where I have a web form with multiple SELECT elements. Is it possible for someone to utilize JavaScript to add new options to ...

Working with session ID and Ajax in JavaScript

Recently, I've discovered that my website is experiencing issues on various versions of Internet Explorer. Despite conducting thorough research, it appears that cookies are not functioning properly when accessed through IE, although they work without ...

Tips for clearing object values without deleting the keys: Resetting the values of an object and its

Upon creating a service to share data among multiple components, it became necessary to reset the object values once the process was complete. To achieve this, I attempted the following: this.UserDetails = {}; This successfully cleared the values and remov ...

Moving elements upwards and downwards using CSS transitions

I am currently designing a metro tiles menu for my website, without using JavaScript and only relying on HTML and CSS. The issue I am facing involves the sliding tiles and the direction in which they slide. Additionally, there seems to be a problem where ...

Having trouble getting the toggle button JavaScript code to function properly in React JS?

I'm in need of some assistance with the comment section located below the JavaScript Code. I am trying to implement a toggle button / hamburger button, but it seems that addEventListener is not necessary for ReactJS. Can someone provide guidance on wh ...

Collecting the timestamp when the page is loaded and then comparing it with the timestamp when the form is

I have implemented an anti-spam time trap in my application (although I realize its effectiveness may not be 100%, it serves as a temporary solution) that functions as follows: 1) Upon loading a page, a hidden input is populated with a timestamp using Jav ...

Having trouble with the installation of js-bson

While attempting to install bson as a dependency of mongodb on my Windows 7 64bit system, I encountered the following error: npm WARN package.json <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97c0f8f4fff2f9e7fbf6f9f2e5d7a7b9 ...

Transforming an object containing methods into a string using Javascript specifically for Photoshop

I have a unique universal object that is essential for my Photoshop scripts. It contains various properties and methods like this: var Object = {}; Object.text = "this is some text"; Object.toolbox = new ToolBox(); // specialized object with its own funct ...

What steps can I take to stop my Details element from moving a nearby Div?

Within this setup, there is a div containing Details and Summaries to the left. Upon expanding the details, the text enlarges and the container moves, causing the image in the right div to shift downward. Is there a way to prevent this from happening? I ...

Internet Explorer 8 comes with excess padding at the top and bottom, causing un

Why is IE 8 adding unnecessary padding to a div? I have checked the styling and nothing seems out of place. Can someone assist with this issue? Html The highlighted div in blue is the main concern. .block { clear: both; } .inline { float: lef ...