0

I have a simple UI like below: enter image description here

when clicks on submit, it redirects to another route/url. But it takes a little bit time to load the next page cause when I click the submit button, in the backed, it runs few machine learning models.

Now, during this loading and processing time, I want to display a page loading GIF. During the display of page loading, I want my form to fade away and only display the GIF in the middle of the page.

I have tried different ways to do it, but seems it's not working perfectly.

The form submission code:

<form class="form-signin" id="myform" action="data" method="post" enctype="multipart/form-data">
        <p class="form-signin-heading">upload input sample in csv format to run the model inference</p>

        <input class="form-control-file" type="file" name="upload-file" value="">
        <input class="form-control-file" type="submit" name="" value="Submit">
</form>

Any help?

sksoumik
  • 845
  • 1
  • 9
  • 23
  • Honestly, I have less idea on front-end dev. Whatever works, it's okay for me. It's just a simple demo for machine learning model inference, not the main part of my app. So, JS/JQuery anything is okay for me as long as it works. – sksoumik Dec 23 '20 at 21:01
  • Does this answer your question: https://stackoverflow.com/questions/6121203/how-to-do-fade-in-and-fade-out-with-javascript-and-css ? – Greg Dec 23 '20 at 21:14
  • @Greg not exactly. – sksoumik Dec 23 '20 at 21:34

2 Answers2

2

This cam be a starting point. When the submit button is clicked, the form will fade out completely, and the spinner will appear. Once your backend is done processing, a redirect will load a the new page.

const form = document.getElementById('myform');
const spinner = document.getElementById('spinner');

form.addEventListener('submit', (e) => {
  e.preventDefault();
  form.classList.toggle('fade-out');
  spinner.classList.toggle('show');
});
form{
  text-align: center;
  transition: opacity 0.6s ease-out;
}

img{
  display: none;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.show{
  display: block;
}

.fade-out{
  opacity: 0;
}
<form class="form-signin" id="myform" action="data" method="post" enctype="multipart/form-data">
    <p class="form-signin-heading">upload input sample in csv format to run the model inference</p>

    <input class="form-control-file" type="file" name="upload-file" value="">
    <input class="form-control-file" type="submit" name="" value="Submit">
</form>
<img src="https://www.dariusland.com/images/load.gif" id="spinner" class="hide">

Note: in the js, I added e.preventDefault() to show the demo, but you obviously don't need it.

clod9353
  • 1,942
  • 2
  • 5
  • 20
1

this should work

the loader will show up when your page is loading and when ready it will fade

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>Page Title</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <link rel='stylesheet' type='text/css' media='screen' href='main.css'>
    <script src='main.js'></script>
</head>
<body onload="load()">



    <!--Place your code here-->




    <div class="loader">

              <!--Change the "src" to your preferred loading GIF-->
        <img src="https://media.giphy.com/media/xTk9ZvMnbIiIew7IpW/giphy.gif" alt="">
    </div>

<style>
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }

    .loader {
        display: block;
        position: fixed;
        width: 100vw;
        height: 100vh;
        background-color: rgb(36, 36, 36);
        z-index: 9999;
        justify-content: space-around;
        align-items: center;
        display: flex;
    }

    .loader h1 {
        font-size: 4rem;
        color: #fff;
        -webkit-animation: loader-anim 3s ease-in-out infinite alternate;
        -moz-animation: loader-anim 3s ease-in-out infinite alternate;
        animation: loader-anim 3s ease-in-out infinite alternate;
    }

    @keyframes loader-anim {
    30% {
        transform: translateY(-3vh);
    }
    100% {
        transform: translateY(0);
    }
    }
</style>
<script>
var loader = document.querySelector('.loader');

//The value after the function is a 500ms delay before closing the loader
function load() {
  setTimeout(function(){loader.style.display = "none"}, 500);
}
</script>
</body>
</html>
Parham Moieni
  • 78
  • 1
  • 7