-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.circle.js
125 lines (103 loc) · 4.47 KB
/
jquery.circle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Project: circle.js
* Description: A library for focusing page elements.
* Author: Greasidis Thodoris, Nona Maria
* License: MIT
*/
// the semi-colon before function invocation is a safety net against concatenated
// scripts and/or other plugins which may not be closed properly.
;(function ( $, window, document, undefined ) {
// undefined is used here as the undefined global variable in ECMAScript 3 is
// mutable (ie. it can be changed by someone else). undefined isn't really being
// passed in so we can ensure the value of it is truly undefined. In ES5, undefined
// can no longer be modified.
// window and document are passed through as local variable rather than global
// as this (slightly) quickens the resolution process and can be more efficiently
// minified (especially when both are regularly referenced in your plugin).
// Create the defaults once
var pluginName = "circle",
defaults = {
animate: false,
animationMode: 'transition' // 'animation'
};
// The actual plugin constructor
function Plugin( element, options ) {
this.element = element;
// jQuery has an extend method which merges the contents of two or
// more objects, storing the result in the first object. The first object
// is generally empty as we don't want to alter the default options for
// future instances of the plugin
this.options = $.extend( {}, defaults, options );
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype = {
init: function() {
// Place initialization logic here
// You already have access to the DOM element and
// the options via the instance, e.g. this.element
// and this.options
// you can add more functions like the one below and
// call them like so: this.yourOtherFunction(this.element, this.options).
var $target = $(this.element);
var targetOffset = $target.offset();
var $circle = $('.CircleJS');
if (!$circle.length) {
var cssClass = "CircleJS";
if (this.options.animate) {
if (this.options.animationMode === 'animation') {
cssClass += " CircleJSAnimated";
}
}
$circle = $('<div class="' + cssClass + '"></div>');
$circle.appendTo('body');
}
var h = $target.outerHeight();
var w = $target.outerWidth();
var diag = Math.sqrt(h*h + w*w);
// assuming that all border widths are equal
var border = parseFloat($circle.css('border-top-width'));
var paddingTop = parseFloat($circle.css('padding-top'));
var paddingLeft = parseFloat($circle.css('padding-left'));
$circle.css({
'top': Math.ceil(targetOffset.top + h/2 - diag/2 - border - paddingTop),
'left': Math.ceil(targetOffset.left + w/2 - diag/2 - border - paddingLeft),
'height': Math.ceil(diag),
'width': Math.ceil(diag)
});
if (this.options.animate && this.options.animationMode === 'transition') {
var initialScale = 5;
$circle.css({
'transform': 'scale(' + initialScale + ')',
});
setTimeout(function(){
$circle.addClass('CircleJSTransitioned');
$circle.css({
'transform': 'scale(1)',
'opacity': 1
});
}, 0);
}
},
destroy: function(el, options) {
var $circle = $('.CircleJS');
if ($circle && $circle.length) {
$circle.remove();
}
}
};
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function ( options ) {
return this.each(function () {
var instance = $.data(this, "plugin_" + pluginName);
if (!instance) {
instance = new Plugin( this, options );
$.data(this, "plugin_" + pluginName, instance);
} else if (options === 'destroy') {
instance.destroy();
}
});
};
})( jQuery, window, document );