Can you provide instructions on how to create a fading effect when clicking on a button before redirecting to another linked website on the same page?

Currently working on a website and my goal is to have the whole page fade out before transitioning to another linked website. Once the new website is loaded, I want it to smoothly fade in.

Any suggestions for coding this effect?

Answer №1

One way to create a smooth transition effect before navigating to a new page is by using CSS animations for the fade-out effect. Then, you can implement a JavaScript function with a setTimeout method to delay the navigation process until after the animation completes.

function goToNewPage(){

    const targetElement = document.querySelector('.websiteLink');
    targetElement.classList.add('fade-out'); // apply fade-out animation to the element

    setTimeout(() =>{
        location.assign('https://example.com')
    } , 3000) // navigate to the new page after a 3-second delay

}
.websiteLink {
    padding: 10px;
    background-color: blue;
    color: #fff;
}
.fade-out {
    animation: fadeout 3s linear 1 forwards;
}

@keyframes fadeout {
    from {
        opacity: 1;
    }
    to {
        opacity: 0;
    }
}
<div class="websiteLink" onclick="goToNewPage()">Click here to visit the website</div>

Note: To make the JavaScript function reusable for multiple elements, consider using the data-href attribute or any other custom data set.

document.querySelectorAll('[data-link]').forEach(element =>{  
   element.onclick = () =>{
       element.classList.add('fade-out');
       const url = element.dataset.link || element.getAttribute('data-link');
       setTimeout(() => location.assign(url), 3000);
   }

})
.websiteLink {
    padding: 10px;
    background-color: blue;
    color: #fff;
    margin: 10px 0px;
}
.fade-out {
    animation: fadeout 3s linear 1 forwards;
}

@keyframes fadeout {
    from {
        opacity: 1;
    }
    to {
     opacity: 0;
    }
}
<div class="websiteLink" data-link="https://example.com">Visit Example Site</div>
<div class="websiteLink" data-link="https://anotherexample.com">Another Example Website</div>

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 causes the appearance of 'GET/ 304 --' in my code? (vue.js, express)

While attempting to fetch data on the client-side using axios in vue.js, I encountered a server-side error with the code 'GET/ 304 --' The reason for this occurrence is unclear to me and I am unsure of how to troubleshoot or resolve it. If I re ...

Lack of intellisense support for .ts files in Visual Studio Code

Currently, I am using Visual Studio Code 1.17.2 on Arch Linux to kickstart my work with Node.js/Angular4. To avoid confusion caused by loosely typed code, I have decided to switch to TypeScript for my NodeJS server as well. This is why my main file is name ...

Modify the background color of a pseudo-element's property value dynamically

How do I change the background color of my burger menu by clicking on an icon? This is the CSS code I have: :root{ --pseudo-backgroundcolor: yellow; } .menu__icon span, .menu__icon::before, .menu__icon::after{ background: va ...

Automatically navigate to a specific selection within the Autocomplete feature

Imagine having a dropdown or Autocomplete list in Material-UI with a long list of items. How can you make the list automatically scroll to a specific item when you open the dropdown? For instance, if we take the list of top100Films, the initial displayed i ...

Processing JSON data in a Laravel Controller

Currently working with Laravel, I am attempting to extract a specific data from a JSON in a controller and then pass the entire JSON to a view (which is currently functioning correctly). $url= 'https://example.com/api; $client = new \Guzz ...

I'm having trouble finding the issue in this straightforward code snippet. Can someone pinpoint the error for

(server-side) const express = require('express'); //setting up app instance const app = express(); //middleware const bodyParser = require('body-parser'); app.use(express.urlencoded({extended: false})); app.use(express.json()); //imple ...

The onChange function is not triggered when the dropdown menu consists of only one item

I am facing an issue with my dynamically populated dropdown menu. When the dropdown contains only one element, the onChange() function of my select tag does not work as expected. It functions perfectly fine when there are multiple elements present. How c ...

The React component patiently waits for the necessary props before rendering

When declaring a component within another parent component, I prefer to define specific props in one file and then assign additional shared properties in the parent component. However, I am encountering an issue where the child component fails to render be ...

The Jquery ajax request fails to function properly once the webpage has been published live

We've implemented a basic jQuery ajax call. A "more" button triggers a call to another PHP file when clicked. While it worked fine on my local server and initially on the live server, it suddenly stopped working live. The code functions perfectly o ...

The function cannot be applied to d[h] due to a TypeError

Having some trouble with my code here. I'm trying to set up routes to display a gif using CSS animation when the page is loading. The gif shows up initially, but then everything fades out and the gif remains on the page. Additionally, I'm getting ...

redux reducer failing to update state within store

I am completely new to redux and still have a lot to learn. I am currently working on a project that requires setting up a redux store and using state from that store. However, as I try to update the state through a reducer, the code fails to work properly ...

Moshi's version of Gson's "serializeNulls" setting

Switching from Moshi to Gson, I made a change in the backend to serialize responses with nullable values as { "value": null } rather than {}. While neither Moshi nor Gson offer this feature by default, Gson provides an option to enable it directly in the ...

Sending user input data from a React text field to a function as arguments

Below are input fields, from which I need to retrieve the entered values and pass them to the onClick event of the button displayed below. <input type="text" style={textFieldStyle} name="topicBox" placeholder="Enter topic here..."/> <input type=" ...

Contrasting the effects of using state separately versus together within ReactJS

I define my state like this: const [state,setState] =useState({ type:false, imageSrc:'', captionImage:'', showImage:false, }); When I update the state, I do it like this: setState({type:true}); setState({captionImage:& ...

The iron-session package does not export the path ./next/dist from the package

I am encountering an issue while using iron-session. Below is the code snippet causing the error: import { withIronSessionSsr } from 'iron-session/next/dist'; ... export const getServerSideProps = withIronSessionSsr(async function ({ req, r ...

Updating and showing a variable in a PHP file using JavaScript within an HTML webpage

My goal is to establish a variable in a PHP file on my server named "likes." Subsequently, I wish to incorporate a like button on my HTML webpage that, when clicked, will utilize JavaScript to modify the "likes" variable in the PHP file and increase it by ...

Creating Transparent Rounded Backgrounds in Google Chrome Packaged Apps: Achieving the same look as Google Hangout app

Looking at the screenshot below, it is evident that the Hangout app has a fully transparent design with background shadow effect applied to it. I have tried various methods in vain, such as applying CSS styling to the "html" and "body" tags of the page, a ...

What methods are available in JavaScript regex for validating city names?

var cityRegex = /^[a-zA-z] ?([a-zA-z]|[a-zA-z] )*[a-zA-z]$/; is the regular expression I attempted to create. However, it fails when inputting a city name like "St. Petersburg." Update: It seems challenging to create a perfect regex pattern for city name ...

Is there a way to adjust the width of the info panel in an SVG to automatically match the width of the SVG itself?

I am looking to place the information panel at the bottom of my SVG map and have it adjust its width according to the width specified in the viewBox. This way, even if I resize the map, the info panel will automatically adjust to fill completely based on t ...

Troubles with jQuery mobile functionality when utilizing hyperlink urls within a table

Currently, I am utilizing jQuery mobile to populate a table using ajax. One of the fields in the table is a hyperlink (href) that has a class assigned to it to trigger an alert on the screen. However, the alert does not appear when clicked: I have attempt ...