简体   繁体   English

将spin.js微调器插入div?

[英]insert the spin.js spinner into a div?

just found spin.js and it seems like a life saver. 刚刚找到spin.js,它似乎是一个救生员。

The question is how to i insert the spinner into my div? 问题是如何将微调器插入我的div?

I have a follow button, that when clicked i remove the background image and currently replace with a loader.gif. 我有一个跟随按钮,单击时我删除背景图像,目前替换为loader.gif。

How can i do the same but with spin.js? 我怎么能用spin.js做同样的事情?

I knocked up a quick example of jsfiddle: 我敲了一个jsfiddle的简单例子:

http://jsfiddle.net/4XpHp/ http://jsfiddle.net/4XpHp/

I would like the spinner to be inside the red square 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;
}

You just need to set: 你只需要设置:

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

JsFiddle 的jsfiddle

This is actually a css issue really. 这实际上是一个css问题。 By default the .spinner div is set to position: absolute (and you can't change that with css because it's an inline style), which means it's going to be positioned in the middle of the nearest positioned ancestor , which I'm assuming was the <body> tag (feel free to correct me here). 默认情况下,.spinner div设置为position: absolute (你不能用css改变它,因为它是一个内联样式),这意味着它将被定位在最近的定位祖先的中间,我假设是<body>标签(在这里随意纠正​​我)。 By making #foo have a relative position, it becomes a positioned ancestor , and the spinner will sit inside it. 通过使#foo具有相对位置,它成为一个定位的祖先 ,并且旋转器将位于其内部。

I updated your fiddle to get it to do what you wanted. 我更新了你的小提琴,让它做你想做的事。 I just had to adjust some of the configuration values to get the top and left positions correct, and then adjust the size of the spinner itself. 我只需要调整一些配置值以使顶部和左侧位置正确,然后调整微调器本身的大小。

http://jsfiddle.net/4XpHp/2/ 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
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM