Home > Net >  add loading image next to the button using css
add loading image next to the button using css

Time:09-22

I am trying to align the loading image (CSS) next to the button. I tried from STO help but still image is not coming next to the button.

Here is the Jsfiddle

#loading-bar-spinner.spinner {
  left: 50%;
  margin-left: -20px;
  top: 50%;
  margin-top: -20px;
  position: absolute;
  z-index: 19 !important;
  animation: loading-bar-spinner 400ms linear infinite;
}

#loading-bar-spinner.spinner .spinner-icon {
  width: 20px;
  height: 20px;
  border: solid 4px transparent;
  border-top-color: #E4002B !important;
  border-left-color: #E4002B !important;
  border-radius: 50%;
  float: left;
}

@keyframes loading-bar-spinner {
  0% {
    transform: rotate(0deg);
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
    transform: rotate(360deg);
  }
}
<form name="sample_data" class="form2">
  <label for="fname">First Name</label>
  <input name="fname" id="fname_id" type="text"><br />
  <label for="lname">Last Name</label>
  <input name="lname" id="lname_id" type="text">
  <input type="submit" class="sfmc_btn" id="subm_sfmc" value="submit">
  <div id="loading-bar-spinner" class="spinner">
    <div class="spinner-icon"></div>
  </div>
</form>

CodePudding user response:

#loading-bar-spinner.spinner {
    display: inline-block;
    animation: loading-bar-spinner 400ms linear infinite;
    }

    #loading-bar-spinner.spinner .spinner-icon {
        width: 12px;
        height: 12px;
        border: solid 3px transparent;
        border-top-color: aliceblue;
        border-left-color: #E4002B !important;
        border-bottom-color: #E4002B;
        border-radius: 50%;
        float: left;
    }

CodePudding user response:

The solution is to wrap the button and spinner together into an element with display inline-block, and then wrap the spinner itself into an element with display inline block.

Please, see the Jsfiddle

$(document).ready(
  $(function() {
    $("#loading-bar-spinner").show();
    $('form:not(.form2)').submit(function(e) {
      e.preventDefault();
      if ($(".form1").validate().checkForm()) {
        $(this).find('input[type=submit]').prop('disabled', true);
        $(this).find('input[type=submit]').prop('value', 'Please wait...');
      }
    });

    $(".form2").submit(function(e) {
      e.preventDefault();
      if ($(".form2").validate().checkForm()) {
        $(this).find('input[type=submit]').prop('disabled', true);
        //$(this).find('input[type=submit]').prop('value', 'submit');
        $("#loading-bar-spinner").show();
      }
    });
  })
);
#loading-bar-spinner.spinner {
  /* left: 50%; */
  /* margin-left: -20px; */
  top: 50%;
  margin-top: -20px;
  position: absolute;
  z-index: 19 !important;
  animation: loading-bar-spinner 400ms linear infinite;
}

#loading-bar-spinner.spinner .spinner-icon {
  width: 20px;
  height: 20px;
  border: solid 4px transparent;
  border-top-color: #E4002B !important;
  border-left-color: #E4002B !important;
  border-radius: 50%;
  float: left;
}

@keyframes loading-bar-spinner {
  0% {
    transform: rotate(0deg);
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
    transform: rotate(360deg);
  }
}

.sfmc_btn > *{
  border:1px solid red;
}

.wrap, .spinner {
  display: inline-block;
  position: relative;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<form name="email" class="form1" action="" method="post">
  <label for="email">Your Email</label>
  <input name="Email" id="email" type="email">
  <input type="submit" value="submit">
</form><br /><br />
<h2>
  Testing Form
</h2>
<form name="sample_data" class="form2">
  <label for="fname">First Name</label>
  <input name="fname" id="fname_id" type="text"><br />
  <label for="lname">Last Name</label>
  <input name="lname" id="lname_id" type="text">
  <div class="wrap">
  
  <input type="submit" class="sfmc_btn" id="subm_sfmc" value="submit">
  <div class="spinner">
    <div id="loading-bar-spinner" class="spinner">
        <div class="spinner-icon"></div>
    </div>
  </div>
  </div>
  
</form>

CodePudding user response:

See the comments in the code for changes made.

#loading-bar-spinner.spinner {
  left: 50%;
  /* margin-left: -20px; */
  top: calc(50% - 3px); /* <-------- Used calc to position vertically */
  /* margin-top: -20px; */
  position: absolute;
  z-index: 19 !important;
  animation: loading-bar-spinner 400ms linear infinite;
}

#loading-bar-spinner.spinner .spinner-icon {
  width: 20px;
  height: 20px;
  border: solid 4px transparent;
  border-top-color: #E4002B !important;
  border-left-color: #E4002B !important;
  border-radius: 50%;
  float: left;
}

.form2 {              /* added to allow */
  position: relative; /* positioning within */
}                     /* this element */

@keyframes loading-bar-spinner {
  0% {
    transform: rotate(0deg);
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
    transform: rotate(360deg);
  }
}
<form name="sample_data" class="form2">
  <label for="fname">First Name</label>
  <input name="fname" id="fname_id" type="text"><br />
  <label for="lname">Last Name</label>
  <input name="lname" id="lname_id" type="text">
  <input type="submit" class="sfmc_btn" id="subm_sfmc" value="submit">
  <div id="loading-bar-spinner" class="spinner">
    <div class="spinner-icon"></div>
  </div>
</form>

CodePudding user response:

<style>

    #loading-bar-spinner.spinner {
        position: absolute;
        animation: loading-bar-spinner 400ms linear infinite;
    }

    #loading-bar-spinner.spinner .spinner-icon {
        width: 20px;
        height: 20px;
        border: solid 4px transparent;
        border-top-color: #E4002B !important;
        border-left-color: #E4002B !important;
        border-bottom-color: #E4002B !important;
        border-radius: 50%;
        float: left;
     }

@keyframes loading-bar-spinner {
   0% {
      transform: rotate(0deg);
      transform: rotate(0deg);
    }

   100% {
     transform: rotate(360deg);
     transform: rotate(360deg);
    }
   }

</style>
<body>
    <form name="sample_data" class="form2">
        <label for="fname">First Name</label>
        <input name="fname" id="fname_id" type="text"><br />
        <label for="lname">Last Name</label>
        <input name="lname" id="lname_id" type="text">

        <div style=" top:25px ; left:280px ; position: fixed; widht:100px;  " >
                <div  style="float:left;" >
                        <input type="submit" class="sfmc_btn" id="subm_sfmc" value="submit">
                </div>
                    
                        
            
                <div style="float: right; margin-left:10px;" >
                    <div id="loading-bar-spinner" class="spinner">
                        <div class="spinner-icon">
                    </div>
                </div>

        </div>
        
            
      </div>
    </form>
</body>

Try this !!!

CodePudding user response:

Surround the button and the div for the spinner in a parent div. Give it a class name. Then in your css, give the class a display of flex, something like this;

<div class"wrapper">
      <input type="submit"..../>
      <div id="loading-bar-spinner" class="spinner">
           <div class="spinner-icon"></div>
       </div>
</div>

Now give wrapper a display of flex to position the children next to each other like so;

.wrapper{
     display: flex;
 } 

You can also read about justify-content and align-item of flex items to position the children elements as required. NB: Don't mind the poor format of my reply, I'm typing from a mobile phone

  • Related