How to utilize the translateY() method to seamlessly shift concealed elements with no empty space

My goal is to create an interactive animation that reveals a specific section when a user clicks on an element. Initially, this section is hidden from view using the translateY(-700px) property. However, I have noticed that applying a transition effect to the margin-bottom property has resulted in a decrease in frames per second (FPS) during the animation.

Now I'm left wondering: Is there a way to animate this section without causing any visible blank space when it's hidden?

Below is the SASS code for the section:

.section {
  padding: 50px 0 20px 0;
  background-color: black;
  display: block;
  box-shadow: 0 4px 15px 0 rgba(0, 0, 0, 0.05);
  transform: translateY(-700px);
  transition: transform 1s;

  &._active {
    transform: translate(0, 0);
    margin-bottom: 0;
  }
}

Answer №1

When utilizing the translate function, the browser retains the original size of the styled element. If you eliminate the padding, one approach is to transition the height together with translateY:

var toggler = document.getElementById("toggler");
var toggled = document.getElementById("toggled");

toggler.addEventListener("click", e => {
  toggled.classList.toggle('_active');
})
.section {
  /* padding: 50px 0 20px 0; */
  background-color: black;
  color: white;
  display: block;
  height: 0;
  box-shadow: 0 4px 15px 0 rgba(0, 0, 0, 0.05);
  transform: translateY(-700px);  
  -webkit-transform: translateY(-700px);
  transition: transform 1s, height 1s;
  -webkit-transition: transform 1s, height 1s;
}
.section._active {
    transform: translate(0, 0);
    margin-bottom: 0;
    height: 130px; /* adjust for your section content */
}
<button id="toggler">Toggle visibility</button>

<section id="toggled" class="section"></section>

<section>
    <p>
  Lorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit amet
  </p>
</section>

If you plan to include content within your section, a workaround to add some space is to utilize a border with the same color as the section background and adjust the height property in your _active class.

Answer №2

After some experimentation, I finally cracked the code.

I noticed that using both the height and margin-bottom properties resulted in a jerky animation. To achieve smooth movement, I had to resort to applying transform: translateY() to all elements that needed to move along with the main element being displayed.

My solution involved assigning an additional class to each section on the webpage and adjusting its transform property accordingly. This workaround produced the seamless animation effect I desired. If you know of a more efficient method, please share!

$('#panel').on('click', function() {
  $('.new-class').toggleClass('down');
  $('.advanced-search').toggleClass('_active');
});
.hero {
  height: 700px;
  background-color:  #FFD400;
  position: relative;
  z-index: 2;
}

#panel {
 width: 100%;
 color: white;
 background: #2E294E;
 font-weight: bold;
 font-family: Helvetica, sans-serif;
 text-align: center;
 padding: 30px 0;
 position: relative;
 z-index: 2;
}

.advanced-search {
 height: 700px;
 display: block;
 width: 100%;
 background: #D90368;
 text-align: center;
 margin-bottom: -700px;
 transform: translateY(-700px);
 transition: transform 1s;
}

.new-class {
 background-color: #F1E9DA;
 height: 300px;
 width: 100%;
 border: 1px solid white;
 text-align: center;
 font-family: Helvetica, sans-serif;
 transition: transform 1s;
}

.new-class.down {
  transform: translateY(700px);
}

