How can you hide an element using ToggleFade and display:none?

My aim is to create a toggle effect that fades in a login box when the 'login' button is clicked. Initially, I want the box to be hidden using display: none so it's not visible or interactive (I wasn't sure if changing visibility would affect button functionality - maybe altering pointer events could help).

The toggle operation works smoothly, except for the first click on the button, where the hide class is removed (revealing the box), but then the toggle fades OUT the box.

Here's the HTML:

<a id="loginBtn" class="loginBtn" href="/">Login</a>
<div id="loginContainer" class="loginContainer hide">
    <a class="registerBtn" href="/">Register</a>
</div>

CSS styles applied:

.loginBtn {
    color: #1493d1;
    background: #fff;
    border-radius: 98px;
    cursor: pointer;
    margin: 15px 0 0 0;
    padding: 8px 15px;
    position: absolute;
    right: 0px;
    /*float: right;*/
    text-decoration: none;
}

.loginContainer {
    width: 175px;
    height: 250px;
    background: #fff;
    border-radius: 10px;
    position: absolute;
    top: 80px;
    right: 0px;
    /*float: right;*/
}

.hide {
    display: none !important;
}

And here's the JavaScript function:

$(document).ready(function () {

    var loginBtn = document.getElementById("loginBtn");

    // Function to display Login box
    function displayLogin() {
        $('#loginContainer').removeClass("hide");
        $('#loginContainer').fadeToggle("fast");
    }

    // Event listener setup
    loginBtn.addEventListener("click", displayLogin);

}); // End of Document Ready

Answer №1

Below is the solution provided for your reference.

$(document).ready(function () {

    var loginBtn = document.getElementById("loginBtn");

    //SHOW MOBILE NAVIGATION ON HAMBURGER CLICK
    function displayLogin(e) {
        e.preventDefault();
        $('#loginContainer').fadeToggle("fast");
        $('#loginContainer').removeClass("hide");
    }

    //EVENT LISTENERS
    loginBtn.addEventListener("click", displayLogin);

});
.loginBtn {
    color: #1493d1;
    background: #fff;
    border-radius: 98px;
    cursor: pointer;
    margin: 15px 0 0 0;
    padding: 8px 15px;
    position: absolute;
    right: 0px;
    /*float: right;*/
    text-decoration: none;
}

.loginContainer {
    width: 175px;
    height: 250px;
    background: #fff;
    border-radius: 10px;
    position: absolute;
    top: 80px;
    right: 0px;
    /*float: right;*/
}

