"Enable a smooth transition and switch the visibility of a div element when clicked

How to create a dropdown form with a transition effect that triggers on click of an element? Once triggered, the transition works smoothly but clicking again only hides the div element without triggering the transition. See the demo here:

Check out the function below:

JavaScript snippet:

<script>

$(function() {

    $("#login").click(function() {
        var visibility = $(".User-Login").css('visibility');

        if(visibility == "visible") {
            $(".User-Login").toggleClass("box-colap");
            $(".User-Login").css('visibility', 'hidden');    
        }
        else {
            $(".User-Login").css('visibility', 'visible');
            $(".User-Login").toggleClass("box-change"); 
        }       
    });
});

HTML structure:

<table class="MainMenu" style="width:100%;background-color:white" align="center">
<a href="#">Home</a>
        &nbsp;
        &nbsp;
        </td>
        <td>
        <a href="#">How It Works </a>
        </td>
        <td>
        <a href="#"> Features</a>
        </td>
        <td>
        <a href="#"> Why Us </a>
        </td>
        <td>
        <a href="#"> Sign Up </a>
        </td>
        <td>
        <a id="login" href="javascript:void(0);"> Login </a>
        </td>
</table>

CSS styles:

.User-Login {  
    visibility: hidden;
    position: absolute;
    left: 1150px;    
    background: black;
    -webkit-transition: height 2s ease;  
    -moz-transition: height 2s ease;  
    -o-transition: height 2s ease;  
    -ms-transition: height 2s ease;  
    transition: height 2s ease;  
    overflow: hidden;
    height: 10px;
    top: 90px;
}  

.box-change {   
    height: 250px;  
}

.box-colap {
    height: 10px;
}

Answer №1

Discover two easy CSS3 solutions that don't require jQuery

Check out fiddle 1 here

Or view fiddle 2 for more examples

Both examples utilize the power of CSS3 for animation effects

Here is the necessary CSS code to create sliding animations:

div{
display:block;
height:120px;
width:120px;
background:red;
}

.hideUp{
    position:relative;
    height:200px;
    width:200px;
    background:blue;
    display:block;
    -webkit-transition:all 1s ease;
    -moz-transition:all 1s ease;
}

.hideUp:focus{
    top:-200px;
}

Below is the corresponding HTML code:

<a href="#" tabindex="-1" class="hideUp"><div><p>CLICK TO MOVE</p></div></a>

:)

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

Traversing through JSON main sections, checking for operation and subsequently retrieving data

I have a json structure below: { users: [ { action: 'add', payload: [Array] } ], categories: [ { action: 'add', payload: [Array] } ], products: [ { action: 'add', payload: [Array] } ] } Can you suggest a method using .m ...

Choosing a single radio button value within a nested ng-repeat loop in AngularJS

Help needed with selecting only one radio button value in nested ng-repeat. Please review the source code and suggest any potential mistakes. <form ng-submit="save()"> <div ng-repeat="x in surveyLst"> <div class="row"> < ...

Incorporating a dynamic HTML editing button to every new row in a table using JavaScript

My application features a form that allows users to dynamically add new rows to a table using JavaScript: // Function to add a new account row if (tit.innerHTML === "Add Account"){ var table = document.getElementById(tableId); var row = table ...

The issue arises when trying to use jQuery on multiple elements with the same class

