質問

I'm attempting to use the spin.js library to display a loading animation. However, going off the spin.js developer example I can't seem to get it to work/ appear; this is apparent by the empty div tag when I open my html file in Firefox.

Here is my code (spinnertest.html):

<html>
<head>
    <script src="spin.min.js">
        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("spinner");
        var spinner = new Spinner(opts).spin(target);
    </script>
</head>
<body>
    <div id="spinner">
    </div>
</body>


Working code for those interested (requires jquery-1.9.0.min.js and spin.min.js in the same directory):

<html>
<head>
    <script type="text/javascript" src="spin.min.js"></script>
    <script type="text/javascript" src="jquery-1.9.0.min.js"></script>
    <script type="text/javascript">
        $(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: 'auto', // Top position relative to parent in px
              left: 'auto' // Left position relative to parent in px
            };
            var target = document.getElementById("spinner");
            var spinner = new Spinner(opts).spin(target);
        });
    </script>
</head>
<body>
    <div id="spinner">
    </div>
</body>

役に立ちましたか?

解決

I don't believe that code inside a script tag with a src attribute will execute. If you change your code to the following, does it work?

<script type="text/javascript" src="spin.min.js"></script>
<script type="text/javascript">
  $(document).ready(function () {
    var opts = ...
    ...
    // all the code that you currently have between <script> and
    // </script> goes here
  });
</script>

他のヒント

You have to initialize it in a DOM ready state, otherwise target will be null. You should also have the include script in a separate script tag.

Using jquery...

$(function() {
  var target = document.getElementById("spinner");
  var spinner = new Spinner(opts).spin(target);
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top