.advanced-search._active {
  transform: translateY(0px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hero"></div>
<div id="panel"> Click to show </div>
<div class="advanced-search"></div>
<div class="new-class">
  Some content
</div>
<div class="new-class">
  Some content
</div>
<div class="new-class">
  Some content
</div>
<div class="new-class">
  Some content
</div>
<div class="new-class">
  Some content
</div>
<div class="new-class">
  Some content
</div>

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 Google Closure Compiler Service is providing faulty code

I've been using Closure Compiler through to minify and obfuscate my JavaScript code, but I seem to be encountering an issue. To troubleshoot, I created a test page with a script. Here's the HTML5 code snippet: <!DOCTYPE html> <html> ...

Extjs: How to Select a Node After Creating a Tree Structure

I am facing an issue with my TreePanel where I want to preselect a specific node when loading it. The nodes are fetched from a remote json file and the tree structure loads correctly. However, the selected node is not getting highlighted and Firebug is sho ...

the div's width isn't getting larger

Check out my code snippet: <script> var change = function(){ alert("sam"); for(var i; i >=200; i++){ var z = String(i); var x= document.getElementById("div1"); x.style.width = z; } }; </script> < ...

Error message "The camera provided is invalid and not an instance of THREE.Camera" appears when attempting to render a cube using Three.js

I've been working on a small "engine" project using three.js to easily create and position objects. So far, I have set up the scene, renderer, and camera successfully. However, when attempting to render and attach a cube to my scene, I encounter an is ...

Implementing external JavaScript files such as Bootstrap and jQuery into a ReactJS application

Just diving into ReactJs, I've got static files like bootstrap.min.js, jquery.min.js, and more in my assets folder. Trying to incorporate them into my ReactJs App but running into issues. Added the code below to my index.html file, however it's ...

Why does Vuetify/Javascript keep throwing a ReferenceError stating that the variable is undefined?

I'm currently developing in Vuetify and I want to incorporate a javascript client for Prometheus to fetch data for my application. You can find the page Here. Despite following their example, I keep encountering a ReferenceError: Prometheus is not def ...

Iterating through a nested array in order to dynamically generate elements using JavaScript/jQuery

Seeking assistance with a specific issue I am facing. Despite extensive research on this platform, I have not found a solution to my problem. Recently, I successfully used jQuery each to loop over objects. However, I am currently struggling to iterate thro ...

Template with a modal overlay

After clicking a button, I want a modal window to appear. However, if the button is inside a template-generated page, the modal window does not appear. <script type="x-template" id="company-template"> <button class="ui but ...

Utilizing Angular.js to Bind AJAX Content to Web Pages

I recently experimented with coding using angular.js version 1.5. <div class="container clearfix" id="mainMenu" ng-controller="MainMenuController as mainMenu"> <ul class="menu clearfix" ng-init="tab = 'ant'"> <li class ...

Looking for a different method to remove a list item within an unordered list using CSS instead of HTML

I am currently working on designing a mock-up website using CSS codes. If you want to see the mock-up website, you can visit: Here is the specific list I am referring to: https://i.sstatic.net/HA3fC.jpg This is the expected outcome: https://i.sstatic.ne ...

Is requestAnimationFrame necessary for rendering in three.js?

I am currently working on the example provided in Chapter 2 of the WebGL Up and Running book. My goal is to display a static texture-mapped cube. The initial code snippet is not functioning as expected: var camera = null, renderer = null, scene = null ...

What is the best way to update information in the `App` component using Vue?

Within Vue, I have an App component that utilizes the <router-view> to extend the functionality of a Login component. My goal is to update specific data within the App component when a button is clicked in the Login component. Is this type of inter ...

What is the best way to send data using ajax?

I'm looking to send variables from post.php to addcomment.php using an AJAX request. I've attempted the code below, but it's not functioning as expected. The page reloads, but no changes occur, and there's no data being added to the dat ...

Ways to enhance the maxDate functionality in a jQuery datepicker

I need to implement a feature where there are two input boxes: in the first box, the user selects a date and in the second box, the date available for selection should be set as 2 days after the selected day. Below is the code snippet that I have been work ...

Ways to retrieve child state in React?

After creating my own form component with a render() method that looks like this: render() { return ( <form onSubmit={this.onSubmit} ref={(c)=>this._form=c}> {this.props.children} </form> ) } I enabled ...

Leveraging information from a single controller for another

I am currently developing a website using MEAN stack. I am facing an issue with passing data from one controller to the next. My goal is to transfer a selected option value to the next page. Here is the section with the select box: <div class="plumber ...

Troubleshooting issues with jQuery plugin functionality post-upgrade

My previous jQuery version was 1.4 or 1.8, but I upgraded to version 1.10.2 to access newer plugins. However, I encountered a problem - my jQuery code for editing attributes stopped working after the upgrade. Below is the code snippet: <script type="te ...

Detecting browser reload in React/Typescript: A guide

Is there a way to determine if the browser has been reloaded? I've explored various solutions, but most suggest using code like this: const typeOfNavigation = (performance.getEntriesByType("navigation")[0] as PerformanceNavigationTiming).type ...

Unable to remove jQuery variable using jQuery .remove() method

My goal is to remove the $movieDiv that appears when I click "#buttonLicensedMovie". It successfully appends to the html and the button hides as expected. However, I am encountering an issue when clicking the anchor tag with id "licensedMovie1", the $movie ...

Reconfigure a portion of a string using Vue's dynamic replacement feature

Currently tackling a problem. In my possession is a string that essentially consists of HTML code: let htmlTitle = "<a href="/news/sky-sport-hd-in-italia-dal-18-novembr">Sky Sport HD in italia dal 18 novembre</a> | <a href="/news/ecco-il-g ...