Dynamic sliding effect in CSS for seamless showing and hiding of div elements

I stumbled upon a fantastic solution in these forums How to create sliding DIV on click? However, what I really wanted was for the content to fade in and out with just a click of a button. Here is the code snippet I am currently working with:

<html>
    <head>
        <title>CSS - Slider Test</title>
        <style type="text/css">
            .slide {
                /*visibility:hidden;*/
                border: 1px solid black;
                height: 0px;
                width:400px;
                overflow:hidden;
                transition:             height 500ms ease;
                    -moz-transition:    height 500ms ease;
                    -ms-transition:     height 500ms ease;
                    -o-transition:      height 500ms ease;
                    -webkit-transition: height 500ms ease;
            }

        </style>
        <script type="text/javascript">
            function slider(){
                var obj = document.getElementById('slide');//
                 //obj.style.visibility= (obj.style.visibility=='hidden')?'visible':'hidden';
                obj.style.height = ( obj.style.height == '0px' || obj.style.height == '' ) ?  '150px'  :  '0px';
            }
        </script>
    </head>
    <body>

    <input type="button" onclick="slider();" value="Click"/>

    <div id="slide" class="slide">
        <table>

            <tr>
                <th colspan="2" align="center">
                    Formats
                </th>
            </tr>
            <tr>
                <td style="width:140px;"><span id="lblCountry">Culture</span></td>
                <td>
                    <select name="cultureList" id="cultureList">
                        <option selected="selected" value="1033">English (United States)</option>
                        <option value="2057">English (United Kingdom)</option>
                        <option value="3084">French (Canada)</option>
                        <option value="4105">English (Canada)</option>

                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <span id="lblNumber">Number</span>
                </td>
                <td>
                    <input name="number" type="text" value="123,456,789.00" id="number" style="width:120px;" />
                </td>
            </tr>
            <tr>
                <td><span id="lblCurrency">Currency</span></td>
                <td>
                    <input name="curr" type="text" value="$123,456,789.00" id="curr" style="width:120px;" />
                </td>
            </tr>


        </table>


    </div>
     <div>
            Some other content
        </div>
    </body>
    </html>

While the current code successfully functions on click, my struggle lies in setting its default visibility to none. I would like it to become visible upon clicking the button before the animation takes place, and then disappear once the animation completes after the second click. Could someone please provide me with guidance? Thanks

Answer №1

When modifying your slider function(), you initially had the following code:

obj.style.visibility = (obj.style.visibility == 'hidden') ? 'visible' : 'hidden';

To simplify, all you need to do is check if visibility is currently visible

obj.style.visibility = (obj.style.visibility == 'visible') ? 'hidden' : 'visible';

Check out the DEMO

Additionally, I recommend updating your CSS transitions to include all properties rather than just height. This way, the user won't notice the visibility changes until the transition completes.

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

What is the mechanism by which a custom hook, functioning as a trigger, initiates the re-rendering of a separate function component?