Recently, I came across a jQuery script for my mobile menu that changes the class on click event. jQuery("#slide-out-open").click(function() { if( jQuery( this ).hasClass( "slide-out-open" ) ) { jQuery('#wrapper').css({overflow:"hidd ...

What could be the reason for the absence of the loading sign in Chrome, even though it appears when the code is run on Firefox?

I implemented a function to display a loading screen on my HTML page with Google Maps integration. However, when I called the function popUpLoadingScreen() and dismissLoadingScreen() to show and hide the loading message while rendering map markers, the loa ...

Troubleshooting Problem with JQuery in the YouTube Player API

Having some difficulty parsing YouTube video ids to a function that plays them in an embedded player using a combination of JavaScript and jQuery with the YouTube Data and Player APIs. Below is my code: <html> <head> <script src="//aj ...

Issue: React-Firebase-Hooks failing to retrieve dataHaving trouble with React-F

I've been utilizing the React-Firebase-Hooks package in my project, but I'm encountering some difficulties with its usage. In the code snippet below, the user.data().username is displayed correctly. However, when I try to use it within useState, ...

What are some unique ways to customize my h1 headers using jQuery UI?

After implementing jQuery UI widgets successfully, I noticed that many pages on my website follow a similar structure: <div id="bdy" class="ui-widget ui-widget-content"> <div class="bdy_box"> <h1>Home</h1 While the first two lines ar ...

jQuery AJAX delivering HTML output upon completion

Currently, I am implementing a jQuery Ajax function to submit the username and password and receive a response. The functionality works seamlessly with the GET method. However, when using the POST method, it successfully sends the data but fails to retur ...

Guide to importing multiple controllers using express

For my upcoming full stack project, I am working on various controllers like signup, login, and profile. Instead of manually requiring each controller and adding them to the app using individual lines of code, I am seeking a more efficient solution. I env ...

Is it possible to continuously loop and gradually fade the same image URL using JavaScript?

I have a dynamic image stored on my web server at example.com/webcam.jpg that is continuously updated by the server. My goal is to display this image on a static HTML page with a looping effect that smoothly transitions from the previous image to the upda ...

Obtain a null value for the hidden field in ASP from a JavaScript parameter

Trying to transfer the selected parameter value from a dropdown menu to a hidden field but the hidden field always ends up empty. No errors in the JavaScript code when traced using browser debugger. What could be the issue? JavaScript $(document).ready( ...

Issues arise when attempting to retrieve information in NextJS from a local API

One of my coworkers has created a backend application using Spring Boot. Strangely, I can only access the API when both our computers are connected to the same hotspot. If I try to access the other computer's API and port through a browser or Postman, ...

Deploying a single node.js app on two separate servers and running them simultaneously

Is it possible to set up my game to run on both the west coast and east coast servers, while still using the same domain? In my code structure, app.js runs on the server with the home route serving as the entry point for the game. This means that users si ...

Creating a load more feature with Vue

Having some trouble implementing a load more button that adds 10 more items to the page when clicked. The button code seems to be causing issues as all items still appear on the page and there are no errors in the console either. As a result, the button is ...

How to organize initial, exit, and layout animations in Framer Motion (React) tutorial?

Currently, I am utilizing framer-motion library for animating a change in grid columns. This is the objective: Within the grid container (#db-wrapper), there are nine buttons arranged. https://i.stack.imgur.com/61pQqm.png When the user switches to the ...

Assistance needed in handling a form with the use of $_POST and $PHP_SELF

Let me explain my goal in creating a webpage using HTML and PHP. The webpage will feature multiple forms to gather user inputs for future use. My plan is to reveal each form only after the previous one has been submitted. Here are the 3 forms: First name ...

Using AngularJS, learn how to populate array objects within a JSON object

I'm trying to load array objects from a multi-select control, then populate a model object called "name" with its name and age values. After that, I want to load an array from a select element into the model object. However, the ng-model directive on ...

Unable to successfully import an external HTML file that contains a script tag

I am currently experiencing an issue with my index.html <!doctype html> <html lang="en"> <head> <meta charset="utf-8> <title>MyApp</title> <link rel="import" href="./common.html"> </head> <body> ...

Maintaining Existing Filters and Incorporating Additional Filters in React/Next.js Data Filtering

I'm currently facing a challenge with filtering data in React/Next.js. The issue I'm encountering is that whenever I set a new filter, the previous filters get removed. For instance, if a user performs a search, it works fine but the tag filters ...