1。menus 实现:

$.fn.menu=function(options){
var $this=$(this);
var cross='<div class="zhiniu_crossup"><div class="zhiniu_crossup_sub"></div></div>';
var crossRight='<div class="zhiniu_crossright"><div class="zhiniu_crossright_sub"></div></div>';
var html='<div class="zhiniu_dropdown_menu"></div>';
var ul='<ul class="zhiniu_dropdown_menu_ul"></ul>';
var li='<li class="zhiniu_dropdown_menu_li"></li>';
var liSpace='<li class="zhiniu_dropdown_menu_li_space"></li>';
var tubs={
'setting':'<span class="glyphicon glyphicon-cog" aria-hidden="true"></span>',
'penceil':'<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>',
'video':'<span class="glyphicon glyphicon-facetime-video" aria-hidden="true"></span>',
'article':'<span class="glyphicon glyphicon-list" aria-hidden="true"></span>',
'logout':'<span class="glyphicon glyphicon-log-out zhiniu_dropmenu_exit" aria-hidden="true"></span>'
}
//*********************in case exist********************\\
if($('.zhiniu_dropdown_menu').length>0){
$('.zhiniu_dropdown_menu').remove();
} $(document.body).append(html); var location=options.location,crossDirect=options.cross;
var marginleft=location[0]||0,margintop=location[1]||0;
$('.zhiniu_dropdown_menu').css(
{'position':'absolute','left':($this.offset().left+marginleft)+'px','top':($this.offset().top+margintop)+'px'}
);
//***********************init DOM *****************************\\
switch(crossDirect){
case 'right':$('.zhiniu_dropdown_menu').append(crossRight);break;
default:$('.zhiniu_dropdown_menu').append(cross);
} $('.zhiniu_dropdown_menu').append(ul);
var menus=options.menus || {}; if(menus instanceof Array){
for(var i=0;i<menus.length;i++){
if(i!=0){
$('.zhiniu_dropdown_menu_ul').append(liSpace);
}
var liObj=$(li).html('<a href="javascript:void(0);">'+(tubs[menus[i].type]?tubs[menus[i].type]:'')+menus[i].content+'</a>').get(0);
if(menus[i].handler && menus[i].handler.length>0){
$(liObj).bind('click',menus[i].handler[0].click);
}
$('.zhiniu_dropdown_menu_ul').append(liObj);
}
}
$('.zhiniu_dropdown_menu').bind('mouseleave',function(){$(this).remove();});
$('.zhiniu_dropmenu_exit').parent('a').addClass('zhiniu_dropmenu_exit');
$('.zhiniu_dropmenu_exit').click(function(){
$('.zhiniu_dropdown_menu').remove();
});
};

2.面向对象的优化

