Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a spinner work using Spin.js?

Hello guys,

I am new at JavaScript and after tons of research on the Internet and failed attempts on implementing a spinner I decided to ask you.

I am using Spin.js ( http://fgnass.github.com/spin.js/#v1.2.6 ). It seems to be an great tool, but I simply cannot make it work. My question is what I doing wrong? I cannot really figure it out. Any help will be much appreciated. Thank you so much.

Here is my piece of code:

   <script src="Scripts/Spin.js" type="text/javascript"></script>

    <script type="text/javascript">
           function spinnerInit() {
               var opts = {
                   lines: 13, // The number of lines to draw
                   length: 7, // The length of each line
                   width: 4, // The line thickness
                   radius: 10, // The radius of the inner circle
                   corners: 1, // Corner roundness (0..1)
                   rotate: 0, // The rotation offset
                   color: '#000', // #rgb or #rrggbb
                   speed: 1, // Rounds per second
                   trail: 60, // Afterglow percentage
                   shadow: false, // Whether to render a shadow
                   hwaccel: false, // Whether to use hardware acceleration
                   className: 'spinner', // The CSS class to assign to the spinner
                   zIndex: 2e9, // The z-index (defaults to 2000000000)
                   top: 'auto', // Top position relative to parent in px
                   left: 'auto', // Left position relative to parent in px
                   visibility: true
               };

               var target = document.getElementById('spinnerContainer');
               //target.style.display = "block";
               var spinner = new Spinner(opts).spin(target);
           }
       </script>

      <script type="text/javascript">
           $(document).ready(function () {
               $('#btnPerformSave').click(function () {
                   spinnerInit();
               });
           });
       </script>

     <div id="spinnerContainer" class="spinner" style="width: 100%; height: 150%;
           position: absolute; z-index: 100; background-color: Gray;
           left: 0; top:  0; bottom: 0;right: 0">
       </div>
like image 462
SunnyDay Avatar asked Sep 03 '12 11:09

SunnyDay


3 Answers

Try replacing

var target = document.getElementById('spinnerContainer');
var spinner = new Spinner(opts).spin(target);

with

$('#spinnerContainer').after(new Spinner(opts).spin().el);

You need to append the html element the spin method creates to the dom

Here is an example to get you started http://jsfiddle.net/5CQJP/1/

like image 130
James Kyburz Avatar answered Oct 16 '22 08:10

James Kyburz


I am not sure if this question ever got answered, but for those who are still looking for an answer:

I found out that I needed to move the javascript underneath the spinnerContainer div. I believe this would also work if you put the javascript in an onload event. Here is what I did:

<div id="spinnerContainer" class="spinner" style="width:100px;height:100px;background-color: Gray;">
</div>
<script src="Scripts/Spin.js" type="text/javascript"></script>
<script type="text/javascript">
    var opts = {
      lines: 13, // The number of lines to draw
      length: 7, // The length of each line
      width: 4, // The line thickness
      radius: 10, // The radius of the inner circle
      corners: 1, // Corner roundness (0..1)
      rotate: 0, // The rotation offset
      color: '#000', // #rgb or #rrggbb
      speed: 1, // Rounds per second
      trail: 60, // Afterglow percentage
      shadow: false, // Whether to render a shadow
      hwaccel: false, // Whether to use hardware acceleration
      className: 'spinner', // The CSS class to assign to the spinner
      zIndex: 2e9, // The z-index (defaults to 2000000000)
      top: 'auto', // Top position relative to parent in px
      left: 'auto' // Left position relative to parent in px
    };
    var target = document.getElementById('spinnerContainer');
    var spinner = new Spinner(opts).spin(target);
</script>
like image 25
minchYoda Avatar answered Oct 16 '22 10:10

minchYoda


Here is my answer. Works great.

//Declare Script
<script src="Scripts/spin.js" type="text/javascript"></script>


 <button  id="btnSearchClick">Search</button>

//Displays Loading Spinner
<div id="loading">
    <div id="loadingcont">
        <p id="loadingspinr">
        </p>
    </div>
</div>



<script type="text/javascript">
     //On button click load spinner and go to another page
     $("#btnSearchClick").click(function () {  
        //Loads Spinner
        $("#loading").fadeIn();
        var opts = {
            lines: 12, // The number of lines to draw
            length: 7, // The length of each line
            width: 4, // The line thickness
            radius: 10, // The radius of the inner circle
            color: '#000', // #rgb or #rrggbb
            speed: 1, // Rounds per second
            trail: 60, // Afterglow percentage
            shadow: false, // Whether to render a shadow
            hwaccel: false // Whether to use hardware acceleration
        };
        var trget = document.getElementById('loading');
        var spnr = new Spinner(opts).spin(trget);

        //Go another page.
        window.location.href = "http://www.example.com/";
   });
 </script>
like image 20
Apollo Avatar answered Oct 16 '22 08:10

Apollo