編寫自己的jQuery提示框(Tip)插件
對(duì)jQuery相信很多同學(xué)和我一樣平時(shí)都是拿來(lái)主義,沒辦法,要怪只能怪jQuery太火了,各種插件基本能滿足平時(shí)的要求。但是這畢竟不是長(zhǎng)久之道,古人云:“授之以魚,不如授之以漁”。
為了方便之前沒有接觸的同學(xué),先來(lái)回顧一下jQuery的插件機(jī)制吧。
//添加check和uncheck插件
jQuery.fn.extend({
check: function() {
return this.each(function() { this.checked = true; });
},
uncheck: function() {
return this.each(function() { this.checked = false; });
}
});
//插件的使用
$("input[type=checkbox]").check();
$("input[type=radio]").uncheck();
其實(shí)jQuery的插件非常簡(jiǎn)單,怪不得jQuery插件滿天飛,之前是我想復(fù)雜了,總覺得寫插件是很高深的事情,不知道有沒有同感的同學(xué)。
動(dòng)手之前先來(lái)做一下需求分析吧(ps:哥是學(xué)軟件工程出生的學(xué)費(fèi)很坑爹啊,不搞搞需求分析對(duì)不起學(xué)費(fèi)啊,呵呵)。
其實(shí)也沒啥好分析的就是做出如下效果:
鼠標(biāo)放上去的時(shí)候彈出微信掃一掃,微信太火了,老板讓網(wǎng)站上放一個(gè),所以哥寫了個(gè)插件滿足一下他,發(fā)工資就是上帝,給錢干活,不要給我談節(jié)操,it宅男都是三觀盡毀,節(jié)操全無(wú)的。扯遠(yuǎn)了??葱Ч麍D吧。