According to the official documentation on Custom React Hooks, one particular use case for utilizing a custom hook is demonstrated through the following example: function FriendListItem(props) { const isOnline = useFriendStatus(props.friend.id); retur ...

Executing Lambda functions on DynamoDB has no effect

Below is the code snippet for a Lambda function: console.log('Loading function'); var aws = require('aws-sdk'); var ddb = new aws.DynamoDB(); function retrieveUser(userid) { var query = ddb.getItem({ TableName: "Users", ...

Tips for customizing the color of Menu in material-ui v5

I've been searching for solutions to change the background color of the Menu, but the methods I found are outdated. The use of @mui/styles and makeStyles is now deprecated, as stated in mui.com/styles/basics/#hook-api. I attempted to change the backgr ...

The media query will only impact elements and classes within the index.html file, and will not apply to any other

As I strive to optimize my website for mobile devices, I am encountering an issue with my media queries. The classes don't seem to respond appropriately on any page other than the index.html, and I'm struggling to identify the root cause. Index. ...

Utilizing Adjacent Sibling Selectors within the context of a specific enclosing class

I have a question regarding the following HTML structure: <div class="a"> <div class="b"></div> <div class="c"></div> </div> My goal is to apply a specific CSS rule only within the a class: .b + .c { margin-le ...

The ASP.NET MVC3 form collection registers as 0 when performing a jQuery ajax post

I'm currently developing a project on ASP.NET MVC3. My current challenge involves POSTing to a method that should return a set of data using the jQuery.ajax api. However, upon handling the request on the server, I noticed that the form collection&apo ...

CKEditor is removing tags and attributes when formatting HTML emails

I have developed an application that enables users to import HTML emails and make adjustments before forwarding them. I am attempting to utilize CKEditor for modifying the imported email; however, it appears to be removing bgcolor tags (and possibly more). ...

Error: JSON parsing failed due to an unexpected token 'S' at position 17

Trying to troubleshoot a syntax error in JSON.parse() within a product warranty registration process. Transitioning from Java to AngularJS for this project, I have an API built in PHP handling the back-end operations and a controller managing communication ...

I am having trouble getting the (:active) pseudo-class to function properly with the menu on my WordPress theme

Purchased a theme called HelpGuru and encountering issues with the CSS of the menu. Reached out to support but they were unable to assist, directing me to a company that specializes in customizing WordPress themes instead. The link to the demo can be found ...

Executing Sequential Jquery Functions in ASP.Net

I have successfully implemented two JQuery functions for Gridview in ASP.Net 1. Enhancing Gridview Header and Enabling Auto Scrollbars <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script& ...

The setState method fails to properly update the state

I am currently utilizing React JS. Below is the code for my React class: class MyReactComponent extends React.Component{ constructor(props){ super(props); this.state = { passAccount: { email: "Email&quo ...

Using Reactjs to Send Emails

Trying to incorporate the SmptJs API for email sending using JavaScript has been quite successful in a simple HTML setup. However, I am facing challenges when attempting to integrate it into a ReactJs component! The API documentation and link can be found ...

Adjust the size of the Facebook share button

Is there a way to resize the Facebook share button? The default size provided by Facebook is way too small, and I'm struggling to make it larger. I've tried changing the class and even adding an ID, but so far nothing has worked as I hoped. Here ...

Resizable dimensions for the dark mode switch

My latest project involves creating a toggle button for switching between light and dark themes using CSS, HTML, and JavaScript: id("theme-btn").addEventListener("change", function() { if (this.checked) { qs(".box").setAttribute('style', ...

Utilize Typescript to aim for the most recent edition of EcmaScript

Are you looking to configure your typescript build to compile to the most recent version or the most current stable release of EcmaScript? For example, using this command: tsc --target <get latest version> Alternatively, in a tsconfig file: { & ...

Switching Profiles in XPages

In my Xpages project, I have two different user profiles set up. I am currently attempting to develop a function that would allow me to easily switch between these two profiles. However, I am unsure of the steps needed to accomplish this task. ...

Error: The callback specified is not a valid function

Here is a function example: reportAdminActions.reportMemberList(project, function(data) { console.log(data); }); This particular function gets called by another ajax operation, similar to the one shown below: reportMemberList: function(projectId, ca ...

ID could not be retrieved from the checkbox

I am facing an issue with my HTML checkboxes. The ids are generated from an angular ng-repeat, but when I try to collect the id for use, it always returns as undefined. $("input:checkbox.time-check-input").each(function () { var ruleUnformatted = ""; ...

validating links in Laravel

Is it feasible to validate a href element in laravel? For instance, I have various user responses in my guest book and each user has the capability to delete their posts. The deletion process is as follows: <div class="col-lg-2"> <ul class="l ...

Which specific transitionend (or animationend) event should I use for this application?

I'm feeling a bit lost when it comes to using transitionend (or if I should be using animationend in this scenario). I'm not sure whether to utilize var, node, or box. Essentially, I am a complete beginner in this case. My goal is to have my div ...