刚刚发现 spin.js,它似乎是一个救星。
问题是如何将微调器插入到我的 div 中?
我有一个关注按钮,单击该按钮时,我会删除背景图像并当前替换为 loader.gif。
我怎样才能用 spin.js 做同样的事情?
我举了一个 jsfiddle 的简单例子:
http://jsfiddle.net/4XpHp/ 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 http://jsfiddle.net/gmvT4/1/
这实际上是一个CSS问题。默认情况下 .spinner div 设置为position: absolute
(你不能用 css 来改变它,因为它是内联样式),这意味着它将被定位在最近定位的祖先 https://developer.mozilla.org/en-US/docs/Web/CSS/position#Absolute_positioning,我假设是<body>
标签(请随时在这里纠正我)。通过制作#foo
有了相对位置,就变成了定位祖先,旋转器将位于其中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)