对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="/UploadFiles/2021-04-02/weixin.jpg">         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 "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插件是否有了新的 认识了呢,希望大家能够喜欢本文。

标签:
jQuery,提示框,Tip,插件

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“编写自己的jQuery提示框(Tip)插件”
暂无“编写自己的jQuery提示框(Tip)插件”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。