function Menu(ele,opts){
var defaults={
w:'100px',
h:'100px',
location:[-100,-20],
cross:'right'
};
this.$elemt=ele;
this.opts=$.extend({},defaults,opts);
this.html={
cross:'<div class="zhiniu_crossup"><div class="zhiniu_crossup_sub"></div></div>',
crossRight:'<div class="zhiniu_crossright"><div class="zhiniu_crossright_sub"></div></div>',
html:'<div class="zhiniu_dropdown_menu"></div>',
ul:'<ul class="zhiniu_dropdown_menu_ul"></ul>',
li:'<li class="zhiniu_dropdown_menu_li"></li>',
liSpace:'<li class="zhiniu_dropdown_menu_li_space"></li>'
};
this.tubs={
'setting':'<span class="glyphicon glyphicon-cog" aria-hidden="true"></span>',
'penceil':'<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>',
'video':'<span class="glyphicon glyphicon-facetime-video" aria-hidden="true"></span>',
'article':'<span class="glyphicon glyphicon-list" aria-hidden="true"></span>',
'logout':'<span class="glyphicon glyphicon-log-out zhiniu_dropmenu_exit" aria-hidden="true"></span>'
}
} Menu.prototype={
init:function(){
//*****in case of exists*****\\
var that = this;
return that.$elemt.each(function(){
if($('.zhiniu_dropdown_menu').length>0){
$('.zhiniu_dropdown_menu').remove();
} $(document.body).append(that.html.html); var marginleft=that.opts.location[0],
margintop=that.opts.location[1]; $('.zhiniu_dropdown_menu').css(
{'position':'absolute','left':(that.$elemt.offset().left+marginleft)+'px','top':(that.$elemt.offset().top+margintop)+'px'}
); //***********************init DOM *****************************\\
switch(that.opts.cross){
case 'right':$('.zhiniu_dropdown_menu').append(that.html.crossRight);break;
default:$('.zhiniu_dropdown_menu').append(that.html.cross);break;
} $('.zhiniu_dropdown_menu').append(that.html.ul); var menus=that.opts.menus || {}; if(menus instanceof Array){
for(var i=0;i<menus.length;i++){
if(i!=0){
$('.zhiniu_dropdown_menu_ul').append(that.html.liSpace);
}
var liObj=$(that.html.li).html('<a href="javascript:void(0);">'+(that.tubs[menus[i].type]?that.tubs[menus[i].type]:'')+menus[i].content+'</a>').get(0);
if(menus[i].handler && menus[i].handler.length>0){
$(liObj).bind('click',menus[i].handler[0].click);
}
$('.zhiniu_dropdown_menu_ul').append(liObj);
}
}
$('.zhiniu_dropdown_menu').bind('mouseleave',function(){$(this).remove();});
$('.zhiniu_dropmenu_exit').parent('a').addClass('zhiniu_dropmenu_exit');
$('.zhiniu_dropmenu_exit').click(function(){
$('.zhiniu_dropdown_menu').remove();
}); });
}
} $.fn.menu=function(){
var that=this;
var opts=arguments[0];
var menu = new Menu(that,opts);
return menu.init();
}

  2.tooltips 实现:

var $this=this;
//position data\\
var pos_left=$this.offset().left+$this.parent().width()+'px',
pos_top =$this.offset().top+$this.height()/5+'px'; //predelete tip\\
var _id='zhiniu_tooltipccc'+$this.attr('id');
$('#'+_id).remove(); var args=arguments; //1:content,2:time
if(arguments.length>0){
var content=args[0],time=args[1]; //********initDom********\\
$(document.body).append('<div class="zhiniu_tooltip" id="'+_id+'"></div>');
$('#'+_id).html(content+'<div class="zhiniu_tooltip_space"></div>').css({
left:pos_left,
top:pos_top
}) if(time && time!=0){
//*******handle time,time up remove********\\
$('#'+_id).animate({display:'none'},time*1000,function(){
$('#'+_id).remove();
})
} }

  

2.面向对象的优化

