I have a HTML page with a text input and a submit button.
<form name="form1" onsubmit="startSpinner()">
<input type="text" name="fname" >
<input type="submit" value="Submit">
</form>
I am creating a spinner using spin.js as follows in the startSpinner function
function startSpinner() {
var opts = {
lines: 17, // The number of lines to draw
length: 30, // The length of each line
width: 20, // The line thickness
radius: 60, // The radius of the inner circle
corners: 1, // Corner roundness (0..1)
rotate: 90, // The rotation offset
direction: 1, // 1: clockwise, -1: counterclockwise
color: '#000', // #rgb or #rrggbb or array of colors
speed: 0.8, // Rounds per second
trail: 33, // 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('spinner_span');
var spinner = new Spinner(opts).spin(target);
}
When I click Submit the spinner apears briefly (~1 sec) and then stops. I checked in the console - the spinner object is no longer in scope. What am I doing wrong?
Related
I'm trying to use http://spin.js.org/ to create a spinner on my site that starts spinning when an AJAX post fires and stops when it completes. I'm struggling to get the spinner working at all, though.
I have a node app and am templating with EJS. Under the usage section, spin.js's website says:
import {Spinner} from 'spin.js';
var opts = {
lines: 13, // The number of lines to draw
length: 38, // The length of each line
width: 17, // The line thickness
radius: 45, // The radius of the inner circle
scale: 1, // Scales overall size of the spinner
corners: 1, // Corner roundness (0..1)
color: '#ffffff', // CSS color or array of colors
fadeColor: 'transparent', // CSS color or array of colors
opacity: 0.25, // Opacity of the lines
rotate: 0, // The rotation offset
direction: 1, // 1: clockwise, -1: counterclockwise
speed: 1, // Rounds per second
trail: 60, // Afterglow percentage
fps: 20, // Frames per second when using setTimeout() as a fallback in IE 9
zIndex: 2e9, // The z-index (defaults to 2000000000)
className: 'spinner', // The CSS class to assign to the spinner
top: '50%', // Top position relative to parent
left: '50%', // Left position relative to parent
shadow: none, // Box-shadow for the lines
position: 'absolute' // Element positioning
};
var target = document.getElementById('foo');
var spinner = new Spinner(opts).spin(target);
I'm not sure where the import {Spinner} from 'spin.js' is supposed to go? I've searched around a lot and haven't been able to find out how to actually implement this. I found this example of a jquery plugin for spin.js but I'm struggling with that one as well. Any help would be much appreciated!
As of right now, this is what I have:
<div id="spinnerContainer" class="spinner" style="width:100px;height:100px;background-color: Gray; color:black;">
</div>
<script src="/scripts/spin.js/spin.js" type="text/javascript"></script>
<script>
var opts = {
lines: 20, // The number of lines to draw
length: 0, // The length of each line
width: 15, // The line thickness
radius: 42, // The radius of the inner circle
scale: 0.85, // Scales overall size of the spinner
corners: 1, // Corner roundness (0..1)
color: '#41d62b', // CSS color or array of colors
fadeColor: 'transparent', // CSS color or array of colors
opacity: 0.05, // Opacity of the lines
rotate: 0, // The rotation offset
direction: 1, // 1: clockwise, -1: counterclockwise
speed: 1, // Rounds per second
trail: 74, // Afterglow percentage
fps: 20, // Frames per second when using setTimeout() as a fallback in IE 9
zIndex: 2e9, // The z-index (defaults to 2000000000)
className: 'spinner', // The CSS class to assign to the spinner
top: '50%', // Top position relative to parent
left: '50%', // Left position relative to parent
shadow: 0, // Box-shadow for the lines
position: 'absolute' // Element positioning
};
var target = document.getElementById('spinnerContainer');
var spinner = new Spinner(opts).spin(target);
</script>
The script to load in spin.js is finding the file correctly, but then I get the error Uncaught SyntaxError: Unexpected token export referencing the line export { Spinner }; from spin.js
I also get an error saying Uncaught ReferenceError: Spinner is not defined which I assume is related to the error above but I'm not sure.
Perhaps all you want is to use a CDN version if you aren't set up to manage imports
var opts = {
lines: 20, // The number of lines to draw
length: 0, // The length of each line
width: 15, // The line thickness
radius: 42, // The radius of the inner circle
scale: 0.85, // Scales overall size of the spinner
corners: 1, // Corner roundness (0..1)
color: '#41d62b', // CSS color or array of colors
fadeColor: 'transparent', // CSS color or array of colors
opacity: 0.05, // Opacity of the lines
rotate: 0, // The rotation offset
direction: 1, // 1: clockwise, -1: counterclockwise
speed: 1, // Rounds per second
trail: 74, // Afterglow percentage
fps: 20, // Frames per second when using setTimeout() as a fallback in IE 9
zIndex: 2e9, // The z-index (defaults to 2000000000)
className: 'spinner', // The CSS class to assign to the spinner
top: '50%', // Top position relative to parent
left: '50%', // Left position relative to parent
shadow: 0, // Box-shadow for the lines
position: 'absolute' // Element positioning
};
var target = document.getElementById('spinnerContainer');
var spinner = new Spinner(opts).spin(target);
<script src="https://cdnjs.cloudflare.com/ajax/libs/spin.js/2.3.2/spin.js"></script>
<div id="spinnerContainer" class="spinner" style="width:100px;height:100px;background-color: Gray; color:black;">
</div>
I was getting same error open the CDN file from #charlietfl answer copy it and replace it with your spin.js file. It will work.
just found spin.js and it seems like a life saver.
The question is how to i insert the spinner into my div?
I have a follow button, that when clicked i remove the background image and currently replace with a loader.gif.
How can i do the same but with spin.js?
I knocked up a quick example of jsfiddle:
http://jsfiddle.net/4XpHp/
I would like the spinner to be inside the red square div.
<div id="foo"></div>
<button id="spin"> Spin! </button>
var opts = {
lines: 13, // The number of lines to draw
length: 17, // The length of each line
width: 8, // The line thickness
radius: 21, // The radius of the inner circle
corners: 1, // Corner roundness (0..1)
rotate: 58, // The rotation offset
direction: 1, // 1: clockwise, -1: counterclockwise
color: '#fff', // #rgb or #rrggbb or array of colors
speed: 0.9, // Rounds per second
trail: 100, // 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: '50%', // Top position relative to parent
left: '50%' // Left position relative to parent
};
$("#spin").click(function(){
var target = document.getElementById('foo');
var spinner = new Spinner(opts).spin(target);
});
#foo {
width: 50px;
height: 50px;
background-color: #f00;
}
You just need to set:
#foo {
position: relative; //Added this
width: 50px;
height: 50px;
background-color: #f00;
}
JsFiddle
This is actually a css issue really. By default the .spinner div is set to position: absolute (and you can't change that with css because it's an inline style), which means it's going to be positioned in the middle of the nearest positioned ancestor, which I'm assuming was the <body> tag (feel free to correct me here). By making #foo have a relative position, it becomes a positioned ancestor, and the spinner will sit inside it.
I updated your fiddle to get it to do what you wanted. I just had to adjust some of the configuration values to get the top and left positions correct, and then adjust the size of the spinner itself.
http://jsfiddle.net/4XpHp/2/
var opts = {
lines: 10, // The number of lines to draw
length: 7, // The length of each line
width: 2, // The line thickness
radius: 5, // The radius of the inner circle
corners: 1, // Corner roundness (0..1)
rotate: 58, // The rotation offset
direction: 1, // 1: clockwise, -1: counterclockwise
color: '#fff', // #rgb or #rrggbb or array of colors
speed: 0.9, // Rounds per second
trail: 100, // 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: '12%', // Top position relative to parent
left: '6.5%' // Left position relative to parent
};
i need help
Actually in my work i need to implement spin.js and block UI in a jqgrid. This is ok when i put the code in the same file and function, but i want to call a method from other JS file, and hear is where my problem. The spin.js and block UI start, then block UI stop with unblock but the spin.js still running.
Here is my code:
In BeforeRequest() in jqgrid i call one method
$.pui.common.loaderAnimationON("pane-content");
this method have this code
loaderAnimationON: function (div) {
var opts = {
lines: 13, // The number of lines to draw
length: 20, // The length of each line
width: 10, // The line thickness
radius: 30, // The radius of the inner circle
corners: 1, // Corner roundness (0..1)
rotate: 0, // The rotation offset
direction: 1, // 1: clockwise, -1: counterclockwise
color: '#000', // #rgb or #rrggbb or array of colors
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: '50%', // Top position relative to parent in px
left: '50%' // Left position relative to parent in px
};
var target = document.getElementById(div);
var spinner = new Spinner(opts).spin(target);
$.blockUI({
blockMsgClass: "gridProjectsLoader",
message: null
});
return spinner;
},
in gridComplete() i call other method
$.pui.common.loaderAnimationOFF("pane-content");
this method have this code
loaderAnimationOFF: function (div) {
var target = document.getElementById(div);
var spinner = $.pui.common.loaderAnimationON();
spinner.stop(target);
$.unblockUI();
}
Anyone can help me?
Thanks guys
You should use the same object to start and stop it. You can use global variables anywhere (just another .js)
Check this jsFiddle. It's stop spinner after 3 secs.
http://jsfiddle.net/YX7dy/8/
spinner=loaderAnimationON('Spin');
setInterval(function(){spinner.stop();},3000);
I am learning how to use Spin.js so that a loading indicator (the spinner) can be shown while the web page is loading.
I got it working but I am not sure whether I am calling the spin/stop in the proper page lifecycle. Is it possible to show the spinner before $(window).ready ?
<script type="text/javascript">
var spinner;
$(window).ready(function loadingAnimation() {
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 = $("body")[0];
spinner = new Spinner(opts).spin(target);
});
window.onload = function() {
spinner.stop();
};
For the working example, please see http://sgratrace.appspot.com/industry.html
I created an object to control the spinning:
Rats.UI.LoadAnimation = {
"start" : function(){
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 : $(window).height()/2.5, // Manual positioning in viewport
left : "auto"
};
var target = $("body")[0];
return new Spinner(opts).spin(target);
},
"stop" : function(spinner){
spinner.stop();
}
};
When the DOM is loaded, I start spinning:
$(document).ready(function(){
// Once the DOM is loaded, start spinning
spinner = Rats.UI.LoadAnimation.start();
pageUI();
});
When the entire page is loaded, I stop spinning:
$(window).load(function(){
// Once the page is fully loaded, stop spinning
Rats.UI.LoadAnimation.stop(spinner);
});
What's the difference between window.onload vs $(document).ready()
See the full code on my github repo:
https://github.com/seahrh/sgratrace/blob/master/war/js/rats.js
https://github.com/seahrh/sgratrace/blob/master/war/index.html
we have an ajax script that gets a word and loads some related data into a div tag.
it works fine but we want to add preloader (without jQuery) to div tag, we tried spin.js but it works only for whole page, this is what we did: (Summarized)
<head>
<script src="spin.min.js"></script>
</head>
<body>
<div id="data">
//data loads here
</div>
<script type="text/javascript" charset="utf-8">
var opts = {
lines: 13, // The number of lines to draw
length: 7, // The length of each line
width: 4, // The line thickness
radius: 28, // The radius of the inner circle
rotate: 0, // The rotation offset
color: '#000', // #rgb or #rrggbb
speed: 1, // Rounds per second
trail: 92, // 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('data');
var spinner = new Spinner(opts).spin(target);
</script>
</body>
what is wrong?
and any other (and better) solutions?
Your data div has full width and spin.js will place the image at the center of the element both vertically and horizontally thats why you are seeing this on a full screen based.
For e.g.
<head>
<script src="spin.js"></script>
</head>
<body>
<div id="data" style="width: 100px; height: 100px; float: left;">
<!-- Data Loads Here-->
</div>
<div id="NoData" style="width: 100px; height: 100px; float: left;">
Hello Hi Bye
</div>
<script type="text/javascript" charset="utf-8">
var opts = {
lines : 13, // The number of lines to draw
length : 7, // The length of each line
width : 4, // The line thickness
radius : 28, // The radius of the inner circle
rotate : 0, // The rotation offset
color : '#000', // #rgb or #rrggbb
speed : 1, // Rounds per second
trail : 92, // 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('data');
var spinner = new Spinner(opts).spin(target);
</script>
</body>