jQueryUI如何自定義組件實現(xiàn)代碼
更新時間:2010年11月14日 19:24:13 作者:
第一次自定義jQueryUI Widget 又是第一次,現(xiàn)在的感受是jQueryUI Widget能讓你代碼組織得更好,風(fēng)格更一致。
如何開始使用
首先用$.widget()方法開始定義你的組件,它只接收三個參數(shù):第一個是組件名稱,第二個是可選的基類組件(默認(rèn)的基類是$.Widget),第三個是組件的原型。
組件名稱必須包含命名空間,要注意的是,官方組件的命名空間是以‘ui'開頭的,比如:‘ui.tabs'。我在下面的用‘我'的拼音(‘wo')。
$.widget("yourNamespace.yourWidgetName",[yourBaseWidget],yourWidgetPrototype)
$.Widget基類含有一個重要的屬性‘options',它用來定義公有參數(shù),組件初始化時外部調(diào)用的參數(shù)會覆蓋內(nèi)部定義的參數(shù);以及三個重要的私有的方法‘_create'、‘_init'、‘',前兩個相當(dāng)于構(gòu)造函數(shù)的作用,按順序執(zhí)行,_create()方法執(zhí)行之后會觸發(fā)'create'事件。 _trigger()方法會將參數(shù)中的指定函數(shù)標(biāo)準(zhǔn)化為W3C事件,并且觸發(fā)這個自定義事件。
另外還有三個公有方法‘enable',‘disable',‘destroy',分別表示啟用、禁用和銷毀組件。
這里很有意思的,是私有方法和公有方法的實現(xiàn)。jQuerUI Widget暴露的方法都是不以‘_'開頭的:
// prevent calls to internal methods
if ( isMethodCall && options.charAt( 0 ) === "_" ) {
return returnValue;
}
實際上,jQueryUI Widget還是保留了原始的API,比如這樣使用:
var $div = $('.demo:first');
var api = $div.data('divZoom');
// console.dir(api);
api.zoomIn();
// 對比
$div.divZoom('zoomIn');
一個實現(xiàn)完全私有變量的小技巧:
(function($) {
var privateVar = '';
$.widget("wo.divZoom",{});
})(jQuery)
所有代碼
/*
* @by ambar
* @create 2010-10-20
* @update 2010-10-25
*/
(function($) {
var html = '<div class="icon-zoom">\
<span title="zoom in" class="zoom-in">zoom in</span>\
<span title="zoom out" class="zoom-out">zoom out</span>\
</div>';
$.widget("wo.divZoom",{
_init : function() {
var self = this, opt = self.options, tgt = opt.target, $el = self.element ;
// 初始一次
if($('div.icon-zoom',$el).length) return;
$el.append(html);
self.target = ( tgt == '' ? $el : $el.find(tgt) );
// 檢測初始值
var level = self.target.attr(opt.dataPrefix);
self.target.attr(opt.dataPrefix,level || opt.level[0]);
self.btnZoomIn = $el.find('span.zoom-in').click( $.proxy(self.zoomIn,self) );
self.btnZoomOut = $el.find('span.zoom-out').click( $.proxy(self.zoomOut,self) );
},
destroy : function(){
this.element.find('div.icon-zoom').remove();
},
options : {
level : [120,160,200],
target : '',
speed : 'normal',
dataPrefix : 'data-zoom-level',
zooming : null,
select : null,
show : null
},
currentLevel : function(){
var self = this, opt = self.options, lvl = Number(self.target.attr(opt.dataPrefix));
return $.inArray(lvl,opt.level);
},
zoomIn : function() {
this.zoom(this.currentLevel() + 1);
},
zoomOut : function() {
this.zoom(this.currentLevel() - 1);
},
zoom : function(i){
var self = this, opt = self.options,lvls = opt.level,$tgt = self.target;
if( i<=-1 || i>=lvls.length ) return;
var value = lvls[i],
originalValue = lvls[self.currentLevel()],
style = { width:value, height:value };
var data = { target : $tgt, css : style, originalCss : {width:originalValue,height:originalValue} };
var goon = self._trigger('start',null,data);
if(!goon) return;
$tgt.animate(style,
{
duration : opt.speed,
step : function(val) {
var css = { width:val, height:val };
self._trigger('zooming',null,$.extend({},data,{css:css}));
},
complete : function(){
$tgt.attr(opt.dataPrefix,value);
self._trigger('stop',null,data);
}
});
}
});
})(jQuery)
在頁面上調(diào)用
<script src="js/jquery-1.4.4.min.js"></script>
<script src="js/jquery.ui.widget.js"></script>
<!-- 自定義的 -->
<script src="js/jquery.ui.wo.divZoom.js"></script>
<script type="text/javascript">
$(function() {
$('div.demo').divZoom({
target : '>a',
level : [80,120,160,200],
zooming : function(e,ui){
// console.log(e,ui.css);
},
start : function(e,ui){
console.log('開始',this,e.type,ui);
},
stop : function(e,ui) {
console.log('結(jié)束',ui.css);
}
});
});
</script>
示例代碼:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>jQueryUI</title>
<style type="text/css">
#receptacle{width:800px;height:500px;background:#cde;position:relative;}
#receptacle .demo{width:80px;height:80px;position:absolute;}
.demo .cont{width:80px;height:80px;display:block;background:#07a;}
.demo-a{top:30px;left:122px;}
.demo-b{top:100px;left:400px;}
.icon-zoom {
position:absolute; width:63px; height:20px; overflow:hidden;
background:url(//img.jbzj.com/file_images/photoshop/201011/icon-zoom.png) no-repeat; cursor:pointer;
}
.icon-zoom span { width:20px; display:block; text-indent:-999em; float:left; }
</style>
<!--<script src="js/jquery.ui.core.js"></script>
<script src="jquery-1.4.4.min.js"></script>
<script src="jquery.ui.widget.js"></script>
<!-- 自定義的 -->
<script src="jquery.ui.wo.divZoom.js"></script>
<script type="text/javascript">
$(function() {
$('div.demo').divZoom({
target : '>a',
level : [80,120,160,200],
zooming : function(e,ui){
// console.log(e,ui.css);
},
start : function(e,ui){
console.log('開始',this,e.type,ui);
},
stop : function(e,ui) {
console.log('結(jié)束',ui.css);
}
});
});
</script>
</head>
<body>
<p>
<a >原文</a>
</p>
<div id="receptacle">
<div class="demo demo-a">
<a class="cont" href="#a">a</a>
</div>
<div class="demo demo-b">
<a class="cont" href="#b">b</a>
</div>
</div>
</body>
</html>
首先用$.widget()方法開始定義你的組件,它只接收三個參數(shù):第一個是組件名稱,第二個是可選的基類組件(默認(rèn)的基類是$.Widget),第三個是組件的原型。
組件名稱必須包含命名空間,要注意的是,官方組件的命名空間是以‘ui'開頭的,比如:‘ui.tabs'。我在下面的用‘我'的拼音(‘wo')。
復(fù)制代碼 代碼如下:
$.widget("yourNamespace.yourWidgetName",[yourBaseWidget],yourWidgetPrototype)
$.Widget基類含有一個重要的屬性‘options',它用來定義公有參數(shù),組件初始化時外部調(diào)用的參數(shù)會覆蓋內(nèi)部定義的參數(shù);以及三個重要的私有的方法‘_create'、‘_init'、‘',前兩個相當(dāng)于構(gòu)造函數(shù)的作用,按順序執(zhí)行,_create()方法執(zhí)行之后會觸發(fā)'create'事件。 _trigger()方法會將參數(shù)中的指定函數(shù)標(biāo)準(zhǔn)化為W3C事件,并且觸發(fā)這個自定義事件。
另外還有三個公有方法‘enable',‘disable',‘destroy',分別表示啟用、禁用和銷毀組件。
這里很有意思的,是私有方法和公有方法的實現(xiàn)。jQuerUI Widget暴露的方法都是不以‘_'開頭的:
復(fù)制代碼 代碼如下:
// prevent calls to internal methods
if ( isMethodCall && options.charAt( 0 ) === "_" ) {
return returnValue;
}
實際上,jQueryUI Widget還是保留了原始的API,比如這樣使用:
復(fù)制代碼 代碼如下:
var $div = $('.demo:first');
var api = $div.data('divZoom');
// console.dir(api);
api.zoomIn();
// 對比
$div.divZoom('zoomIn');
一個實現(xiàn)完全私有變量的小技巧:
復(fù)制代碼 代碼如下:
(function($) {
var privateVar = '';
$.widget("wo.divZoom",{});
})(jQuery)
所有代碼
復(fù)制代碼 代碼如下:
/*
* @by ambar
* @create 2010-10-20
* @update 2010-10-25
*/
(function($) {
var html = '<div class="icon-zoom">\
<span title="zoom in" class="zoom-in">zoom in</span>\
<span title="zoom out" class="zoom-out">zoom out</span>\
</div>';
$.widget("wo.divZoom",{
_init : function() {
var self = this, opt = self.options, tgt = opt.target, $el = self.element ;
// 初始一次
if($('div.icon-zoom',$el).length) return;
$el.append(html);
self.target = ( tgt == '' ? $el : $el.find(tgt) );
// 檢測初始值
var level = self.target.attr(opt.dataPrefix);
self.target.attr(opt.dataPrefix,level || opt.level[0]);
self.btnZoomIn = $el.find('span.zoom-in').click( $.proxy(self.zoomIn,self) );
self.btnZoomOut = $el.find('span.zoom-out').click( $.proxy(self.zoomOut,self) );
},
destroy : function(){
this.element.find('div.icon-zoom').remove();
},
options : {
level : [120,160,200],
target : '',
speed : 'normal',
dataPrefix : 'data-zoom-level',
zooming : null,
select : null,
show : null
},
currentLevel : function(){
var self = this, opt = self.options, lvl = Number(self.target.attr(opt.dataPrefix));
return $.inArray(lvl,opt.level);
},
zoomIn : function() {
this.zoom(this.currentLevel() + 1);
},
zoomOut : function() {
this.zoom(this.currentLevel() - 1);
},
zoom : function(i){
var self = this, opt = self.options,lvls = opt.level,$tgt = self.target;
if( i<=-1 || i>=lvls.length ) return;
var value = lvls[i],
originalValue = lvls[self.currentLevel()],
style = { width:value, height:value };
var data = { target : $tgt, css : style, originalCss : {width:originalValue,height:originalValue} };
var goon = self._trigger('start',null,data);
if(!goon) return;
$tgt.animate(style,
{
duration : opt.speed,
step : function(val) {
var css = { width:val, height:val };
self._trigger('zooming',null,$.extend({},data,{css:css}));
},
complete : function(){
$tgt.attr(opt.dataPrefix,value);
self._trigger('stop',null,data);
}
});
}
});
})(jQuery)
在頁面上調(diào)用
復(fù)制代碼 代碼如下:
<script src="js/jquery-1.4.4.min.js"></script>
<script src="js/jquery.ui.widget.js"></script>
<!-- 自定義的 -->
<script src="js/jquery.ui.wo.divZoom.js"></script>
<script type="text/javascript">
$(function() {
$('div.demo').divZoom({
target : '>a',
level : [80,120,160,200],
zooming : function(e,ui){
// console.log(e,ui.css);
},
start : function(e,ui){
console.log('開始',this,e.type,ui);
},
stop : function(e,ui) {
console.log('結(jié)束',ui.css);
}
});
});
</script>
示例代碼:
復(fù)制代碼 代碼如下:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>jQueryUI</title>
<style type="text/css">
#receptacle{width:800px;height:500px;background:#cde;position:relative;}
#receptacle .demo{width:80px;height:80px;position:absolute;}
.demo .cont{width:80px;height:80px;display:block;background:#07a;}
.demo-a{top:30px;left:122px;}
.demo-b{top:100px;left:400px;}
.icon-zoom {
position:absolute; width:63px; height:20px; overflow:hidden;
background:url(//img.jbzj.com/file_images/photoshop/201011/icon-zoom.png) no-repeat; cursor:pointer;
}
.icon-zoom span { width:20px; display:block; text-indent:-999em; float:left; }
</style>
<!--<script src="js/jquery.ui.core.js"></script>
<script src="jquery-1.4.4.min.js"></script>
<script src="jquery.ui.widget.js"></script>
<!-- 自定義的 -->
<script src="jquery.ui.wo.divZoom.js"></script>
<script type="text/javascript">
$(function() {
$('div.demo').divZoom({
target : '>a',
level : [80,120,160,200],
zooming : function(e,ui){
// console.log(e,ui.css);
},
start : function(e,ui){
console.log('開始',this,e.type,ui);
},
stop : function(e,ui) {
console.log('結(jié)束',ui.css);
}
});
});
</script>
</head>
<body>
<p>
<a >原文</a>
</p>
<div id="receptacle">
<div class="demo demo-a">
<a class="cont" href="#a">a</a>
</div>
<div class="demo demo-b">
<a class="cont" href="#b">b</a>
</div>
</div>
</body>
</html>
相關(guān)文章
精心挑選的12款優(yōu)秀的基于jQuery的手風(fēng)琴效果插件和教程
當(dāng)你想在有限的頁面空間內(nèi)展示多個內(nèi)容片段的時候,手風(fēng)琴(Accordion)效果就顯得非常有用,它可以幫助你以對用戶非常友好的方式實現(xiàn)多個內(nèi)容片段之間的切換。借助流行的 jQuery 框架,只需很少的代碼就可以實現(xiàn)精美的手風(fēng)琴效果,幫助你的網(wǎng)站吸引更多用戶的眼球2012-08-08基于jquery的使ListNav兼容中文首字拼音排序的實現(xiàn)代碼
jQuery的字母排序插件ListNav不支持中文,比較頭疼,最后找到一個取中文首字母的JS函數(shù),再配合ListNav,可以完善支持中文按首字母進(jìn)行排序。2011-07-07利用jQuery簡單實現(xiàn)產(chǎn)品展示圖片左右滾動功能(示例代碼)
本篇文章主要是對利用jQuery簡單實現(xiàn)產(chǎn)品展示圖片左右滾動功能的示例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01jquery關(guān)于頁面焦點的定位(文本框獲取焦點時改變樣式 )
用戶在輸入文字時,如果能高亮顯示正在輸入的那個文本框的話,會更人性化些,下面就使用jQuery來實現(xiàn)。2010-09-09