javascript - 将 spin.js 微调器插入到 div 中?

标签 javascript jquery css spin.js

刚刚找到 spin.js,它看起来像是救命稻草。

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

我有一个关注按钮,当我点击它时,我删除了背景图片并当前替换为 loader.gif。

除了使用 spin.js,我如何做同样的事情?

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

http://jsfiddle.net/4XpHp/

我希望微调器位于红色方形 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;
}

最佳答案

你只需要设置:

#foo {
   position: relative; //Added this
   width: 50px;
   height: 50px;
   background-color: #f00;
}

JsFiddle

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

关于javascript - 将 spin.js 微调器插入到 div 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23843817/

相关文章:

javascript - 从元素中获取 href 并添加到图像的 src 属性

css - 父元素为 'height: 0;',子元素不可见,但还在?

javascript - 克隆一个 div 但它的格式不像主 div

javascript - 如何在浏览器窗口中从 Javascript Windows 通用应用程序打开 URL?

javascript - 使用后退按钮后如何从上一页获取单选值?

css pie 仅适用于字段元素

javascript - 使用单选按钮而不是链接单击的 jquery 选项卡

javascript - Django Rest Framework 许多相关领域 - Javascript ajax - 整数列表的正确格式

javascript - 制作一个简单的图像切换器

javascript - 具有 MVC 模型的完整日历事件数组