/* * 鼠标提示(jTitle)插件 * 江鸿宾(QQ33080907) * 最近修改:2016.12.09 * 本插件只用于作者参与的项目,未经许可请勿转载 */ //绑定插件用法 //;$(function(){$('[title]').jTitle({style:'yellow'});}); //插件 (function ($) { 'use strict'; $.fn.jTitle = function (options) { //默认参数 var defaults = { style: 'yellow' }; var opts = $.extend(defaults, options); var style = {}; //风格(借鉴:http://tutorialzine.com/2010/07/colortips-jquery-tooltip-plugin/) if (opts.style === 'yellow') { style = { background: 'rgba(249,242,186,.9)', border: '1px solid #e9d315', color: '#5b5316' }; } else if (opts.style === 'blue') { style = { background: 'rgba(217,241,251,.9)', border: '1px solid #7fcdee', color: '#1b475a' }; } else if (opts.style === 'green') { style = { background: 'rgba(242,253,241,.9)', border: '1px solid #b6e184', color: '#558221' }; } else if (opts.style === 'red') { style = { background: 'rgba(187,59,29,.9)', border: '1px solid #8f2a0f', color: '#fcfcfc' }; } else if (opts.style === 'black') { style = { background: 'rgba(51,51,51,.9)', border: '1px solid #111', color: '#fcfcfc' }; } else if (opts.style === 'white') { style = { background: 'rgba(255,255,255,.9)', border: '1px solid #ddd', color: '#555' }; } //提示框,嵌套两层是为了避免使用padding和border产生宽度计算混乱 var jtitle = $(''); //把提示框插入到文档中 if ($('jtitle').length < 1) { jtitle.appendTo($('body')).mouseover(function () { $(this).hide(); }); } //为当前绑定的每个元素赋予hover事件 return this.each(function () { var $this = $(this); var title = $this.attr('title').replace(/\r/g, '
'); if (title !== '') { $this .removeAttr('title') .data('title',title) .hover(function (e) { //重置宽度和位置 jtitle.width('auto').css({ top: 0, left: 0 }).find('div').html(title); //设置宽度,非XHTML文档和IE6不支持max-width,所以用JS统一处理 var w = jtitle.width(); if (w > 300) { w = 300; } jtitle.width(w); setPosition(e); jtitle.fadeIn('normal'); }, function () { jtitle.hide(); }) .mousemove(function (e) { setPosition(e); }); } }); //设置提示框的位置 function setPosition(e) { var ww = $(window).width(); if (ww === 0) { ww = $(document).width(); } var w = jtitle.width(); //提示框和光标有23的垂直偏移量,但是I形光标和箭头光标提示框离得过远,暂时无法解决 var top = e.pageY + 23; var left = e.pageX; var rightmargin = ww - left; //后面减1表示留有空隙,防止和浏览器右边框靠得太紧 if (rightmargin < w) { left = ww - w - 1; } jtitle.css({ top: top, left: left }); } }; })(jQuery);