刚刚找到 spin.js,它看起来像是救命稻草。
问题是如何将微调器插入到我的 div 中?
我有一个关注按钮,当我点击它时,我删除了背景图片并当前替换为 loader.gif。
除了使用 spin.js,我如何做同样的事情?
我敲了一个 jsfiddle 的简单例子:
我希望微调器位于红色方形 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;
}
这实际上是一个 css 问题。默认情况下,.spinner div 设置为 position: absolute
(你不能用 css 改变它,因为它是内联样式),这意味着它将被定位在 nearest positioned ancestor 的中间。 ,我假设是 <body>
标签(请随时在这里纠正我)。通过制作 #foo
有一个相对位置,它成为一个定位的祖先,微调器将坐在它里面。
关于javascript - 将 spin.js 微调器插入到 div 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23843817/