.hide {
    display: none !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="loginBtn" class="loginBtn" href="/">Login</a>
<div id="loginContainer" class="loginContainer hide">
    <a class="registerBtn" href="/">Register</a>
</div>

If this does not solve the issue, please let me know.

Answer №2

Animations do not function when the display property is set to none because it is not a process, but rather an action.

One solution could be to adjust the height or width of the element to 0 while animating it, and then switch the display property to none once the animation is complete or when the user presses a specific key.

Another option is to animate the opacity change, which may help resolve the issue you are facing.

Answer №3

$('.title').on('click', function() {
    var boxOpacity = parseInt($('.box').css('opacity'));
    $('.box').animate({
        opacity: boxOpacity == 0 ? 1 : 0
    });
});

Click here for the demo!

This script utilizes opacity instead of display: none;

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

Implementing Conditional Drag and Drop Functionality with DragControl in three.js

Is there a way to toggle the activation status of DragControls based on the value of a flag called drag_flag? My current approach doesn't seem to be working as intended. I would appreciate it if you could provide guidance on how I can achieve this co ...

Triangle below header that seamlessly blends with header background gradient

I need to create a unique header design which includes a gradient and a triangle for decoration. While I have no trouble implementing the gradient and positioning the triangle using ::after, I am facing an issue with matching the color of the triangle to ...

The width of the inline element is determined prior to the JSON content being loaded

One issue I am facing involves styling an inline element, specifically an <a> element. The code in question is: <a class="text></a> .text{ display:inline-block; background:red; padding:5px } The problem arises when trying to load text ...

Storing information while navigating with react routers

I am working on a project where I need to create multiple pages with forms. As the user navigates from one page to another, I want the data they entered to be saved automatically in the state of that component. The user has not submitted anything yet and t ...

Revolutionary Notification System - Harnessing the Power of Cutting-Edge notification

My system is working well, but I'm facing an issue with the GET method. It keeps using the same notification_id from the while loop for every search, resulting in repetitive requests. Here's an example of what's happening: jquery....486299 ...

Angular neglects the specified animation timing

I created a sidebar component that relies on the sidebarExpanded state passed through Input(). Despite the correct animation trigger upon input change, the width adjustment happens abruptly. Interestingly, the same animation code works flawlessly in anothe ...

Establishing a recurring interval when the component mounts for a specified period of time

I have read through several Q&As on this topic, but I am still unable to pinpoint what mistake I am making. The code snippet below is meant to display a countdown in the console and update the DOM accordingly, however, it only prints 0s in the console ...

Transform the JSON.pretty_generate() function into a visually appealing accordion layout

Within my Ruby on Rails application, the JSON file is being dynamically generated via the controller. Currently, I am rendering it using render :json => JSON.pretty_generate(@xml_hash), :status => :ok, and it is displaying in a tree-like structure. ...

Unable to fetch JSON values using Jquery

I'm struggling to identify the error in my code. The line $.getJSON is designed to fetch data through an AJAX request by passing the currently selected fruit name. The retrieved data, which consists of an array with 'variety' index containi ...

What is the best way to integrate the each_slice method in Rails views with React components

In my Parent component Monsters, I am rendering the Monster component. for each monster in @state.monsters React.createElement Monster, key: monster.id, monster: monster, team: @state.team... Within the monster: div className: 'col-md-4' ...

Creating an illuminated atmosphere: How to place a light source within a container using ThreeJS

Attempting to replicate the luminous box from Beat Saber in ThreeJS: https://i.sstatic.net/6IUmp.png Initiated by crafting a shape in Blender and exporting an OBJ. Imported it into Three, simply as geometry: https://i.sstatic.net/BGA1n.png Subsequently ...

Image spotlight effect using HTML canvas

I am currently seeking a solution to create a spotlight effect on an HTML canvas while drawing an image. To darken the entire image, I used the following code: context.globalAlpha = 0.3; context.fillStyle = 'black'; context.fillRect(0, 0, image. ...

Tips for obtaining Array elements within a JSON object utilizing handlebars

var express = require('express'); var router = express.Router(); var data = { products :{ name:'Computers', price:'500' }, items:[ {name: 'Keyboard' , cost:'50'}, {name: 'M ...

Node.js is having trouble locating a module that has been installed

Upon transferring my node.js application to a different PC (where it functioned flawlessly on the development machine) and manually installing all the necessary dependencies, I encountered an error when attempting to run it: C:\Users\myself>n ...

"Can you explain the concept of an undefined id in an AJAX request

Within my mongodb database, I have two Tables: GstState Store While working on my Store form, I encountered an issue where the JSON response was returning an undefined id when trying to select a state based on country via an ajax call to fetch GstStates ...

Having difficulty passing a PHP variable to JS for updating an SQL file through PHP call

Having some difficulties with this issue. I am trying to transfer a PHP variable to JavaScript. The JavaScript then sends the variable to a php file like domain.com/updatesql.php?userid=USERNAME. Here's my index.php file which successfully passes the ...

Tips on handling a nested HTML hyperlink in Selenium using Python with IE11

Can anyone assist me in resolving the issue of clicking on a hyperlink within an HTML page that contains nested HTML elements up to 4 levels deep? I have attempted various methods using XPATH, but they do not seem to work during runtime. I am utilizing Sel ...

Enhance tick labels in C3.js with supplementary information

I need a specific format for the tick: tick: { fit: true, multiline: false, outer: false, format: function (x) { var value = this.api.ca ...

Responsive design is key in ensuring that Bootstrap columns adjust accordingly

I am attempting to create a responsive block in the center of my webpage with the help of Bootstrap. However, I am having trouble getting the columns to respond correctly to my bootstrap setup. Can anyone point out what I might be doing wrong? <lin ...

Is there a quicker method than using document.getElementById('element-id').innerHTML to retrieve content?

Currently, I am developing a user-friendly step-by-step wizard for my website to gather information about custom orders. Utilizing JavaScript, I have been tasked with updating the content of each "page" using the document.getElementById('element-id&ap ...