Design a dynamic div element that gracefully transitions in and out from the edge of the screen

Currently, I am in the process of developing a simple clickdummy for an application layout. The layout includes a details view on the right side that should only be visible after clicking a button. I am struggling to figure out how to make this div move using CSS or jQuery.

I attempted to use the CSS attribute visibility to show and hide the div, but it just appears instead of smoothly moving in from the side.
I also experimented with setting margin-left on the details view, but this ended up disrupting my entire layout.

What is the best way or best practice to achieve an animated moving div?

You can find my code on jsfiddle. The red details div is the one intended to move.

function toggleButton() {


}
/*STYLE*/
.window {
  height: 100vh;
  width: 100%;
  overflow-x: hidden;
overflow-y: hidden;
}
.header, .footer {
  height: 50px;
  color: white;
  background-color: black;
  width: 100%;
}
.stage {  
  background-color: white;
  height: calc(100% - 100px);
  width: 100%;
}

.navigation {
  background-color: lightgrey;
  width: 250px;
  height: 100%;
  float: left;
}

.content{
  background-color: dimgrey; 
  height: 100%;
  width: calc(100% - 400px);
  float: left;
}

.details {
  background-color: red;
  height: 100%;
  width: 150px;
  float: right;
}

#test-table {
  width: 100%;
}

#test-table td {
  text-align:center; 
  vertical-align:middle;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
    <div class="window">  
        <div class="header">
          header
        </div>    
        <div class="stage">       
          <div class="navigation">
            navigation
          </div>      
          <div class="content">
            <table id="test-table" border=1>
            <tr>
              <th>Name</th>
              <th>Age</th>
              <th>Place</th>  
              <th>Details</th> 
            </tr>
            <tr>
              <td>John</td>
              <td>20</td>
              <td>USA</td>
              <td><input type="button" onclick="toggleButton()" value="i" /> </td>
            </tr>
            <tr>
              <td>Mary</td>
              <td>30</td>
              <td>Canada</td>
              <td><input type="button" onclick="toggleButton()" value="i" /> </td>
            </tr>
            <tr>
              <td>Tim</td>
              <td>40</td>
              <td>Germany</td>
              <td><input type="button" onclick="toggleButton()" value="i" /> </td>
            </tr>              
            </table>
          </div>      
          <div class="details" id="details">
            details
          </div>      
        </div>
        <div class="footer">
          footer
        </div>
      </div>
</body>

UPDATE

I came across an interesting solution in this discussion. I tried implementing this method (jsfiddle), but now my red div is too tall. How can I resolve this issue?

Answer №1

My preference is to use jQuery UI for achieving what you desire.

$( "#button1" ).on('click', function() {
   $( "#details" ).toggle( "slow" , function() {
     // Animation complete.
   });
});

I made adjustments to your CSS in the fiddle, specifying that the details div should have position:absolute;

In addition, I switched your input fields to

<input type="button" id="button1" value="i" />
(instead of using onClick="toggleButton()", I assigned an id to the input).

If you wish to learn more about the jQuery.toggle() effect, feel free to visit this link.

Here's the updated fiddle with the changes.

The movement might seem a bit unusual at first, but I will address that (unless you prefer it as is).

Best of luck :)

Answer №2

To achieve this effect, you can utilize the CSS animation property. Check out this updated code example

Below is the animation code snippet I implemented:

@keyframes move {
  0% { transform: translateX(150px); }
  50% { transform: translateX(0); }
  100% { transform: translateX(150px); }
}

Make sure to apply it within the .details class like so:

animation: move 1s ease;
animation-iteration-count: infinite;

Answer №3

To approach this issue, elementzero23 suggests utilizing keyframes in CSS, which offers a variety of methods.

Another option is to use JQUERY to modify the CSS or manipulate classes during scenarios where multiple changes are necessary.

Personally, I find JQUERY to be more straightforward, unless specific requirements dictate otherwise.

For reference, check out this sample: https://jsfiddle.net/ksf9jsbg/

HTML

<div class="box1"></div>
<div class="box2"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

CSS

.box1{
  position:absolute;
  top:0;
  left:0;
  width:20%;
  height:20%;
  background: #000;
}

.box2{
  position:absolute;
  top:50%;
  left:0;
  width:20%;
  height:20%;
  background: #090;
  transition: all 1.5s ease;  /*ADD THIS TO ENSURE SMOOTH MOVEMENT*/
}

JS

$('.box1').mouseup(function() {
$('.box2').css("transform", "translateX(300px)"); //THE MOST IMPORTANT PART
});

In this example, only the transform property of an element with the class "box2" was modified.

UPDATE

Check out the revised version of your fiddle, give it a go.

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

Javascript and the output of Ajax request

I'm facing an issue with my JavaScript files interacting with the response from an ajax request. It seems that the JavaScript is unable to read the response from the ajax call. My question is, how can I get my jQuery plugin to access the classes in t ...

