属于你的jQuery提示框(Tip)插件_javascript技巧
插件可以满足常用的提示显示,支持12个方向,支持边框、背景色、文本颜色自定义,支持位置微调、层级微调、宽度间距等参数调整。
先看看效果:
tips:提示信息组件
参数:
- msg:'asdf',内容
- dire:2,方向
- w:250,宽度
- _x:0,横向偏移
- _y:0,纵向偏移
- zIndex:100000,层级
- borderColor:#FFF,边框颜色
- bgColor:#FFF,背景颜色
- useHover:true是否使用悬浮显示
- color:默认提示文字颜色
- padding:边距
javascript代码:
(function ($) { var defaults = { dire: 12, w: 250, _x: 0, _y: 0, borderColor: '#FFBB76', bgColor: '#FFFCEF', color: '#FF0000', padding: [5, 10], arrWidth: 10, useHover: true, zIndex: 100000 }; $.fn.tips = function (opt) { var tip, opts = $.extend({}, defaults, opt); if (this[0]) { opts.tag = this; if (opts.useHover) { opts.tag.hover(function () { tip = new Tip(opts); tip.show(); }, function () { tip.close(); }); } else { tip = new Tip(opts); tip.show(); } return this; } }; function Tip(opts) { this.dire = opts.dire; this.width = opts.w; this.zIndex = opts.zIndex; this.borderColor = opts.borderColor; this.bgColor = opts.bgColor; this.color = opts.color; this.padding = opts.padding; this.arrWidth = opts.arrWidth; this.offsetX = opts._x; this.offsetY = opts._y; this.tag = opts.tag; this.msg = opts.msg; this.wrap = $('<div class="tip-wrap"></div>'); this.innerArr = $('<div class="tip-arr-a"></div>'); this.outerArr = $('<div class="tip-arr-b"></div>'); this.init(); }; Tip.prototype = { init: function () { var msg = this.tag.data('tipMsg'); if (!this.msg) { this.msg = msg; } this.createTemp(); }, createTemp: function () { var t = this; t.createWrap(); t.setPosition(); }, createWrap: function () { var t = this; t.wrap.html(t.msg); var wrapCSS = { width: t.width, border: '1px solid ' + t.borderColor, 'border-radius': '5px', background: t.bgColor, color: t.color, padding: t.getPadding() }; t.outerArr.css(t.getArrStyle(t.dire, t.arrWidth, t.borderColor)); t.innerArr.css(t.getArrStyle(t.dire, t.arrWidth, t.bgColor)); t.wrap.prepend(t.innerArr).prepend(t.outerArr).css(wrapCSS); $('body').append(t.wrap); }, setPosition: function () { var t = this; var posObj = t.getPos(t.dire, t.getPosition(t.tag), t.getPosition(t.wrap), t.arrWidth), pos = posObj.pos, innerPos = posObj.innerPos, outerPos = posObj.outerPos; t.wrap.css({top: pos.y, left: pos.x}); t.innerArr.css({top: innerPos.y, left: innerPos.x}); t.outerArr.css({top: outerPos.y, left: outerPos.x}); }, getPadding: function () { var t = this, pad = '0px', padArr = t.padding, len = padArr.length; switch (len) { case 1: pad = padArr[0] + 'px'; break; case 2: pad = padArr[0] + 'px ' + padArr[1] + 'px'; break; case 3: pad = padArr[0] + 'px ' + padArr[1] + 'px ' + padArr[2] + 'px'; break; case 4: pad = padArr[0] + 'px ' + padArr[1] + 'px ' + padArr[2] + 'px ' + padArr[3] + 'px'; break; } return pad; }, getPosition: function (tag) { return {t: tag.offset().top, l: tag.offset().left, h: tag.outerHeight(), w: tag.outerWidth()}; }, getArrStyle: function (dir, width, color) { var style; switch (dir) { case 11: case 12: case 1: style = { 'border-bottom-style': 'solid', 'border-width': '0px ' + width + 'px ' + width + 'px', 'border-bottom-color': color }; break; case 2: case 3: case 4: style = { 'border-left-style': 'solid', 'border-width': width + 'px 0px ' + width + 'px ' + width + 'px', 'border-left-color': color }; break; case 5: case 6: case 7: style = { 'border-top-style': 'solid', 'border-width': width + 'px ' + width + 'px 0px', 'border-top-color': color }; break; case 8: case 9: case 10: style = { 'border-right-style': 'solid', 'border-width': width + 'px ' + width + 'px ' + width + 'px 0px', 'border-right-color': color }; break; } return style || {}; }, getPos: function (d, tagPos, pos, arrWidth) { var _pos, _innerPos, _outerPos, l = tagPos.l, t = tagPos.t, w = tagPos.w, h = tagPos.h, ww = pos.w, hh = pos.h; switch (d) { case 0: case 1: _pos = {x: l + w / 2 + arrWidth + 20 + 1 - ww, y: t + h + arrWidth}; _outerPos = {x: ww - 2 - 20 - arrWidth * 2, y: -arrWidth}; _innerPos = {x: ww - 2 - 20 - arrWidth * 2, y: -arrWidth + 1}; break; case 2: _pos = {x: l - ww - arrWidth, y: t + h / 2 - arrWidth - 20 - 1}; _outerPos = {x: ww - 2, y: 20}; _innerPos = {x: ww - 2 - 1, y: 20}; break; case 3: _pos = {x: l - ww - arrWidth, y: t + h / 2 - hh / 2}; _outerPos = {x: ww - 2, y: (hh - 2) / 2 - arrWidth}; _innerPos = {x: ww - 2 - 1, y: (hh - 2) / 2 - arrWidth}; break; case 4: _pos = {x: l - ww - arrWidth, y: t + h / 2 + arrWidth + 20 + 1 - hh}; _outerPos = {x: ww - 2, y: hh - 2 - 20 - arrWidth * 2}; _innerPos = {x: ww - 2 - 1, y: hh - 2 - 20 - arrWidth * 2}; break; case 5: _pos = {x: l + w / 2 + arrWidth + 20 + 1 - ww, y: t - arrWidth - hh}; _outerPos = {x: ww - 2 - 20 - arrWidth * 2, y: hh - 2}; _innerPos = {x: ww - 2 - 20 - arrWidth * 2, y: hh - 2 - 1}; break; case 6: _pos = {x: l + w / 2 - ww / 2, y: t - arrWidth - hh}; _outerPos = {x: (ww - 2) / 2 - arrWidth, y: hh - 2}; _innerPos = {x: (ww - 2) / 2 - arrWidth, y: hh - 2 - 1}; break; case 7: _pos = {x: l + w / 2 - 20 - arrWidth, y: t - arrWidth - hh}; _outerPos = {x: 20, y: hh - 2}; _innerPos = {x: 20, y: hh - 2 - 1}; break; case 8: _pos = {x: l + w + arrWidth, y: t + h / 2 + arrWidth + 20 + 1 - hh}; _outerPos = {x: -arrWidth, y: hh - 2 - 20 - arrWidth * 2}; _innerPos = {x: -arrWidth + 1, y: hh - 2 - 20 - arrWidth * 2}; break; case 9: _pos = {x: l + w + arrWidth, y: t + h / 2 - hh / 2}; _outerPos = {x: -arrWidth, y: (hh - 2) / 2 - arrWidth}; _innerPos = {x: -arrWidth + 1, y: (hh - 2) / 2 - arrWidth}; break; case 10: _pos = {x: l + w + arrWidth, y: t + h / 2 - arrWidth - 20 - 1}; _outerPos = {x: -arrWidth, y: 20}; _innerPos = {x: -arrWidth + 1, y: 20}; break; case 11: _pos = {x: l + w / 2 - 20 - arrWidth, y: t + h + arrWidth}; _outerPos = {x: 20, y: -arrWidth}; _innerPos = {x: 20, y: -arrWidth + 1}; break; case 12: _pos = {x: l + w / 2 - ww / 2, y: t + h + arrWidth}; _outerPos = {x: (ww - 2) / 2 - arrWidth, y: -arrWidth}; _innerPos = {x: (ww - 2) / 2 - arrWidth, y: -arrWidth + 1}; break; default: _pos = {x: 0, y: 0}; } return { pos: _pos, innerPos: _innerPos, outerPos: _outerPos }; }, show: function () { this.wrap.show(); }, close: function () { this.wrap.remove(); } }; })(jQuery);
CSS:
.tip-wrap { position: absolute; display: none; } .tip-arr-a, .tip-arr-b { position: absolute; width: 0; height: 0; line-height: 0; border-style: dashed; border-color: transparent; } page: <div class="test"> <span data-tip-msg="我是测试数据<br>我是测试数据<br>我是测试数据">我是测试数据</span> </div> <script> $('.test span').tips(); </script>
效果:
以上就是一款简简单单的jQuery提示框(Tip)插件,希望大家可应用到自己的项目中,有所收获。

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

