Reduce opacity of div upon clicking javascript:history.back()

I have successfully implemented a fadeIn CSS animation, but I am facing an issue where the block disappears abruptly when I click the close button. Is there a way to reverse the animation when the user clicks the "close" link (which triggers javascript:history.back())?

Check out the demo here

#showme:target {
    display: block;
    position: fixed;
    width: 100%; height: 100%;
    top: 0; left; 0;
    background: lightblue;
    -webkit-animation: fadein 1s; /* Safari, Chrome and Opera > 12.1 */
       -moz-animation: fadein 1s; /* Firefox < 16 */
        -ms-animation: fadein 1s; /* Internet Explorer */
         -o-animation: fadein 1s; /* Opera < 12.1 */
            animation: fadein 1s;
}

Answer №1

If you're looking to add some nice animations, why not consider using jQuery? However, it's important to think about the purpose behind incorporating animations with the history.back() function. The code snippet below provides a solution that avoids excessive CSS.

Here is the HTML code:

<div id="showme" style="display:none;">
    <p>I'm now visible!</p>
    <p><a href="#" id="close">Close</a></p>
</div>

<div id="wrapper">
    <a id="show" href="#">Show</a>
</div>

For the JavaScript part:

$('#close').on('click', function(event) {
    event.preventDefault();
    $('#showme').fadeOut('slow');
});

$('#show').on('click', function(event) {
    event.preventDefault();
    $('#showme').fadeIn('slow');
});

And here's the necessary CSS:

body {margin: 0;}

#showme {
    display: block;
    position: fixed;
    width: 100%; height: 100%;
    top: 0; left: 0;
    background: lightblue;
}

You can view the interactive example on fiddle: http://jsfiddle.net/cmqg7cyf/4/

Answer №2

To achieve this effect, simply incorporate the jQuery fadeOut method.

The initial argument denotes the duration of one second as stated in your CSS code. The subsequent argument is a callback function that executes once the fade out operation is complete.

$('#showme').fadeOut(duration, function () {
    history.back();
});

This particular line should be included within the event handler for when the close button is clicked.

Best of luck!

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

Using the mouse scroll to activate the $anchorScroll effect in Angular instead of a click

I have been searching for a solution to my problem without any luck so far. Hopefully, someone here can provide guidance and help me find a solution. My goal is to replicate the behavior shown in the second example on the following page: https://docs.angu ...

Why is Bootstrap taking precedence over my CSS body styles?

After switching to Bootstrap 4, I noticed that my body background color is always white due to the update. Even after attempting to set a more specific class, the issue still persists. Can anyone offer assistance? Your help is greatly appreciated. ...

Completing a third-party htaccess file

What's the best way to create a nodejs script that periodically monitors a 3rd-party website, but encounters issues with a username and password prompt generated by .htpasswd/.htacces authentication? How can nodejs handle this situation? ...

Can a collapse that was saved in a separate file be displayed on the requested page?

Imagine having a navigation bar on your website's index page with four buttons, each leading to a different page. What if you wanted to bring all those separate pages together into one by using collapsible elements? That's the challenge this user ...

Can flexbox be utilized on iPhone 4/iOS4, or is there a backup option available?

I have been scouring the internet but haven't found a solution yet. I am trying to make my flexbox grid rows compatible with iPhone 4, but I can't seem to make it work. The version of Safari on iOS4 does not support the new flexbox syntax, so I t ...

The newly added radio button does not function as a separate group as expected

I currently have a set of radio buttons: <input type="radio" class='form-control' name="progress_type[]" value="Journal Papers"><span class='radio-label'>Journal Paper</span> <input type="radio" class='form-co ...

Dynamically Growing Navigation Bar Elements with Nested Subcategories Based on Class Identification

Let's say you have a menu bar structured as follows: <nav> <ul class="nav"> <li class="menu1"><a href="#">Menu Item 1</a></li> <li class="menu2"><a href="#">Menu Item 2</a> <ul& ...

Updating style of an element by hovering over another

What is the best way to change the CSS of one element when hovering over another element in CSS? We can achieve this using the following code: .example .example2 li:hover .element If our HTML looks like this: <div class='example'><di ...

Flex containers positioned next to each other without overlapping and adjusting in relation to one another

I would like to prevent the lower div (which includes an image and a button) from overlapping with the division above it that contains text. So, when the button encounters this upper division, I want it to move down in a way that pushes the image beneath i ...

Javascript - issue with accurately retrieving element offset value while scrolling

My goal is to dynamically change classes for elements based on their position during scrolling. I am trying to calculate the top offset element value and adjust the classes accordingly. Here is the function I am using: handleScroll () { const header = ...

What causes the discrepancy between a website's source code and the code viewed in the browser inspection tool? (Regarding web scraping)

This is my first programming project and I apologize in advance for any mistakes in terminology. My Objective: I am currently working on a project to scrape data from my local library's website. My main goal is to automate the process of renewing bo ...

Protecting Your Data with Python Web Scraping Techniques

Currently, I am in the process of setting up an automated Web Scraping system using Python to extract HTML data from a website and transform it into a JSON file with a predefined format. While I have successfully obtained the HTML data as a .text file util ...

A guide to customizing the appearance of Textfield labels in Material-UI using React

Can someone assist me with changing the label color in Material-UI from grey to black? I am new to Material-UI and struggling to figure it out. Below is the code snippet: import React from "react"; import ReactDOM from "react-dom"; import { TextField, Bu ...

Issues with jQuery horizontal sliding gallery functionality

My attempt at creating a jQuery sliding video gallery is not working as I hoped. Instead of scrolling through the images when clicking arrow buttons, the entire div moves left or right depending on the direction. HTML: <div id="videocontainer"> & ...

The continuation of unraveling the mystery of utilizing `overflow-y:scroll` in a horizontal slider

As a beginner, I realized too late that adding code in comments is not an option. Consequently, I decided to create a new question and ask for your assistance once again. To optimize the use of space, I have implemented bxSlider with fixed dimensions (wid ...

What is the reason for having both "DOCTYPE: HTML5" and "HTML5" options in the web toolbar of VS 2012 RC?

Upon installing VS 2012 RC, I noticed that in the HTML toolbar dropdown, there are two options related to HTML5: DOCTYPE: HTML5 HTML5 I am curious to know what the distinction is between these two HTML5 choices. I was under the impression that the recom ...

Single-use condition for checkbox functionality in JavaScript

As a newcomer to javascript programming, I am struggling to make a checkbox display two radio buttons only when it is unchecked, even after researching solutions. Here is my current code snippet: <span><strong>Not Available</strong></ ...

Issue with div:after transition not functioning in Safari

The transition effect on div:after does not function properly in Safari. Below is the code for reference: HTML: .bbtn { width: 151px; height: 48px; background: #ef2e41; margin: auto; margin-top: 32px; -webkit-transition: all 0.3s ease-in-ou ...

Retrieving the height of the HTML body once all data has finished loading in AngularJS

Trying to determine the height of the HTML document (body) after loading content from a service using AngularJS. /* DISPLAY RECENT POSTS */ app.controller('RecentPostsController', function ($scope, $http) { $http.get("/site/recentpostsjs ...

The problem with styling a CSS button text

My website's CSS button used to have the text in the middle, but now it appears at the bottom and the button is smaller. Someone had access to my root folder and made changes that I am unaware of. .button { display: inline; margin-top: 2em; ...