编写自己的jQuery提示框(Tip)插件


Posted in Javascript onFebruary 05, 2015

对jQuery相信很多同学和我一样平时都是拿来主义,没办法,要怪只能怪jQuery太火了,各种插件基本能满足平时的要求。但是这毕竟不是长久之道,古人云:“授之以鱼,不如授之以渔”。

为了方便之前没有接触的同学,先来回顾一下jQuery的插件机制吧。

//添加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();

其实jQuery的插件非常简单,怪不得jQuery插件满天飞,之前是我想复杂了,总觉得写插件是很高深的事情,不知道有没有同感的同学。

动手之前先来做一下需求分析吧(ps:哥是学软件工程出生的学费很坑爹啊,不搞搞需求分析对不起学费啊,呵呵)。

其实也没啥好分析的就是做出如下效果:

鼠标放上去的时候弹出微信扫一扫,微信太火了,老板让网站上放一个,所以哥写了个插件满足一下他,发工资就是上帝,给钱干活,不要给我谈节操,it宅男都是三观尽毁,节操全无的。扯远了。看效果图吧。

编写自己的jQuery提示框(Tip)插件

使用方法其他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");

        }

    });

});

看一下可配置的选项吧

defaultOptions :{

            title : '',//标题

            content : '',//内容

            direction : 'bottom',//弹出反向,相对于选中元素

            html : false,//是否允许内容为html元素

            template : '<div class="tip"><div class="tip-inner"><h3></h3><div class="tip-container"></div></div></div>'//弹出框模版

        }

最后上高清无码源码有兴趣的看看,没兴趣的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");

                //设置标题

                title.text(this.options.title);

                //设置内容

                if (this.options.html) {

                    container.html(this.options.content);

                } else {

                    container.text(this.options.content);

                }

                //添加tip到body

                $("body").append(this.tip);

                //计算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;

}

以上就是本文的所有内容了,小伙伴们对如何编写jQuery插件是否有了新的 认识了呢,希望大家能够喜欢本文。

Javascript 相关文章推荐
JavaScript 学习笔记(四)
Dec 31 Javascript
jQuery 入门级学习笔记及源码
Jan 22 Javascript
Javascript实现可旋转的圆圈实例代码
Aug 04 Javascript
详解javascript中原始数据类型Null和Undefined
Dec 17 Javascript
jQuery实现输入框邮箱内容自动补全与上下翻动显示效果【附demo源码下载】
Sep 20 Javascript
简单理解vue中track-by属性
Oct 26 Javascript
ES6新数据结构Set与WeakSet用法分析
Mar 31 Javascript
vue引入swiper插件的使用实例
Jul 19 Javascript
vue.js element-ui tree树形控件改iview的方法
Mar 29 Javascript
pm2发布node配置文件ecosystem.json详解
May 15 Javascript
浅谈vue项目,访问路径#号的问题
Aug 14 Javascript
三剑客:offset、client和scroll还傻傻分不清?
Dec 04 Javascript
使用pjax实现无刷新更改页面url
Feb 05 #Javascript
BOOTSTRAP时间控件显示在模态框下面的bug修复
Feb 05 #Javascript
jquery手风琴特效插件
Feb 04 #Javascript
Jquery中find与each方法用法实例
Feb 04 #Javascript
javascript中Array数组的迭代方法实例分析
Feb 04 #Javascript
AngularJs根据访问的页面动态加载Controller的解决方案
Feb 04 #Javascript
15款jQuery分布引导插件分享
Feb 04 #Javascript
You might like
Windows PHP5和Apache的安装与配置
2009/06/08 PHP
php 判断是否是中文/英文/数字示例代码
2013/09/30 PHP
php实现高效获取图片尺寸的方法
2014/12/12 PHP
Yii2实现自定义独立验证器的方法
2017/05/05 PHP
php的instanceof和判断闭包Closure操作示例
2020/01/26 PHP
addRule在firefox下的兼容写法
2006/11/30 Javascript
JQuery Dialog的内存泄露问题解决方法
2010/06/18 Javascript
客户端js性能优化小技巧整理
2013/11/05 Javascript
jquery.validate[.unobtrusive]和Bootstrap实现tooltip错误提示问题分析
2016/10/30 Javascript
浅析vue数据绑定
2017/01/17 Javascript
微信小程序 五星评价功能的实现
2017/03/09 Javascript
jQuery使用ajax_动力节点Java学院整理
2017/07/05 jQuery
AngularJS实现注册表单验证功能
2017/10/16 Javascript
vue与vue-i18n结合实现后台数据的多语言切换方法
2018/03/08 Javascript
实战node静态文件服务器的示例代码
2018/03/08 Javascript
angularJs复选框checkbox选中进行ng-show显示隐藏的方法
2018/10/08 Javascript
vue-router传参用法详解
2019/01/19 Javascript
vue项目移动端实现ip输入框问题
2019/03/19 Javascript
webpack中如何加载静态文件的方法步骤
2019/05/18 Javascript
微信小程序 (地址选择1)--选取搜索地点并显示效果
2019/12/17 Javascript
python基础入门详解(文件输入/输出 内建类型 字典操作使用方法)
2013/12/08 Python
Python实现识别手写数字 Python图片读入与处理
2020/03/23 Python
Python面向对象程序设计之类的定义与继承简单示例
2019/03/18 Python
详解用Python为直方图绘制拟合曲线的两种方法
2019/08/21 Python
详解从Django Allauth中进行登录改造小结
2019/12/18 Python
Python查找不限层级Json数据中某个key或者value的路径方式
2020/02/27 Python
Python web如何在IIS发布应用过程解析
2020/05/27 Python
python支持多继承吗
2020/06/19 Python
英文求职信结束语大全
2013/10/26 职场文书
中学门卫岗位职责
2013/12/26 职场文书
商场消防管理制度
2014/01/12 职场文书
乡镇防汛工作汇报
2014/10/28 职场文书
门店店长岗位职责
2015/04/14 职场文书
保护环境建议书作文400字
2015/09/14 职场文书
导游词之鲁迅祖居
2019/10/17 职场文书
使用Python开发贪吃蛇游戏 SnakeGame
2022/04/30 Python