-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.menu-animation-icon.js
188 lines (157 loc) · 4.36 KB
/
jquery.menu-animation-icon.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* jquery plugin menu animation icon
* @author Simon Zhao
*
*/
/*global define */
;(function(plugin){
//AMD support
if(typeof define === 'function' && define.amd)
{
define(['jquery'], plugin);
}
else
{
plugin(jQuery);
}
}(function($){
$.fn.menuAnimationIcon = function(options){
options = options ? options: {};
var defaults = {
animation: true,
bgColor:'#000000', // icon background
width:40, // icon width
height:40, // icon height
borderColor:'#000000', // icon border color
borderWidth:0, // icon border with
speed:400, // animation speed
menuColor:'#FFFFFF', // menu color
crossColor:'#CB3837', // close icon color
lineWidth:'20px', // width of delimiter line in menu
lineHeight:'3px', // height of delimiter line in menu
lineMargin:'7px', // margin of delimiter line in menu
cursor:'pointer', // cursor
menuEvent:function(){}, // click event
crossEvent:function(){} // close event
};
var opt = $.extend(defaults, options);
return this.each(function(){
var $this = $(this);
var status = status? 'cross':'menu';
var setWrapper = function(){
$this.css({
'cursor':opt.cursor,
'background':opt.bgColor,
'width':opt.width,
'height':opt.height,
'border-color':opt.borderColor,
'border-width':opt.borderWidth,
'position':'relative',
'display':'inline-block',
'border-style':'solid',
});
};
var lineHeight = parseInt(opt.lineHeight);
var lineMargin = parseInt(opt.lineMargin);
var secondMarginTop = -lineHeight/2;
var firstMarginTop = secondMarginTop - lineMargin;
var thirdMarginTop = secondMarginTop + lineMargin;
var createIcon = function(){
$('<div class="first-line"></div>').css({
'margin-left':'-10px',
'margin-top':firstMarginTop
}).appendTo($this);
$('<div class="second-line"></div>').css({
'margin-left':'-10px',
'margin-top':secondMarginTop
}).appendTo($this);
$('<div class="third-line"></div>').css({
'margin-left':'-10px',
'margin-top':thirdMarginTop
}).appendTo($this);
$('div',$this).css({
'position':'absolute',
'left':'50%',
'top':'50%',
'width':opt.lineWidth,
'height':opt.lineHeight,
'background':opt.menuColor
});
};
var setCrossColor = function(){
$('div',$this).css({'background':opt.crossColor});
};
var setMenuColor = function(){
$('div',$this).css({'background':opt.menuColor});
};
// menu transfrom to cross
var menuToCross = function(){
$('.first-line',$this).stop().animate(
{'margin-top':secondMarginTop},
{duration:opt.speed, complete:function(){
$(this).css({'transform':'rotate3d(0, 0, 1, -45deg)'});
setCrossColor();
opt.menuEvent();
}});
$('.third-line',$this).stop().animate(
{'margin-top':secondMarginTop},
{duration:opt.speed, complete:function(){
$(this).css({'transform':'rotate3d(0, 0, 1, 45deg)'});
}});
$('.second-line', $this).hide();
status = 'cross';
};
// cross transform to menu
var crossToMenu = function(){
$('.second-line', $this).show();
$('.first-line', $this).stop()
.css({'margin-top':secondMarginTop,'transform':'rotate3d(0, 0, 1, 0deg)'})
.animate(
{'margin-top':firstMarginTop},
{duration:opt.speed,complete:function(){
setMenuColor();
opt.crossEvent();
}});
$('.third-line', $this).stop()
.css({'margin-top':secondMarginTop,'transform':'rotate3d(0, 0, 1, 0deg)'})
.animate(
{'margin-top':thirdMarginTop},
{duration:opt.speed,complete:function(){
}});
status = 'menu';
};
var init = function(){
setWrapper();
createIcon();
};
init();
$this.on('click',function(){
if(opt.animation === true)
{
if(status === 'menu')
{
menuToCross();
}
else
{
crossToMenu();
}
}
else{
if(status === 'menu')
{
opt.menuEvent();
}
else
{
opt.crossEvent();
}
}
});
});
};
var $menuAnimationIcon = $.menuAnimationIcon = function(options){
$('.menuIcon').menuAnimationIcon(options);
};
return $menuAnimationIcon; // AMD requirement
}));