jQuery引用方法详解:快速上手指南jQuery是一个流行的JavaScript库,被广泛用于网站开发中,它简化了JavaScript编程,并为开发者提供了丰富的功能和特性。本文将详细介绍jQuery的引用方法,并提供具体的代码示例,帮助读者快速上手。引入jQuery首先,我们需要在HTML文件中引入jQuery库。可以通过CDN链接的方式引入,也可以下载

jQuery中如何使用PUT请求方式?在jQuery中,发送PUT请求的方法与发送其他类型的请求类似,但需要注意一些细节和参数设置。PUT请求通常用于更新资源,例如更新数据库中的数据或更新服务器上的文件。以下是在jQuery中使用PUT请求方式的具体代码示例。首先,确保引入了jQuery库文件,然后可以通过以下方式发送PUT请求:$.ajax({u

jQuery是一款广泛应用于前端开发的快速、小巧、功能丰富的JavaScript库。自2006年发布以来,jQuery已经成为众多开发者的首选工具之一,但是在实际应用中,它也不乏一些优势和劣势。本文将深度剖析jQuery的优势与劣势,并结合具体的代码示例进行说明。优势:1.简洁的语法jQuery的语法设计简洁明了,可以大大提高代码的可读性和编写效率。比如,

jQuery如何移除元素的height属性?在前端开发中,经常会遇到需要操作元素的高度属性的需求。有时候,我们可能需要动态改变元素的高度,而有时候又需要移除元素的高度属性。本文将介绍如何使用jQuery来移除元素的高度属性,并提供具体的代码示例。在使用jQuery操作高度属性之前,我们首先需要了解CSS中的height属性。height属性用于设置元素的高度

标题:jQuery小技巧:快速修改页面所有a标签的文本在网页开发中,我们经常需要对页面中的元素进行修改和操作。在使用jQuery时,有时候需要一次性修改页面中所有a标签的文本内容,这样可以节省时间和精力。下面将介绍如何使用jQuery快速修改页面所有a标签的文本,同时给出具体的代码示例。首先,我们需要引入jQuery库文件,确保在页面中引入了以下代码:<

标题:使用jQuery修改所有a标签的文本内容jQuery是一款流行的JavaScript库,被广泛用于处理DOM操作。在网页开发中,经常会遇到需要修改页面上链接标签(a标签)的文本内容的需求。本文将介绍如何使用jQuery来实现这个目标,并提供具体的代码示例。首先,我们需要在页面中引入jQuery库。在HTML文件中添加以下代码:

jQuery是一种流行的JavaScript库,被广泛用于处理网页中的DOM操作和事件处理。在jQuery中,eq()方法是用来选择指定索引位置的元素的方法,具体使用方法和应用场景如下。在jQuery中,eq()方法选择指定索引位置的元素。索引位置从0开始计数,即第一个元素的索引是0,第二个元素的索引是1,依此类推。eq()方法的语法如下:$("s

如何判断jQuery元素是否具有特定属性?在使用jQuery操作DOM元素时,经常会遇到需要判断元素是否具有某个特定属性的情况。这种情况下,我们可以借助jQuery提供的方法来轻松实现这一功能。下面将介绍两种常用的方法来判断一个jQuery元素是否具有特定属性,并附上具体的代码示例。方法一:使用attr()方法和typeof操作符//判断元素是否具有特定属
