-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo.html
318 lines (311 loc) · 10.8 KB
/
demo.html
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<!DOCTYPE HTML>
<html>
<head>
<title>jQuery Plugin Demos</title>
<link rel="stylesheet" href="demo.css">
<link rel="stylesheet" href="prettify.css">
<link rel="stylesheet" href="dialog/jquery-ui.css">
</head>
<body id='jquery-plugin'>
<div class="header">jQuery Plugin Demo</div>
<div class="main">
<div class='fn-plugin'>
<ol>
<li>
<h1>this关键字</h1>
<pre class='prettyprint v-hidden'>
(function($){
$.fn.pinkify = function() {
// this引用的是jQuery本身
return this.each(function() {
// 在循环体内,this关键字引用的是DOM元素
$(this).css({
'background-color': '#fe57a1',
'color': '#fff',
'text-shadow': 'none'
});
});
}
})(jQuery)</pre>
<button class='seeCode'>查看代码</button><button id='thiskeyword'>测试</button>
</li>
<li>
<h1>确保$指向jQuery</h1>
<pre class='prettyprint v-hidden'>
(function($, window, document, undefined){
$.fn.pinkify = function() {}
})(jQuery, window, document)</pre>
<button class='seeCode'>查看代码</button>
</li>
<li>
<h1>链式调用</h1>
<pre class="prettyprint">$element.pinkify().css({
'font-weight':'bold',
'text-align':'center'
})</pre>
<button id='chainCall'>测试</button><p class='chainCallText'>链式调用</p>
</li>
<li>
<h1>插件参数设置</h1>
<pre class="prettyprint v-hidden">
;(function($){
$.fn.bestPluginEver = function(options) {
var settings = $.extend({
'color': '#fff',
'background-color': '#fe57a1'
}, options);
return this.css(settings);
}
})(jQuery)</pre>
<button class='seeCode'>查看代码</button>
</li>
<li>
<h1>保持私有函数的私有性</h1>
<pre class="prettyprint v-hidden">
(function($, windows, document, undefined){
$.fn.dataCruncher = function(options) {
//插件之外不可访问
function privateSort( data ) {
// 私有方法
}
return this.each(function() {
var data = privateSort( data );
});
}
})(jQuery, windows, document)</pre>
<button class='seeCode'>查看代码</button>
</li>
<li>
<h1>jQuery对象扩展DEMO</h1>
<pre class="prettyprint v-hidden">
;(function($, window, document, undefined) {
$.fn.highlight = function(options) {
debug(this);
var opts = $.extend({}, $.fn.highlight.defaults, options);
return this.each(function() {
$this = $(this);
var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
$this.css({
backgroundColor: o.background,
color: o.foreground
});
var markup = $this.html();
markup = $.fn.highlight.format(markup);
$this.html(markup);
});
};
// 私有函数:debug
function debug($obj) {
if (window.console && window.console.log)
window.console.log('highlight selection count: ' + $obj.size());
};
// 定义暴露format函数
$.fn.highlight.format = function(txt) {
return '<strong>' + txt + '</strong>';
};
// 插件的defaults
$.fn.highlight.defaults = {
foreground: 'red',
background: 'yellow'
};
})(jQuery, window, document)</pre>
<div class="fntest">
<button class='seeCode'>查看代码</button><button class="finalTest">测试(Default)</button><button class="finalTest2">测试</button>
<p>test text test text test text test text test text test text test text test text test text test text</p>
<p>测试文字测试文字测试文字测试文字测试文字</p>
</div>
</li>
</ol>
</div>
<div class="widget-plugin">
<ol>
<li>
<h1>Widget插件开发DEMO</h1>
<pre class="prettyprint v-hidden">
;(function($, window, document, undefined){
$.widget("zjdgx.pluginName", {
options: {
hashtag: "#hotpink"
},
_create: function() {
// 创建
},
_destroy: function() {
// 销毁
},
_setOption: function (key, value) {
// 在jQuery UI 1.8中, 必须从基widget中手动调用_setOption方法
$.Widget.prototype._setOption.apply(this, arguments);
// 在jQuery UI 1.9及更高版本中, 使用_super代替
this._super("_setOption", key, value);
},
pluginMethod: function() {
// 插件功能方法
}
});
})(jQuery, window, document)</pre>
<button class="seeCode">查看代码</button>
</li>
<li>
<h1>简单的有状态的插件</h1>
<pre class="prettyprint v-hidden">
$.widget('zjdgx.progressbar', {
options: {
showArrow: true
},
// 重写_create方法
_create: function() {
var progress = this.options.value + '%',
progressText = '<div class="curRate showArrow" style="width: '+this.options.value+'%;">'
+ progress + '</div><i class="curPosition"></i>';
if (!this.options.showArrow) {
progressText = '<div class="curRate" style="width: '+this.options.value+'%;"></div><div class="barText">'+progress+'</div>';
}
this.element.addClass('progressbar').append(progressText);
this.setArrowPosition();
},
setArrowPosition: function() {
if (this.options.showArrow) {
this.element.find('.curPosition').css('left', this.element.find('.curRate').width()-4);
}
},
widgetEventPrefix: 'zjdgx',
value: function (value) {
if (value) {
if (value > 100) {
value = 10;
}
if (value < 0) {
value = 0;
}
this.options.value = value;
if (this.options.showArrow) {
this.element.find('.curRate').css('width', value+'%').html(value+'%');
} else {
this.element.find('.curRate').css('width', value+'%').next().html(value+'%');
}
this.setArrowPosition();
// 提供回调功能让用户进行扩展
if (this.options.value == 100) {
this._trigger('complete', null, {value: 100});
}
//**事件类型则是由插件名称和回调名称合并而成(progressbarcomplete)。回调和事件被触发时会收到同样的两个参数:事件对象和相关数据
} else {
return this.options.value;
}
},
// 当参数被修改时执行一些操作
_setOption: function (key, value) {
this.options[key] = value;
if (key === 'value')
this.value(value);
},
destroy: function() {
this.element
.removeClass("progressbar progressbar-with-arrow progressbar-with-no-arrow")
.removeAttr("style")
.text("");
// call the base destroy function
$.Widget.prototype.destroy.call(this);
},
complete: function (options) {
this.element.html(options.str).css(options.style);
}
});</pre>
<button class="seeCode">查看代码</button><button id='progressbarTest'>测试</button><button id='progressbarWithArrow'>测试(带箭头)</button><button id="_destroy">销毁</button>
<div class="progressbarTest"></div>
<div class="progressbarTest"></div>
</li>
<li>
<h1>widget插件的继承</h1>
<pre class="prettyprint v-hidden">
$.widget('zjdgx.dialog', $.ui.dialog, {
red: function() {
var e = this.element;
if ( e.css('color') === '#f00' || e.css('color') === 'rgb(255, 0, 0)' ) {
this.blue();
} else {
e.css('color', '#f00');
}
return this;
},
blue: function() {
this.element.css('color', '#0f0');
return this;
},
close: function() {
console.log('zjdgx-dialog closed.....');
this._super('close');
}
});</pre>
<button class="seeCode">查看代码</button><button class="widgetExtend">测试</button>
<p class='widgetExtendText'>widget extension test character.</p>
</li>
<li>
<h1>widget _supper _supperApply</h1>
<pre class="prettyprint v-hidden">
$.widget('zjdgx.suppercompute', {
compute: function(a, b) {
this.element.html(a+b);
console.log('suppercompute: '+ (a+b));
}
});
$.widget('zjdgx.subcompute', $.zjdgx.suppercompute, {
compute: function(a, b, c) {
console.log('subcompute called suppercompute...');
this._supper(a, b);
this.element.html(a+b+c);
console.log('subcompute: '+ (a+b+c));
}
});</pre>
<button class="seeCode">查看代码</button><button id='supper'>super测试</button><button id='supperSub'>sub测试</button>
<div id='_supperResult'><p>supper result</p><p>sub result</p></div>
</li>
<li>
<h1>textboxdecorator(修改textbox样式)</h1>
<pre class="prettyprint v-hidden">
(function($, windows, document, undefined) {
$.widget('ui.textboxcecorator', {
options: {},
_init: function(){
//验证是否是需要装饰的元素
//this.element也就是调用此widget的元素
var e = this.element,
tg = e.attr("tagName").toLowerCase(),
ty = e.attr("type").toLowerCase();
if (!(tg === "input" || tg === "textarea")) {
return;
}
if (!(ty === "text" || ty === "password")) {
if (tg === "input") return;
}
e.addClass("ui-widget ui-state-default ui-corner-all");
//添加hover效果和active效果
e.mouseover(function(){
e.addClass("ui-state-hover");
}).mouseout(function(){
e.removeClass("ui-state-hover");
}).mousedown(function(){
e.addClass("ui-state-active");
}).mouseup(function(){
e.removeClass("ui-state-active");
});
},
//销毁时,移除widget增加的样式
destroy:function(){
this.element.removeClass("ui-widget ui-state-default ui-corner-all ui-state-hover ui-state-active");
}
});
})(jQuery, windows, document)</pre>
<div class="test"><button class='seeCode'>查看代码</button><input id='widgetTextbox' type="text"></div>
</li>
</ol>
</div>
</div>
<script type='text/javascript' src='jquery-1.8.1.min.js'></script>
<script type='text/javascript' src='dialog/jquery-ui.min.js'></script>
<script type='text/javascript' src='jquery-plugin-demo.js'></script>
<script type='text/javascript' src='run_prettify.js'></script>
<script type='text/javascript' src='demoEvents.js'></script>
</body>
</html>