刚刚找到了spin.js,它似乎是一个拯救生命的人。
问题是如何将旋转器插入到我的div中?
我有一个“跟随”按钮,当单击该按钮时,我会删除背景图像,并将其替换为loader.gif。
除了spin.js,我还能做同样的事情吗?
我想出了一个快活的例子:
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;
}发布于 2014-05-24 10:35:05
你只需要设置:
#foo {
position: relative; //Added this
width: 50px;
height: 50px;
background-color: #f00;
}JsFiddle
这实际上是一个css问题。默认情况下,.spinner div被设置为position: absolute (您不能用css更改它,因为它是内联样式),这意味着它将位于https://developer.mozilla.org/en-US/docs/Web/CSS/position#Absolute_positioning的中间,我假设它是<body>标记(请在这里纠正我)。通过使#foo具有相对位置,它将成为一个定位祖先,旋转器将位于其中。
发布于 2014-05-24 10:43:15
我更新了你的小提琴让它做你想做的事。我只需调整一些配置值,以使顶部和左侧位置正确,然后调整旋转器本身的大小。
http://jsfiddle.net/4XpHp/2/
var opts = {
lines: 10, // The number of lines to draw
length: 7, // The length of each line
width: 2, // The line thickness
radius: 5, // 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: '12%', // Top position relative to parent
left: '6.5%' // Left position relative to parent
};https://stackoverflow.com/questions/23843817
复制相似问题