将spin.js微调器插入div?

Lov*_*ock 4 javascript css jquery spin.js

刚刚找到spin.js,它似乎是一个救生员.

问题是如何将微调器插入我的div?

我有一个跟随按钮,单击时我删除背景图像,目前替换为loader.gif.

我怎么能用spin.js做同样的事情?

我敲了一个jsfiddle的简单例子:

http://jsfiddle.net/4XpHp/

我想旋转器在红色方格内.

<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;
}
Run Code Online (Sandbox Code Playgroud)

Mig*_*ell 6

你只需要设置:

#foo {
   position: relative; //Added this
   width: 50px;
   height: 50px;
   background-color: #f00;
}
Run Code Online (Sandbox Code Playgroud)

的jsfiddle

这实际上是一个css问题.默认情况下,.spinner div被设置为position: absolute(并且你不能用css改变它,因为它是一个内联样式),这意味着它将被定位在最近的定位祖先的中间,我假设它是<body>标记(请随时在此纠正我).通过制作#foo相对位置,它成为一个定位的祖先,旋转器将坐在它里面.