retrieveStyle() rooted framework Category

I have implemented a customized Native Base style theme following the instructions in this link. Imports: import material from './native-base-theme/variables/material'; import getTheme from './native-base-theme/components'; return ( ...

trouble executing a straightforward xpath search

I'm having trouble with this piece of code: Notice: Trying to get property of non-object in test.php on line 13 Despite what seems to be a correct xpath query and the presence of tags in the provided URL, the code is not returning the expected resul ...

Using PHP and JQuery to Implement Cloudinary Callbacks

After uploading images to my cloudinary account, I am unsure where cloudinary is directing the image_id. The documentation mentions that cloudinary_cors.html is supposed to be the destination for Cloudinary's callbacks, but this seems odd as it doesn& ...

Error: Trying to break down a non-iterable object is not valid

Currently working on an Ionic React app and encountering the following error: Error: TypeError - Invalid attempt to destructure non-iterable instance Once I run the program, the error occurs at this point: . Shown below is a snippet of my code: import ...

Tips for assigning a cookie as the id of a div?

I've been tasked with making the selected menu stand out when the page is refreshed. I'm thinking of using a cookie to achieve this. Here's the HTML code: <div class="menuBar"> <div class="menuHeader ui-corner-top"> ...

Javascript - issue with accurately retrieving element offset value while scrolling

My goal is to dynamically change classes for elements based on their position during scrolling. I am trying to calculate the top offset element value and adjust the classes accordingly. Here is the function I am using: handleScroll () { const header = ...

What causes the scrollTop to appear erratic?

There is a simple issue that I find difficult to explain in text, so I have created a video demonstration instead. Please watch the video at this link: The functionality on my page works perfectly when scrolling down, as it replaces images with the next i ...

Exploring tools to grasp the intricacies of HTML page structure

Can you recommend any alternatives to Firebug for analyzing the structure of my HTML code? If so, please provide details on what sets these tools apart from Firebug in terms of functionality. ...

Uncover and control nested objects in JSON data

Having a dynamic foo object with the possibility of nested parent objects raises the question of how to effectively: 1) Determine the last object that has a parent? 2) Create an array containing all nested parent objects along with the initial obj (foo.o ...

Make the Height of the Parent Div Based on the Position of its Children

If you're looking to design a card with three child divs, where only one is visible at a time and transitions smoothly as the user progresses through steps 1, 2, and 3, then you've come to the right place. In the code snippet provided below, the ...

Fancybox causing issue with submit button functionality

When a link is clicked, I am triggering the opening of a submit button styled fancybox. The fancy box contains fields for username and password that need to be authenticated upon clicking the submit button (for now, the fancybox is just a submit button). ...

How come my show and edit routes are not recognizing req.body as defined, unlike my create route?

I recently refactored my code to make it more organized by moving some parts into separate models in my app.js file. However, after doing so, I started encountering errors stating that the items within the req.body object are undefined. Unfortunately, I&ap ...

Having trouble establishing an HTTPS connection with a Node.js server

After connecting to my server through the console without any errors, I encountered an issue in Chrome where it displayed a message stating This site can’t provide a secure connection local host uses an unsupported protocol. Here is the code snippet: im ...

Exploring the World of jQuery Caching and Navigating Through Objects

I'm interested in learning more about jQuery caching and how it can enhance performance. Can you explain how to properly utilize this technique? From what I understand, when using a jQuery selector, you're essentially searching the DOM to create ...

When utilizing Angular, the mat-datepicker is displayed underneath the modal, even after attempting to modify the z-index

I am encountering a problem with a mat-datepicker displaying below a modal in my Angular application. Here are the key details: Html: <div class="col-12"> <mat-form-field appearance="fill"> <mat-label>Start Date ...

Manipulating DOM elements using JavaScript Vanilla with the Jquery <<S.fn.init [selector]>> framework

I have a project where I need to switch the logic written in jQuery to vanilla JavaScript. Here is an overview of my code: I am using the keydown event on an input element to trigger a function that searches an API based on the input value. var selectedU ...

Struggling to Locate the Chat Element on Google Hangouts Using Selenium Python

Lately, I have been struggling with the selenium module in Python. I am attempting to create a script that can automatically send messages to my friends as requested by one of them. Although I can successfully log into Google Hangouts using Selenium, I am ...

Check if the DIV element does not exist in the DOM before using the

I have been working on a large project and what I need is if div 1 does not contain div 2 as a child{ div1.appendChild(div2) } However, I am facing difficulties in solving this issue Here is my code snippet <script> dc = document.createElement( ...

"Headers cannot be set once they have been sent to the client... Error handling for unhandled promise rejection

Having trouble with cookies in the header - specifically, encountering an error at line number 30. The error message reads: "Cannot set headers after they are sent to the client." Additionally, there is an UnhandledPromiseRejectionWarning related to a prom ...