使用方法其他jQuery沒什么不同:
$(function(){
var t = $(".weixin").Tip({
title:'微信掃一掃',
content:'<img src="img/weixin.jpg" width="160px" height="160px;">',
html:true,
direction:'bottom'
});
t.bind({
mouseover:function(){
t.Tip("show");
},
mouseout:function() {
t.Tip("hide");
}
});
});
看一下可配置的選項(xiàng)吧
defaultOptions :{
title : '',//標(biāo)題
content : '',//內(nèi)容
direction : 'bottom',//彈出反向,相對(duì)于選中元素
html : false,//是否允許內(nèi)容為html元素
template : '<div class="tip"><div class="tip-inner"><h3></h3><div class="tip-container"></div></div></div>'//彈出框模版
}
最后上高清無(wú)碼源碼有興趣的看看,沒興趣的ctrl+c,ctrl+v吧
!function($){
var Tip = function(element, options){
this.init(element, options);
}
Tip.prototype = {
constructor : Tip,
init : function(element, options){
this.element = $(element);
this.options = $.extend({},this.defaultOptions,options);
},
show : function() {
if (!this.tip) {
this.tip = this.getTip();
var title = this.tip.find("h3"),
container = this.tip.find(".tip-container");
//設(shè)置標(biāo)題
title.text(this.options.title);
//設(shè)置內(nèi)容
if (this.options.html) {
container.html(this.options.content);
} else {
container.text(this.options.content);
}
//添加tip到body
$("body").append(this.tip);
//計(jì)算tip的位置
var eLeft = this.element.offset().left,
eTop = this.element.offset().top,
eWidth = this.element.innerWidth(),
eHeight = this.element.innerHeight(),
tipw = this.tip[0].offsetWidth,
tiph = this.tip[0].offsetHeight,
top,
left;
switch(this.options.direction) {
case 'top' :
top = eTop - tiph;
left = (eLeft - tipw/2) + eWidth/2;
this.tip.css({top: top, left: left});
break;
case 'left' :
top = (eTop - tiph/2) + eHeight/2;
left = eLeft - tipw;
this.tip.css({top: top, left: left});
break;
case 'bottom' :
top = eTop + eHeight;
left = (eLeft - tipw/2) + eWidth/2;
this.tip.css({top: top, left: left});
break;
case 'right' :
top = (eTop - tiph/2) + eHeight/2;
left = eLeft + eWidth;
this.tip.css({top: top, left: left});
break;
default:
break;
}
} else {
this.tip.css({display:'block'});
}
},
hide : function() {
this.getTip().css({display:"none"});
},
getTip : function() {
return this.tip ? this.tip : $(this.options.template);
},
detach : function() {
},
defaultOptions :{
title : '',
content : '',
direction : 'bottom',
html : false,
template : '<div class="tip"><div class="tip-inner"><h3></h3><div class="tip-container"></div></div></div>'
}
}
$.fn.Tip = function(option) {
return this.each(function(){
var e = $(this),
data = e.data('tip'),
options = typeof option == "object" && option;
if (!data) e.data("tip", new Tip(this,options));
if (typeof option == 'string') data[option]();
});
}
}(window.jQuery);
css樣式
.tip {
position: absolute;
padding: 3px;
background: #efefef;
border-radius: 2px;
top: 0px;
left: 0px;
}
.tip .tip-inner {
background: #fafafb;
border: 1px solid #d8d8d8;
}
.tip .tip-inner h3 {
font-size: 14px;
padding: 5px;
border-bottom: 1px solid #eee;
}
.tip .tip-inner .tip-container {
padding: 5px;
}
以上就是本文的所有內(nèi)容了,小伙伴們對(duì)如何編寫jQuery插件是否有了新的 認(rèn)識(shí)了呢,希望大家能夠喜歡本文。
- jQuery帶箭頭提示框tooltips插件集錦
- jquery SweetAlert插件實(shí)現(xiàn)響應(yīng)式提示框
- 屬于你的jQuery提示框(Tip)插件
- jquery插件珍藏(圖片局部放大/信息提示框)
- jquery插件制作 提示框插件實(shí)現(xiàn)代碼
- 基于jQuery Tipso插件實(shí)現(xiàn)消息提示框特效
- jQuery消息提示框插件Tipso
- jQuery懸停文字提示框插件jquery.tooltipster.js用法示例【附demo源碼下載】
- Colortip基于jquery的信息提示框插件在IE6下面的顯示問(wèn)題修正方法
- jQuery提示框插件SweetAlert用法分析
相關(guān)文章
jQuery對(duì)val和atrr("value")賦值的區(qū)別介紹
jQuery中val和atrr(value),對(duì)瀏覽器的區(qū)別,有默認(rèn)值的情況下,如果用val()賦值了,那么當(dāng)修改這個(gè)值得時(shí)候,google不能獲取最新的值,但是ie可以2014-09-09
JQuery擴(kuò)展插件Validate 5添加自定義驗(yàn)證方法
從前面的示例中不難看出validate中自帶的驗(yàn)證方法足以滿足一般的要求,對(duì)于特別的要求可以使用addMethod(name,method,message)添加自定義的驗(yàn)證規(guī)則,下面的示例中添加了一個(gè)用于正則表達(dá)式驗(yàn)證的擴(kuò)展驗(yàn)證的方法2011-09-09
基于JQuery和原生JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)定位導(dǎo)航特效
本文通過(guò)實(shí)例代碼給大家介紹了基于JQuery和原生JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)定位導(dǎo)航特效,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-04-04
jQuery進(jìn)行組件開發(fā)完整實(shí)例
這篇文章主要介紹了jQuery進(jìn)行組件開發(fā)的方法,以完整實(shí)例形式分析了基于jQuery實(shí)現(xiàn)自定義組件的相關(guān)技巧,代碼備有詳盡的注釋便于理解,需要的朋友可以參考下2015-12-12
jquery 頁(yè)面滾動(dòng)到指定DIV實(shí)現(xiàn)代碼
頁(yè)面滾動(dòng)到指定DIV的方法有很多,在本文將為大家介紹下jquery是如何實(shí)現(xiàn)的2013-09-09
jQuery插件Timelinr 實(shí)現(xiàn)時(shí)間軸特效
時(shí)間軸是依據(jù)時(shí)間順序,把一方面或多方面的事件串聯(lián)起來(lái),形成相對(duì)完整的記錄體系,再運(yùn)用圖文的形式呈現(xiàn)給用戶,本文給大家收集了互聯(lián)網(wǎng)上的效果比較不錯(cuò)的一款,分享給大家2015-10-10