function Tooltips(eme,opt){
this.$elemt=eme;
var defaults={
color:'#ff0000',
time:2
};
this.opts=$.extend({},defaults,opt);
} Tooltips.prototype={
init:function(){
var that = this,
$this = that.$elemt;
return that.$elemt.each(function(){ var pos_left=$this.offset().left+$this.parent().width()+'px',
pos_top =$this.offset().top+$this.height()/5+'px'; //*****in case of exists*****\\
var _id='zhiniu_tooltipccc'+($this.attr('id')||$this.attr('className')||$this.attr(tagName));
$('#'+_id).remove(); var content=that.opts[0],time=that.opts[1] || that.opts.time ; //********initDom********\\
$(document.body).append('<div class="zhiniu_tooltip" id="'+_id+'"></div>');
$('#'+_id).html(content+'<div class="zhiniu_tooltip_space"></div>').css({
left:pos_left,
top:pos_top
}) if(time && time!=0){
//*******handle time,time up remove********\\
$('#'+_id).animate({display:'none'},time*1000,function(){
$('#'+_id).remove();
})
} }); }
};
$.fn.tooltips=function(){
var that = this,
opts = arguments; var tooltips=new Tooltips(that,opts);
return tooltips.init();

  

  3.alert实现

var msg=arguments[0];
var wh=arguments[1] || [200,100];
var w=wh[0],h=wh[1];
var alertBGDIV='<div class="zhiniu_alert_bg"></div>';
var alertMSGDIV='<div class="zhiniu_alert_msg"><div class="alert alert-danger" role="alert" style="margin:0;text-align:center"></div><div class="zhiniu_alert_yes">确定</div></div>';
var body=$(document.body);
var screenWidth=body.width(),screenHeight=body.height();
var left=(screenWidth-w)/2,top=(screenHeight-h)/2; //*********remove bgdiv*************\\
$('.zhiniu_alert_bg').remove();
$('.zhiniu_alert_msg').remove(); body.append(alertBGDIV);
body.append(alertMSGDIV);
$('.zhiniu_alert_msg').css({
width:w+'px',
height:h+'px',
left:left+'px',
top:top+'px'
})
$('.alert-danger').html(msg);
$('.zhiniu_alert_bg').bind('click',function(){
$('.zhiniu_alert_msg').remove();
$('.zhiniu_alert_bg').remove();
});
$('.zhiniu_alert_yes').bind('click',function(){
$('.zhiniu_alert_msg').remove();
$('.zhiniu_alert_bg').remove();
});

  3.优化

function Alert(ele,opt){
this.$elemt = ele;
this.w=(opt[1]|| [200,100])[0];
this.h=(opt[1]|| [200,100])[1];
this.msg = opt[0] || '请稍侯...';
// this.opts=$({},{msg:'请稍等...',wh:[200,100]},);
this.elem={
alertBGDIV:'<div class="zhiniu_alert_bg"></div>',
alertMSGDIV:'<div class="zhiniu_alert_msg"><div class="alert alert-danger" role="alert" style="margin:0;text-align:center"></div><div class="zhiniu_alert_yes">确定</div></div>'
};
this.init();
} Alert.prototype={
init:function(){
var that = this; var $body= $(document.body);
var screenWidth=$body.width(),
screenHeight=$body.height();
var w = that.w,h = that.h;
var left=(screenWidth-w)/2,
top=(screenHeight-h)/2; //*********remove bgdiv*************\\
$('.zhiniu_alert_bg').remove();
$('.zhiniu_alert_msg').remove(); $body.append(that.elem.alertBGDIV);
$body.append(that.elem.alertMSGDIV); $('.zhiniu_alert_msg').css({
width:w+'px',
height:h+'px',
left:left+'px',
top:top+'px'
}) this.initEvents();
},
initEvents:function(){
var that = this;
$('.zhiniu_alert_bg,.zhiniu_alert_yes').bind('click',function(){
that.close();
});
/*
$('.zhiniu_alert_yes').bind('click',function(){
that.close();
});
*/
},
close:function(){
$('.zhiniu_alert_msg').remove();
$('.zhiniu_alert_bg').remove();
},
alert:function(){
var that = this;
//return that.$elemt.each(function(){
$('.alert-danger').html(that.msg);
//});
}
}
$.alert=function(){
var alt = new Alert(this,arguments);
return alt.alert();

  完成。用面向对象的思想去处理插件的时候,要给插件添加功能会更方便。

jquery 插件的实现和优化的更多相关文章

  1. 6款强大的 jQuery 网页布局创建及优化插件

    本文将为您介绍6款功能强大的jQuery插件,它们能够帮助您方便快捷地创建复杂的网络布局并进行优化. 1.UI.Layout 该插件可以创建任何你想要的UI形式:包括从简单的标题或侧边栏,到一个包含工 ...

  2. 15 款优化表单的 jQuery 插件

    网页上的表单提供给用户的一种交互的方式,用户输入数据,提交到服务器,等待后续的处理.这些表单在我们浏览的网页中随处可见,也容易被我们忽略. 比如,“联系我们”页面会提供一个表单给用户填写他们的信息和想 ...

  3. ajax——三级联动下拉列表框的优化(简化页面,用jquery插件代替原来页面代码,返回处理数据类型为"TEXT")

    数据库: 主页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  4. 分享20款移动开发中很有用的 jQuery 插件

    今天,很显然每个网站都需要有一个移动优化的界面以提高移动用户的使用体验.在开发任何移动项目时,要尽可能保持每一种资源尺寸都尽可能的小,以给最终用户提供一个好的体验是非常重要的.在这篇文章中我们已经编制 ...

  5. 原创jquery插件treeTable(转)

    由于工作需要,要直观的看到某个业务是由那些子业务引起的异常,所以我需要用树表的方式来展现各个层次的数据. 需求: 1.数据层次分明: 2.数据读取慢.需要动态加载孩子节点: 3.支持默认展开多少层. ...

  6. 最新的jQuery插件和JavaScript库

    每一个前端开发人员很清楚的重要性和功能的JavaScript库提供.它提供了一个简单的接口,用于构建快速动态的接口,而无需大量的代码. 谢谢你的超级从事jQuery开发者社区,人始终是创造新的和令人惊 ...

  7. jQuery插件写法总结以及面向对象方式写法总结

    前两个是jQuery插件,后面2个是以对象的形式开发,都类似. 写法一 (function($, window){ // 初始态定义 var _oDialogCollections = {}; // ...

  8. 网站开发中很实用的 HTML5 & jQuery 插件

    这篇文章挑选了15款在网站开发中很实用的 HTML5 & jQuery 插件,如果你正在寻找能优化网站,使其更具创造力和视觉冲击,那么本文正是你需要的.这些优秀的 jQuery 插件能为你的网 ...

  9. JQuery插件:遮罩+数据加载中。。。(特点:遮你想遮,罩你想罩)

    在很多项目中都会涉及到数据加载.数据加载有时可能会是2-3秒,为了给一个友好的提示,一般都会给一个[数据加载中...]的提示.今天就做了一个这样的提示框. 先去jQuery官网看看怎么写jQuery插 ...

随机推荐

  1. Java基础教程:JDBC编程

    Java基础教程:JDBC编程 1.什么是JDBC JDBC 指 Java 数据库连接,是一种标准Java应用编程接口( JAVA API),用来连接 Java 编程语言和广泛的数据库. JDBC A ...

  2. 《CSS权威指南(第三版)》---第七章 基本视觉格式化

    主要知识记录: 1.给一个元素指定内容区宽度,如果设置了内边距,边框和外边距,这些因素都会影响CSS的width属性. 2.在水平格式化的7个属性中,width,margin-left,margin- ...

  3. Contiki 2.7 Makefile 文件(三)

    2.第二部分 这里的usage,targets,savetarget,savedefines都是伪目标. 和all不同,这些伪目标不会被执行,除非显式指定这些目标. 这里有两个目标savetarget ...

  4. HDU 4405 Aeroplane chess:期望dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意: 你在下简化版飞行棋... 棋盘为一个线段,长度为n. 上面有m对传送门,可以直接将你从a ...

  5. ES索引瘦身 禁用_source后需要设置field store才能获取数据 否则无法显示搜索结果

    在默认情况下,开启_all和_source 这样索引下来,占用空间很大. 根据我们单位的情况,我觉得可以将需要的字段保存在_all中,然后使用IK分词以备查询,其余的字段,则不存储. 并且禁用_sou ...

  6. POJ3107Godfather(求树的重心裸题)

    Last years Chicago was full of gangster fights and strange murders. The chief of the police got real ...

  7. P1912 [NOI2009]诗人小G[决策单调性优化]

    地址 n个数划分若干段,给定$L$,$p$,每段代价为$|sum_i-sum_j-1-L|^p$,求总代价最小. 正常的dp决策单调性优化题目.不知道为什么luogu给了个黑题难度.$f[i]$表示最 ...

  8. windwos 10 谷歌浏览器出现彩色闪条

    应该是上个星期五开始,发现电脑从别的地方切换到谷歌浏览器就会出现闪条,开始也没太注意,但是到周一还是这样,所以再网上查了下, 说什么的都有,什么你按脑屏幕坏了,内存条不行什么是的.后来才发现原来是谷歌 ...

  9. windows下vs2012用gsoap开发webservice实例

    零:说明 1.本文是根据网上前人经验结合自己动手操作写成,开发工具用的vs2012,gsoap用的是gsoap-2.8: 2.gsoap提供的工具简单介绍 1)wsdl2h.exe:根据WSDL文件生 ...

  10. bootstrap 全局样式

    reset.css html { font-family: sans-serif; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100% ...