What is the best way to have a div gradually glide out (utilizing CSS's transition feature) upon the click of a particular button?

I want the hint-bubble div to smoothly slide out (using 'transition: 0.5s') whenever the hint-btn is clicked. I have successfully implemented it so that the hint-bubble appears when the button is clicked, but it appears instantly and I am struggling to slow down the transition.

HTML:

<body>
            <div class="hints">
                <p>Need assistance?</p>
                <br>
                <p>Click the button below for some hints!<p>
                <button class="hint-btn">Hints</button>
            </div>
            <div class="hint-bubble">I'd like this div to slide out when the "Hints" button is clicked</div>
        </div>
</body>

CSS:

.hints {
    right: 28rem;
    bottom: 33.4rem;
    border: 1px solid black;
    background-color: #707070;
    color: white;
    box-shadow: 0px 0px 1px 1px black;
    border-radius: 3px;
}

.hints p:first-child {
    padding-bottom: 0.4rem;
    border-bottom: 3px solid #a6a4a4;   
    margin-bottom: -1rem;
}
.hints p:nth-child(3) {
    font-size: 0.7rem;
}
.hint-btn {
    font-family: 'Montserrat', sans-serif;
    background: none;
    width: 3rem;
    font-size: 0.75rem;
    border-radius: 4px;
    border: 1px solid #595656;
    background-color: #F47B13;
    color: white;
    box-shadow: 0px 0px 1px #F47B13;
    outline: none;
    padding-top: 0.2rem;
    padding-bottom: 0.2rem;
}
.hint-btn:hover {
    background: #c76410;
    transition: 0.4s;
}
.hint-btn:active {
    background: #f2b683;
    transition: 0.6s ease-in-out;
}
.hint-bubble {
    width: 15.6rem;
    padding: 0.2rem;
    text-align: center;
    color: white;
    background-color: #707070;
    font-size: 0.8rem;
    border-radius: 3px;
    box-shadow: 0px 0px 1px 1px black;
    padding: 0.7rem;
    transition: 0.8s;
    
    right: 28rem;
    bottom: 32.5rem;
    display: none;
}
.hint-bubble:before {
  position: absolute;
    content: "";
    width: 0px;
    height: 0px;
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-top: 10px solid transparent;
    border-bottom: 0.8rem solid #707070;
    right: 7.2rem;
    top: -1.3rem;
}

Javascript:

const btnHint = document.querySelector(".hint-btn");
const hintBubble  = document.querySelector(".hint-bubble");
const hintsBox = document.querySelector(".hints");
let isOn = null;

btnHint.addEventListener("click", () => {
    if (isOn) {
        hintBubble.style.display = "none";
        isOn = false;
    } else {
        hintBubble.style.display = "unset";
        isOn = true;
    }
});

You can view it on codepen as well: https://codepen.io/gchib00/pen/ExNrvrR

Answer №1

Don't rely on the display property to smoothly transition visibility of elements,
instead, utilize opacity and pointer-events: none to maintain clickability

You can streamline toggling with classList.toggle to simplify your code and separate styles from the script for easier management

const btnHint = document.querySelector(".hint-btn");
const hintBubble = document.querySelector(".hint-bubble");
const hintsBox = document.querySelector(".hints");

btnHint.addEventListener("click", () => {

  hintBubble.classList.toggle("shown")

});
.hints {
  // CSS styling for hints container
  ...
}

// More CSS rules for different elements within the hints container

.hint-btn {
  // Styles for the hint button
  ...
}

// Additional styles for the hint bubble

.hint-bubble {
  // CSS properties for the hint bubble
  ...
}

... // more CSS code 

<div class="hints">
  <p>Need help?</p>
  <br />
  <p>Click button below to get some hints!</p>
  <button class="hint-btn">Hints</button>
</div>
<div class="hint-bubble">I would like this div to slide out when "Hints" button is clicked</div>

Learn more:

Answer №2

element provides valuable information on how to effectively utilize the CSS properties of opacity and visibility instead of using transition with the display rule. It also suggests modifying the default attributes of the .hint-bubble selector in the CSS file by adding visibility: hidden and opacity: 0, while removing display: none. Furthermore, it emphasizes the importance of reviewing the JavaScript code snippet provided to ensure smooth functionality. The code snippet includes event listeners for button clicks, toggling the visibility and opacity of a hint bubble. Additionally, the CSS styling for elements like .hints, .hint-btn, and .hint-bubble is detailed within the answer, along with specific design choices such as color schemes, box shadows, and transitions. Overall, the guidance offered in this response aims to optimize the user experience by enhancing the visual and interactive elements of the webpage through strategic CSS and JavaScript implementations.

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

Setting a JavaScript value for a property in an MVC model

I am currently working on an asp.net mvc application and I am in the process of implementing image uploading functionality. Below is the code for the image upload function: $(document).ready(function () { TableDatatablesEditable.init(); ...

A minimalist dropdown menu using only HTML and CSS with an onclick function

I have been working on creating an onclick dropdown menu for mobile devices that should disappear when viewed on wider screens, but I've encountered an issue where the links in the menus are not clickable even though the @media query is set up correct ...

Tips for customizing the selection tip of a custom cursor using CSS

I have integrated a color picker jquery plugin on my website to allow users to pick colors from an image. I made a modification in the CSS to change the cursor to an eyedropper icon. However, currently when clicking on the image, the color pointed by the u ...

JQuery Submission with Multiple Forms

Hey everyone! I have a jQuery form with multiple fieldsets that switch between each other using jQuery. Eventually, it leads to a submit button. Can someone assist me by editing my jfiddle or providing code on how I can submit this data using JavaScript, j ...

Why doesn't the Iframe onLoad event trigger when uploading a file?

I have a straightforward iframe <iframe class="ifr" src="about:blank"></iframe> It contains an onload handler. $(".ifr").on('load',function (){ alert("iframe loaded") }); There are also two buttons: Pressing the first button ...

Creating flexible Vue routes using a single router component and a navigational menu

I am working on a Vue project and I want to implement nested routes in my router.js. My goal is to have just one menu for all the nested routes and only one <router-view></router-view> in a master component. This is what I envision (in pseudoc ...

Guide on sharing a Vue project with other devices within a network

I'm a complete beginner when it comes to vue.js. After working on a few small projects, I want to make sure I understand how to share a vue project with others at this stage. Typically, I know that running 'npm run serve' in my project dir ...

HTML: Dealing with issues in resizing and resolution with floating elements on the left and right using

Encountering some issues with the HTML code below when resizing the window: 1: The right bar suddenly drops down if the width is made too small. 2: The spacing between the content and the right bar expands as the window width increases. <style ty ...

The anticipated reply was supposed to consist of an array, however it ended up being an

I'm brand new to Angular and I've been searching for a solution to my issue with no luck. My app is supposed to retrieve data from a MongoDB database and display it to the user. However, I keep getting this error message: Error: [$resource:bad ...

Conditional statement in Javascript for document.cookie

I am attempting to create a basic if statement that relies on the value of a cookie. The function looks like this: function setHomePage() { if ($.cookie('settingOne') == 'jjj') { $('.secO').css('display', & ...

Ways to determine if new data has been added to a MySQL table

Can anyone explain how the Facebook notification system operates? Here is my code snippet: (function retrieve_req() { $.ajax({ url: 'request_viewer_and_its_utilities.php', success: function(data) { $('.inte ...

Disallowing selection on input text field

I am seeking to disable selection on an input field while still allowing for focusing, with the following criteria: Preventing selection via mouse Preventing selection via keyboard (including Ctrl+A, shift+arrows) Permitting focus on the field using both ...

Issues with Weglot link hooks not functioning properly within the sticky header

I have integrated Weglot for translations on my website, aigle.ca. Due to issues with their widget, I am using link hooks instead. You can find more information about link hooks at: weglot.com link-hooks However, when scrolling down the page and the menu ...

Creating a Form in a Popup with Bootstrap

I have implemented Bootstrap on my website. Check it out at: hubb.tekkkz.com I am facing an issue where, when clicking on the login/register button on the right, the modal pops up. However, the user input elements within the modal are not taking up the ful ...

Customizing the appearance of the dragged element in jQuery UI

Is there a way to modify the color of a draggable list item after it has been dragged? Any assistance is greatly appreciated. $("#DragWordList li").draggable({helper: 'clone'}); ...

The specified instant cannot be located in 'moment' while attempting to import {Moment} from 'moment' module

Struggling in a reactJS project with typescript to bring in moment alongside the type Moment Attempted using import moment, { Moment } from 'moment' This approach triggers ESLint warnings: ESLint: Moment not found in 'moment'(import/n ...

The dimensions of the div are fixed and contains some text

Can someone help me with my coding issue? I created a custom drop-down menu, but the problem is that my content div does not expand to 100% of the width. It seems to be stuck at 160px. Here is the CSS code snippet: .dropdown-content { display: none; po ...

What are the steps for combining AngularJS with Java JAAS authentication system?

My web application combines AngularJS on the frontend with Java on the backend. Angular communicates with the Java backend through Restful webservices exchanging JSON data over HTTP. I am in need of developing an authentication mechanism for this app and I ...

Combining Mouseover and Click Events in Vue JS

Having four pictures, I want to display a specific component when hovering over them. However, I also need to bind the click event so that clicking on the picture will reveal the component. The challenge is that I am unable to simultaneously bind two event ...

I encountered an error while trying to deploy my next.js project on Vercel - it seems that the module 'react-icons/Fa' cannot be found, along with

I'm currently in the process of deploying my Next.js TypeScript project on Vercel, but I've encountered an error. Can someone please help me with fixing this bug? Should I try running "npm run build" and then push the changes to GitHub again? Tha ...