【JavaScript】spin.js!非常好用,我很喜欢!

  1 /**
  2  * Copyright (c) 2011-2014 Felix Gnass
  3  * Licensed under the MIT license
  4  * http://spin.js.org/
  5  *
  6  * Example:
  7     var opts = {
  8       lines: 12             // The number of lines to draw
  9     , length: 7             // The length of each line
 10     , width: 5              // The line thickness
 11     , radius: 10            // The radius of the inner circle
 12     , scale: 1.0            // Scales overall size of the spinner
 13     , corners: 1            // Roundness (0..1)
 14     , color: ‘#000‘         // #rgb or #rrggbb
 15     , opacity: 1/4          // Opacity of the lines
 16     , rotate: 0             // Rotation offset
 17     , direction: 1          // 1: clockwise, -1: counterclockwise
 18     , speed: 1              // Rounds per second
 19     , trail: 100            // Afterglow percentage
 20     , fps: 20               // Frames per second when using setTimeout()
 21     , zIndex: 2e9           // Use a high z-index by default
 22     , className: ‘spinner‘  // CSS class to assign to the element
 23     , top: ‘50%‘            // center vertically
 24     , left: ‘50%‘           // center horizontally
 25     , shadow: false         // Whether to render a shadow
 26     , hwaccel: false        // Whether to use hardware acceleration (might be buggy)
 27     , position: ‘absolute‘  // Element positioning
 28     }
 29     var target = document.getElementById(‘foo‘)
 30     var spinner = new Spinner(opts).spin(target)
 31  */
 32 ;(function (root, factory) {
 33
 34   /* CommonJS */
 35   if (typeof module == ‘object‘ && module.exports) module.exports = factory()
 36
 37   /* AMD module */
 38   else if (typeof define == ‘function‘ && define.amd) define(factory)
 39
 40   /* Browser global */
 41   else root.Spinner = factory()
 42 }(this, function () {
 43   "use strict"
 44
 45   var prefixes = [‘webkit‘, ‘Moz‘, ‘ms‘, ‘O‘] /* Vendor prefixes */
 46     , animations = {} /* Animation rules keyed by their name */
 47     , useCssAnimations /* Whether to use CSS animations or setTimeout */
 48     , sheet /* A stylesheet to hold the @keyframe or VML rules. */
 49
 50   /**
 51    * Utility function to create elements. If no tag name is given,
 52    * a DIV is created. Optionally properties can be passed.
 53    */
 54   function createEl (tag, prop) {
 55     var el = document.createElement(tag || ‘div‘)
 56       , n
 57
 58     for (n in prop) el[n] = prop[n]
 59     return el
 60   }
 61
 62   /**
 63    * Appends children and returns the parent.
 64    */
 65   function ins (parent /* child1, child2, ...*/) {
 66     for (var i = 1, n = arguments.length; i < n; i++) {
 67       parent.appendChild(arguments[i])
 68     }
 69
 70     return parent
 71   }
 72
 73   /**
 74    * Creates an opacity keyframe animation rule and returns its name.
 75    * Since most mobile Webkits have timing issues with animation-delay,
 76    * we create separate rules for each line/segment.
 77    */
 78   function addAnimation (alpha, trail, i, lines) {
 79     var name = [‘opacity‘, trail, ~~(alpha * 100), i, lines].join(‘-‘)
 80       , start = 0.01 + i/lines * 100
 81       , z = Math.max(1 - (1-alpha) / trail * (100-start), alpha)
 82       , prefix = useCssAnimations.substring(0, useCssAnimations.indexOf(‘Animation‘)).toLowerCase()
 83       , pre = prefix && ‘-‘ + prefix + ‘-‘ || ‘‘
 84
 85     if (!animations[name]) {
 86       sheet.insertRule(
 87         ‘@‘ + pre + ‘keyframes ‘ + name + ‘{‘ +
 88         ‘0%{opacity:‘ + z + ‘}‘ +
 89         start + ‘%{opacity:‘ + alpha + ‘}‘ +
 90         (start+0.01) + ‘%{opacity:1}‘ +
 91         (start+trail) % 100 + ‘%{opacity:‘ + alpha + ‘}‘ +
 92         ‘100%{opacity:‘ + z + ‘}‘ +
 93         ‘}‘, sheet.cssRules.length)
 94
 95       animations[name] = 1
 96     }
 97
 98     return name
 99   }
100
101   /**
102    * Tries various vendor prefixes and returns the first supported property.
103    */
104   function vendor (el, prop) {
105     var s = el.style
106       , pp
107       , i
108
109     prop = prop.charAt(0).toUpperCase() + prop.slice(1)
110     if (s[prop] !== undefined) return prop
111     for (i = 0; i < prefixes.length; i++) {
112       pp = prefixes[i]+prop
113       if (s[pp] !== undefined) return pp
114     }
115   }
116
117   /**
118    * Sets multiple style properties at once.
119    */
120   function css (el, prop) {
121     for (var n in prop) {
122       el.style[vendor(el, n) || n] = prop[n]
123     }
124
125     return el
126   }
127
128   /**
129    * Fills in default values.
130    */
131   function merge (obj) {
132     for (var i = 1; i < arguments.length; i++) {
133       var def = arguments[i]
134       for (var n in def) {
135         if (obj[n] === undefined) obj[n] = def[n]
136       }
137     }
138     return obj
139   }
140
141   /**
142    * Returns the line color from the given string or array.
143    */
144   function getColor (color, idx) {
145     return typeof color == ‘string‘ ? color : color[idx % color.length]
146   }
147
148   // Built-in defaults
149
150   var defaults = {
151     lines: 12             // The number of lines to draw
152   , length: 7             // The length of each line
153   , width: 5              // The line thickness
154   , radius: 10            // The radius of the inner circle
155   , scale: 1.0            // Scales overall size of the spinner
156   , corners: 1            // Roundness (0..1)
157   , color: ‘#000‘         // #rgb or #rrggbb
158   , opacity: 1/4          // Opacity of the lines
159   , rotate: 0             // Rotation offset
160   , direction: 1          // 1: clockwise, -1: counterclockwise
161   , speed: 1              // Rounds per second
162   , trail: 100            // Afterglow percentage
163   , fps: 20               // Frames per second when using setTimeout()
164   , zIndex: 2e9           // Use a high z-index by default
165   , className: ‘spinner‘  // CSS class to assign to the element
166   , top: ‘50%‘            // center vertically
167   , left: ‘50%‘           // center horizontally
168   , shadow: false         // Whether to render a shadow
169   , hwaccel: false        // Whether to use hardware acceleration (might be buggy)
170   , position: ‘absolute‘  // Element positioning
171   }
172
173   /** The constructor */
174   function Spinner (o) {
175     this.opts = merge(o || {}, Spinner.defaults, defaults)
176   }
177
178   // Global defaults that override the built-ins:
179   Spinner.defaults = {}
180
181   merge(Spinner.prototype, {
182     /**
183      * Adds the spinner to the given target element. If this instance is already
184      * spinning, it is automatically removed from its previous target b calling
185      * stop() internally.
186      */
187     spin: function (target) {
188       this.stop()
189
190       var self = this
191         , o = self.opts
192         , el = self.el = createEl(null, {className: o.className})
193
194       css(el, {
195         position: o.position
196       , width: 0
197       , zIndex: o.zIndex
198       , left: o.left
199       , top: o.top
200       })
201
202       if (target) {
203         target.insertBefore(el, target.firstChild || null)
204       }
205
206       el.setAttribute(‘role‘, ‘progressbar‘)
207       self.lines(el, self.opts)
208
209       if (!useCssAnimations) {
210         // No CSS animation support, use setTimeout() instead
211         var i = 0
212           , start = (o.lines - 1) * (1 - o.direction) / 2
213           , alpha
214           , fps = o.fps
215           , f = fps / o.speed
216           , ostep = (1 - o.opacity) / (f * o.trail / 100)
217           , astep = f / o.lines
218
219         ;(function anim () {
220           i++
221           for (var j = 0; j < o.lines; j++) {
222             alpha = Math.max(1 - (i + (o.lines - j) * astep) % f * ostep, o.opacity)
223
224             self.opacity(el, j * o.direction + start, alpha, o)
225           }
226           self.timeout = self.el && setTimeout(anim, ~~(1000 / fps))
227         })()
228       }
229       return self
230     }
231
232     /**
233      * Stops and removes the Spinner.
234      */
235   , stop: function () {
236       var el = this.el
237       if (el) {
238         clearTimeout(this.timeout)
239         if (el.parentNode) el.parentNode.removeChild(el)
240         this.el = undefined
241       }
242       return this
243     }
244
245     /**
246      * Internal method that draws the individual lines. Will be overwritten
247      * in VML fallback mode below.
248      */
249   , lines: function (el, o) {
250       var i = 0
251         , start = (o.lines - 1) * (1 - o.direction) / 2
252         , seg
253
254       function fill (color, shadow) {
255         return css(createEl(), {
256           position: ‘absolute‘
257         , width: o.scale * (o.length + o.width) + ‘px‘
258         , height: o.scale * o.width + ‘px‘
259         , background: color
260         , boxShadow: shadow
261         , transformOrigin: ‘left‘
262         , transform: ‘rotate(‘ + ~~(360/o.lines*i + o.rotate) + ‘deg) translate(‘ + o.scale*o.radius + ‘px‘ + ‘,0)‘
263         , borderRadius: (o.corners * o.scale * o.width >> 1) + ‘px‘
264         })
265       }
266
267       for (; i < o.lines; i++) {
268         seg = css(createEl(), {
269           position: ‘absolute‘
270         , top: 1 + ~(o.scale * o.width / 2) + ‘px‘
271         , transform: o.hwaccel ? ‘translate3d(0,0,0)‘ : ‘‘
272         , opacity: o.opacity
273         , animation: useCssAnimations && addAnimation(o.opacity, o.trail, start + i * o.direction, o.lines) + ‘ ‘ + 1 / o.speed + ‘s linear infinite‘
274         })
275
276         if (o.shadow) ins(seg, css(fill(‘#000‘, ‘0 0 4px #000‘), {top: ‘2px‘}))
277         ins(el, ins(seg, fill(getColor(o.color, i), ‘0 0 1px rgba(0,0,0,.1)‘)))
278       }
279       return el
280     }
281
282     /**
283      * Internal method that adjusts the opacity of a single line.
284      * Will be overwritten in VML fallback mode below.
285      */
286   , opacity: function (el, i, val) {
287       if (i < el.childNodes.length) el.childNodes[i].style.opacity = val
288     }
289
290   })
291
292
293   function initVML () {
294
295     /* Utility function to create a VML tag */
296     function vml (tag, attr) {
297       return createEl(‘<‘ + tag + ‘ xmlns="urn:schemas-microsoft.com:vml" class="spin-vml">‘, attr)
298     }
299
300     // No CSS transforms but VML support, add a CSS rule for VML elements:
301     sheet.addRule(‘.spin-vml‘, ‘behavior:url(#default#VML)‘)
302
303     Spinner.prototype.lines = function (el, o) {
304       var r = o.scale * (o.length + o.width)
305         , s = o.scale * 2 * r
306
307       function grp () {
308         return css(
309           vml(‘group‘, {
310             coordsize: s + ‘ ‘ + s
311           , coordorigin: -r + ‘ ‘ + -r
312           })
313         , { width: s, height: s }
314         )
315       }
316
317       var margin = -(o.width + o.length) * o.scale * 2 + ‘px‘
318         , g = css(grp(), {position: ‘absolute‘, top: margin, left: margin})
319         , i
320
321       function seg (i, dx, filter) {
322         ins(
323           g
324         , ins(
325             css(grp(), {rotation: 360 / o.lines * i + ‘deg‘, left: ~~dx})
326           , ins(
327               css(
328                 vml(‘roundrect‘, {arcsize: o.corners})
329               , { width: r
330                 , height: o.scale * o.width
331                 , left: o.scale * o.radius
332                 , top: -o.scale * o.width >> 1
333                 , filter: filter
334                 }
335               )
336             , vml(‘fill‘, {color: getColor(o.color, i), opacity: o.opacity})
337             , vml(‘stroke‘, {opacity: 0}) // transparent stroke to fix color bleeding upon opacity change
338             )
339           )
340         )
341       }
342
343       if (o.shadow)
344         for (i = 1; i <= o.lines; i++) {
345           seg(i, -2, ‘progid:DXImageTransform.Microsoft.Blur(pixelradius=2,makeshadow=1,shadowopacity=.3)‘)
346         }
347
348       for (i = 1; i <= o.lines; i++) seg(i)
349       return ins(el, g)
350     }
351
352     Spinner.prototype.opacity = function (el, i, val, o) {
353       var c = el.firstChild
354       o = o.shadow && o.lines || 0
355       if (c && i + o < c.childNodes.length) {
356         c = c.childNodes[i + o]; c = c && c.firstChild; c = c && c.firstChild
357         if (c) c.opacity = val
358       }
359     }
360   }
361
362   if (typeof document !== ‘undefined‘) {
363     sheet = (function () {
364       var el = createEl(‘style‘, {type : ‘text/css‘})
365       ins(document.getElementsByTagName(‘head‘)[0], el)
366       return el.sheet || el.styleSheet
367     }())
368
369     var probe = css(createEl(‘group‘), {behavior: ‘url(#default#VML)‘})
370
371     if (!vendor(probe, ‘transform‘) && probe.adj) initVML()
372     else useCssAnimations = vendor(probe, ‘animation‘)
373   }
374
375   return Spinner
376
377 }));
时间: 2024-09-29 21:03:19

【JavaScript】spin.js!非常好用,我很喜欢!的相关文章

【javascript】js 获取 url 后的参数值

以前写过一篇类似的博文(提取 url 的搜索字符串中的参数),但是个人觉得使用起来不是很方便,今天抽空重新写了个函数,该函数代码更加简洁. //获取 url 后的参数值 function getUrl(para){ var paraArr = location.search.substring(1).split('&'); var paraObj = {}; for(var i = 0;k = paraArr[i];i++){ paraObj[k.substring(0,k.indexOf('=

【JavaScript】——JS入门

结束XML之旅,开始JavaScript的学习,看视频,了解了她的前世今生,还是为她捏了把汗啊!看了部分视 频了,简单的总结一下吧! JavaScript是什么? JavaScript是一种基于面向对象和事件驱动,并具有相对安全性的客户端脚本语言. 这是JavaScript的定义,有没有看出很熟悉的概念? 首先是面向对象和事件驱动,这是从VB 6.0那看到的概念:Visual Basic是一种由 Microsoft 公司开发的 结构化的.模块化的.面向对象的.包含协助开发环境的事件驱动为机制的可

【JavaScript】JS跨域设置和取Cookie

cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用 JavaScript 来创建和取回 cookie 的值.本文主要JS怎样读取Cookie以及域的设置. AD: 在Javascript脚本里,一个cookie 实际就是一个字符串属性.当你读取cookie的值时,就得到一个字符串,里面当前WEB页使用的所有cookies的名称和值.每个cookie除了 name名称和value值这两个属性以外,还有四个属性.这些属性是:

spin.js 在jsp的使用

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page contentType="text/html;charset=utf-8"%>  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"

第十二篇 JavaScript(简称JS) 实现显示与隐藏

JavaScript JavaScript简称JS.JS是脚本语言,它是一种轻量级的编程语言,是可以插入HTML页面的编程代码,几乎所有现代浏览器都是支持的. 理论老师不行,我就抄袭手册上的一些关键字段给大家,然后我们写代码来学习. JS也和CSS一样,是可以外部引用的,但是CSS用的是link标签,而JS用的则是script标签,和CSS一样,要写在head标签里哦,引用文件都要写在这里的. 我们来写一个看看: <head lang="en"> <script sr

(转载)重新介绍 JavaScript(JS 教程)

引言 为什么会有这一篇“重新介绍”呢?因为 JavaScript 堪称世界上被人误解最深的编程语言.虽然常被视作“玩具语言”,但它看似简洁外衣下,还隐藏着强大的语言特性. JavaScript 目前广泛应用于一大批知名应用中,对于网页和移动开发者来说,深入理解 JavaScript 就尤有必要. 先从这门语言的历史谈起.1995 年 Netscape 一位名为 Brendan Eich 的员工创造了 JavaScript,随后在 1996 年初,JavaScript 首先被应用于 Netscap

[TypeScript] 建置输出单一JavaScript档案(.js)与Declaration档案(.d.ts)

[TypeScript] 建置输出单一JavaScript档案(.js)与Declaration档案(.d.ts) 问题情景 开发人员使用Visual Studio来开发TypeScript,可以很方便快速的将项目里的所有TypeScript档案(.ts),一口气全部编译成为JavaScript档案(.js),用以提供html网页使用.但是当软件项目越来越庞大的时候,过多的.js档引用,会增加开发.html档案时的负担;并且每个.js档之间的相依关系,也很容易因为引用顺序的错误,而造成不可预期的

IE (6-11)版本,在使用iframe的框架时,通过a标签javascript:; 和js跳转parent.location的时候 出现在新页面打开的情况

问题描述: 使用iframe的情况下,在子框架中,使用如下形式的跳转: <a href="javascript:;" onclick="parent.location.href='login.php';"> 退出</a> 在IE浏览器下,点击后,会在退出的情况下,再打开一个页面,URL显示为 javascript:;  的情况出现,也就是说 a标签的 javascript:; 并未生效! 这是一个很奇怪的现象,在谷歌等现代浏览器中并不存在该问

JavaScript(JS)简介

历史背景介绍 (Brendan Eich)在其Netscape Navigator 2.0产品中开发出一套livescript的脚本语言.Sun和Netscape共同完成.后改名叫Javascript. JavaScript的组成 ECMAScript :定义了js的语法标准: 包含变量 .表达式.运算符.函数.if语句 for循环 while循环.内置的函数,对象 (封装 继承 多态) 基于对象的语言.使用对象 文档对象模型(DOM) Document object model :操作网页上元