Tips for creating a more seamless box transition within HTML

I've been searching for information on this topic, but I haven't been able to find a satisfactory answer.
I want to create a div that displays "Contact Us" and when clicked, smoothly reveals a layer with input fields.

I know I can use JavaScript to toggle between display:none and display:block, but how can I achieve a smooth transition?

Here's a basic example (the actual implementation will be more polished):

<div id="contact-us" onClick="showContactUs()">
<div id="contact-us-content" style="display: block;">
  Name - <input type="text" name="name">
  Email- <input type="text" name="email">
</div>
</div>

The accompanying JavaScript code is:

function showContactUs(){
   var contact = document.getElementById("contact-us-content");
   contact.style.display = "block";
}

If you have any tips or resources that could help me accomplish this task, please share them. I'm not very experienced with jQuery, but I am open to learning if it's a better solution.

Answer №1

If you're looking to achieve a smoother appearance without using display: block, one approach is to apply a transition effect on the opacity property. One way to implement this is by dynamically adding a class using JavaScript and handling the styling with CSS.

You can view an example of this in action here: https://jsfiddle.net/3wLrfk3d/

$(document).on('click', '#contact-us', show_contact_form)

function show_contact_form () {
    $('#contact-us-content').addClass('shown')
}

CSS:

#contact-us-content {
    opacity: 0;
    transition: opacity .5s ease;

    &.shown {
        opacity: 1;
    }
}

I utilized jQuery and Sass in my demonstration, but feel free to adapt it to vanilla JavaScript and CSS.

Answer №2

My usual approach is to transition the opacity, however, this results in the element still being present even when invisible, occupying space and blocking mouse events. To address this, I implement two classes: one for fading the element and another for completely hiding it once the fading process is complete:

$(function() {
var $box = $('.box');
$('.toggle').on('click', function() {
if (!($box).hasClass('visible')) {
    $box.addClass('transitioning');
setTimeout(function() {
      $box.addClass('visible');
      }, 1)
    } else {
    $box.removeClass('visible');
      setTimeout(function() {
    $box.removeClass('transitioning');
      }, 400)
    }
  })
})
body {
  font-family: Helvetica, Arial, sans-serif;
}

.box {
  background-color: #333;
  color: white;
  border: 5px solid white;
  padding: 50px 10px;
  box-shadow: 0 0 10px 0 rgba(0, 0, 0, .4);
  text-align: center;
  transition: opacity .4s ease-in-out;
  display: none;
  opacity: 0;
}

.transitioning {
  display: block;
}

.visible {
  opacity: 1;
}

.toggle {
  margin-bottom: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="toggle">Toggle Box</button>
<div class="box">
  <h1>Hi there</h1>
</div>

Please note: This example is a quick demonstration and may behave strangely if the toggle button is clicked repeatedly. Handle at your discretion.

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

Harness the power of Vue.js by implementing plugin methods in your code

For my first attempt at building a SPA with Vue, I decided to re-use a few functions but encountered some issues. The error message "this.ExperienceToLevel is not a function" kept popping up and it left me puzzled. Furthermore, I'm contemplating if c ...

Using the Grails asset-pipeline with an external JavaScript library

In transitioning from Grails 2 to Grails 3, I am facing the challenge of managing my JavaScript files with the asset-pipeline plugin. The issue lies in using external libraries such as globalize and ajax-solr, which are large and consist of multiple interd ...

Utilizing the jQuery Selector for Headers

I am currently working on grasping the concept of using the Jquery selector. https://learn.jquery.com/using-jquery-core/selecting-elements/ In my project, I have a table and I am trying to create an input field within a modal. My main challenge lies in c ...

Learn the steps to showcase the output in a paragraph using ReactJS

Is there a way to display the result in the browser instead of just exporting it to the console when using the code below? I am new to React and would like the result to appear in a paragraph or another tag on the webpage. import React, { Component, use ...

Utilize Vue.js to sort by price bracket and category

Hello, I am currently new to working with Vue.js and I am attempting to create two filters - one for price range and the other for genre. Any guidance or assistance would be greatly appreciated. Below is a snippet of my code: ul class="filter-wrapper"&g ...

Ways to choose an identifier larger than

Currently, I am in the process of creating a news system for a website. For this project, I referred to a tutorial on building a news system which can be found here: To showcase the news articles, I have implemented the following code snippet: if(!$_GET[ ...

Develop ContentService API in Google Apps Script to retrieve and deliver JSON data stored in Google Drive

Exploring the latest features unveiled at Google IO 2012 is still a work in progress. When it comes to GAS and ContentService, the ability to create an API to serve content (from Google Drive) is a key highlight. An illustrative example would involve: var ...

Set up global variables for components to access

Currently, I am working on a Laravel 8 project with vue-sweetalert2 integration. My goal is to set up the Toast behavior once and then be able to call it within various components. At the moment, my code looks like this: /js/components/Mypage.vue <scr ...

List items out of place because of CSS incompatibility in Firefox and Chrome

After creating a list of names displayed in an <ul>, I implemented some CSS styling only to encounter browser-specific issues. In Chrome: The list element is shifted by one row. In Firefox: All list items are collapsing into one item. Here&apo ...

"Implement highcharts redraw() method to update the chart, along with a callback function that interacts

I am working with a chart that utilizes the events.load function to draw lines based on the properties of the chart. The load function is functioning as expected, but I want to erase and redraw the lines each time the chart is redrawn, such as when hiding ...

Why isn't Google Map showing up in my HTML <div> section?

I am currently working on a web page that consists of two containers. The mainmapdiv container covers the entire page, while the mainhomediv container is positioned on top of the mainmapdiv. My goal is to hide the mainhomediv container and show a Google Ma ...

Can a Vue component in SFC form be utilized as a mixin?

Consider this scenario, where I have a component created in SFC format: // Parent.vue <template> <div> {{ txt }} </div> </template> <script> export default { name: 'ParentComponent', data() { return ...

Difficulty dealing with Firestore using get() followed by add()

Recently started learning Vue.js and Firestore, facing a challenge with what should be a simple task. 1) I am trying to fetch default values from an existing template document in my Firestore database. 2) The goal is to use these default values to create ...

The Image Slider functions smoothly in Chrome, but encounters issues in IE11

Here is a link to the code I've been working on: http://jsfiddle.net/wf32jbhx/ I attempted to host images on my SharePoint site, but they ended up stacking on top of each other instead of formatting properly. Oddly enough, everything appears fine in ...

Uncertain about the process of upgrading this chart with the latest array modifications

My code includes a function that updates the data array with user input, which is used to create a bar graph. The issue I am facing is that while the data array in the chart gets updated upon user input, the chart itself does not. How can I ensure that the ...

Error message: Unable to access the state property of an undefined object

I've been attempting to integrate a react sticky header into my stepper component. However, I've encountered an issue where it doesn't render when added inside my App.js file. As a result, I've started debugging the code within App.js ...

Steps for extracting information from an html webpage that runs javascript

I'm currently working on a program to extract the IUPACcondensed from this specific website. On this page, G03307GF serves as the ID. The desired information is as follows: HexNAc(b1-?)[Fuc(a1-?)]GlcNAc(b1-2)Man(a1-3)[HexNAc(b1-?)[Fuc(a1-?)]GlcNAc(b ...

Facing problem with Angular 7 when making a GET request for non-JSON data

Currently, I am retrieving JSON data from a URL using the following method: this.http.get('http://localhost:3200/mydata').subscribe(data => { console.log(data); }); The response is in JSON format, and everything seems to be working fine. ...

Removing HTML elements using JQuery

Example: http://jsfiddle.net/axfsD/1/ I'm facing an issue with removing HTML tags from my webpage. Below are two examples where I have used the replace command to strip tags. However, it successfully strips the tags of the second button but not the f ...

UI-router issue: UI view and links not functioning properly

Recently, I decided to implement ui-router for Angular in my project. After adding the following code snippet to my app module within the app.js file: angular .module("ngClassifieds", ['ngMaterial', 'ui.router']) .config(function($md ...