How jQueryUI customizes component implementation code_jquery
First start defining your component using the $.widget() method, which only receives three parameters: the first is the component name, and the second is the optional base class component (The default base class is $.Widget), and the third one is the prototype of the component.
The component name must include the namespace. It should be noted that the namespace of the official component starts with 'ui', such as: 'ui.tabs'. I use the pinyin of ‘我’ (‘wo’) below.
$.widget("yourNamespace.yourWidgetName",[yourBaseWidget ],yourWidgetPrototype)
$.Widget base class contains an important attribute 'options', which is used to define public parameters. When the component is initialized, the externally called parameters will override the internally defined parameters; and three There are three important private methods '_create', '_init', and ''. The first two are equivalent to the functions of constructors and are executed in order. The 'create' event will be triggered after the _create() method is executed. The _trigger() method will standardize the specified function in the parameter as a W3C event and trigger this custom event.
There are also three public methods 'enable', 'disable', and 'destroy', which respectively represent enabling, disabling and destroying components.
What is very interesting here is the implementation of private methods and public methods. The methods exposed by jQuerUI Widget do not start with '_':
// prevent calls to internal methods
if ( isMethodCall && options.charAt( 0 ) === "_" ) {
return returnValue;
}
In fact, jQueryUI Widget still retains the original API, for example, it can be used like this:
var $div = $('.demo:first');
var api = $div.data('divZoom');
// console.dir(api);
api .zoomIn();
// Comparison
$div.divZoom('zoomIn');
A little trick to implement completely private variables:
(function($) {
var privateVar = '';
$. widget("wo.divZoom",{});
})(jQuery)
All codes
/*
* @by ambar
* @create 2010-10-20
* @update 2010-10-25
*/
(function($) {
var html = '
zoom in
zoom out
$.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)
在页面上调用
示例代码:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Vue.js is a very popular open source JavaScript framework in the field of modern front-end development. It simplifies many problems in the front-end development process and makes it easier to develop complex applications through component development. One of the very useful features is to easily implement two-way data binding in components using the v-model directive. Although Vue.js provides many built-in input components, if you need a customized input component, you can satisfy it by implementing a v-model custom component.

UniApp is a cross-platform development framework based on Vue.js, which can simultaneously develop applications for multiple platforms such as mini programs, H5, and App. In UniApp, we can use custom components to achieve page reuse and improve development efficiency. The following will introduce how to use custom components to achieve page reuse and provide code examples. 1. Create a custom component. First, create a new folder in the components directory of the UniApp project and name it custom-

UniApp is a cross-platform development framework based on Vue.js. It can realize the development of multiple terminals (including apps, mini programs, H5 and other platforms) through a set of codes. Compared with traditional native development, UniApp provides a more efficient and convenient way to develop cross-platform applications. In this article, we will introduce how UniApp implements the development of custom components and modules, and give the corresponding design and development methods. 1. Design and development methods of custom components Component design: First, we need to clearly define the custom components

Steps and precautions for using the Vue.extend function to customize components In Vue.js, you can easily customize components using the Vue.extend function. Custom components allow us to create reusable components based on business needs and call them in different places. This article will introduce the steps to use the Vue.extend function to customize components and some things to pay attention to, and demonstrate it through code examples. Step 1: Import Vue.js and Vue.extend function to start custom group

As a commonly used JavaScript framework, Vue provides very convenient and powerful tools for processing form input. Vue provides reactive properties and events that allow us to easily handle form input, validation, and submission. This article will introduce how to implement responsive forms and custom form components under Vue. 1. Implementation of Vue responsive form Form model binding Vue provides a very simple way to implement form data input and automatic response. Through the v-model directive, we can convert the form

Methods and examples of custom components using the Vue.extend function Vue is a popular JavaScript framework for building user interfaces. It provides a simple and intuitive way to create reusable and composable components. The standard way to define components in Vue is to use the Vue.component function, but in some cases we may need to create components dynamically. Vue provides the Vue.extend function to help us achieve this. Vue.exten

Vue3 is a very powerful JavaScript framework, its core is Vue instances and components. Vue instances are created with defined options, and components are defined by the Vue component API. The createClass function in Vue3 is exactly one of the component APIs. What is the createClass function? Vue3's createClass function provides a declarative way to define components. This is a very important concept because it provides opportunities for component reuse and

: 1. Servlet Overview Servlet is a server-side programming component widely used in JAVAWEB development. Compared with traditional CGI scripts such as CGI, it has many advantages. First of all, Servlet is based on Java technology, so it can be easily integrated into JavaEE applications. Secondly, Servlet components can be reused, which helps improve development efficiency and code quality. 2. Servlet extension Servlet is not an immutable component. Developers can extend it according to their own needs to achieve specific functions. There are mainly the following ways to extend Servlet: Inherit the Servlet class: This is the simplest way, developers can inherit the Servlet class
