jQuery UI ??? ?? ???
/ 部件庫(Widget Factory)
部件庫(Widget Factory)
使用與所有 jQuery UI 小部件相同的抽象化來創(chuàng)建有狀態(tài)的 jQuery 插件。
如需了解更多有關(guān)部件庫(Widget Factory)的細(xì)節(jié),請查看 API 文檔 部件庫(Widget Factory)。
默認(rèn)功能
該演示展示了一個簡單的使用部件庫(jquery.ui.widget.js)創(chuàng)建的自定義的小部件。
三個區(qū)塊是以不同的方式初始化的。點(diǎn)擊改變他們的背景顏色。查看源碼及注釋理解工作原理。
實(shí)例
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 部件庫(Widget Factory) - 默認(rèn)功能</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.9.1.js"></script> <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css"> <style> .custom-colorize { font-size: 20px; position: relative; width: 75px; height: 75px; } .custom-colorize-changer { font-size: 10px; position: absolute; right: 0; bottom: 0; } </style> <script> $(function() { // 部件定義,其中 "custom" 是命名空間,"colorize" 是部件名稱 $.widget( "custom.colorize", { // 默認(rèn)選項(xiàng) options: { red: 255, green: 0, blue: 0, // 回調(diào) change: null, random: null }, // 構(gòu)造函數(shù) _create: function() { this.element // 添加一個主題化的 class .addClass( "custom-colorize" ) // 防止雙擊來選擇文本 .disableSelection(); this.changer = $( "<button>", { text: "改變", "class": "custom-colorize-changer" }) .appendTo( this.element ) .button(); // 綁定 changer 按鈕上的 click 事件到 random 方法 this._on( this.changer, { // 當(dāng)小部件被禁用時,_on 不會調(diào)用 random click: "random" }); this._refresh(); }, // 當(dāng)創(chuàng)建及之后改變選項(xiàng)時調(diào)用 _refresh: function() { this.element.css( "background-color", "rgb(" + this.options.red +"," + this.options.green + "," + this.options.blue + ")" ); // 觸發(fā)一個回調(diào)/事件 this._trigger( "change" ); }, // 一個改變顏色為隨機(jī)值的公共方法 // 可通過 .colorize( "random" ) 直接調(diào)用 random: function( event ) { var colors = { red: Math.floor( Math.random() * 256 ), green: Math.floor( Math.random() * 256 ), blue: Math.floor( Math.random() * 256 ) }; // 觸發(fā)一個事件,檢查是否已取消 if ( this._trigger( "random", event, colors ) !== false ) { this.option( colors ); } }, // 自動移除通過 _on 綁定的事件 // 在這里重置其他的修改 _destroy: function() { // 移除生成的元素 this.changer.remove(); this.element .removeClass( "custom-colorize" ) .enableSelection() .css( "background-color", "transparent" ); }, // _setOptions 是通過一個帶有所有改變的選項(xiàng)的哈希來調(diào)用的 // 當(dāng)改變選項(xiàng)時總是刷新 _setOptions: function() { // _super 和 _superApply this._superApply( arguments ); this._refresh(); }, // _setOption 是為每個獨(dú)立的改變的選項(xiàng)調(diào)用的 _setOption: function( key, value ) { // 防止無效的顏色值 if ( /red|green|blue/.test(key) && (value < 0 || value > 255) ) { return; } this._super( key, value ); } }); // 通過默認(rèn)選項(xiàng)進(jìn)行初始化 $( "#my-widget1" ).colorize(); // 通過兩個自定義的選項(xiàng)進(jìn)行初始化 $( "#my-widget2" ).colorize({ red: 60, blue: 60 }); // 通過自定義的 green 值和一個只允許顏色足夠綠的隨機(jī)的回調(diào)進(jìn)行初始化 $( "#my-widget3" ).colorize( { green: 128, random: function( event, ui ) { return ui.green > 128; } }); // 點(diǎn)擊切換 enabled/disabled $( "#disable" ).click(function() { // 為每個小部件使用自定義的選擇器來找到所有的實(shí)例 // 所有的實(shí)例一起切換,所以我們可以從第一個開始檢查狀態(tài) if ( $( ":custom-colorize" ).colorize( "option", "disabled" ) ) { $( ":custom-colorize" ).colorize( "enable" ); } else { $( ":custom-colorize" ).colorize( "disable" ); } }); // 在初始化后,點(diǎn)擊設(shè)置選項(xiàng) $( "#black" ).click( function() { $( ":custom-colorize" ).colorize( "option", { red: 0, green: 0, blue: 0 }); }); }); </script> </head> <body> <div> <div id="my-widget1">改變顏色</div> <div id="my-widget2">改變顏色</div> <div id="my-widget3">改變顏色</div> <button id="disable">切換 disabled 選項(xiàng)</button> <button id="black">改為黑色</button> </div> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例