引用
提示的功能,就上网找了一个插件,发现colortip实现比较简单,就定了这个插件。
实现过程
效果:
效果:@H_404_177@
文件中进行配置的:
</span><span style="color: #0000ff;">var</span> defaultSettings =<span style="color: #000000;"> {
color : </span>'yellow',<span style="color: #008000;">//</span><span style="color: #008000;">默认颜色</span>
timeout : 500<span style="color: #000000;">
}
</span><span style="color: #0000ff;">var</span> supportedColors = ['red','green','blue','white','yellow','black'];<span style="color: #008000;">//</span><span style="color: #008000;">总共有6种<a href="https://www.jb51.cc/tag/zhuti/" target="_blank" class="keywords">主题</a>的颜色</span>
<span style="color: #008000;">/*</span><span style="color: #008000;"> Combining the default settings object with the supplied one </span><span style="color: #008000;">*/</span><span style="color: #000000;">
settings </span>=<span style="color: #000000;"> $.extend(defaultSettings,settings);
</span><span style="color: #008000;">/*</span><span style="color: #008000;">
* Looping through all the elements and returning them afterwards.
* This will add chainability to the plugin.
</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span>.each(<span style="color: #0000ff;">function</span><span style="color: #000000;">(){
</span><span style="color: #0000ff;">var</span> elem = $(<span style="color: #0000ff;">this</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> If the title attribute is empty,continue with the next element</span>
<span style="color: #0000ff;">if</span>(!elem.attr('title')) <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Creating new eventScheduler and Tip objects for this element.</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> (See the class definition at the bottom).</span>
<span style="color: #0000ff;">var</span> scheduleEvent = <span style="color: #0000ff;">new</span><span style="color: #000000;"> eventScheduler();
</span><span style="color: #0000ff;">var</span> tip = <span style="color: #0000ff;">new</span> Tip(elem.attr('title'<span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Adding the tooltip markup to the element and</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> applying a special class:</span>
<span style="color: #000000;">
elem.append(tip.generate()).addClass(
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Checking to see whether a supported color has been</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> set as a classname on the element.</span>
<span style="color: #0000ff;">var</span> hasClass = <span style="color: #0000ff;">false</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">var</span> i=0;i<supportedColors.length;i++<span style="color: #000000;">)
{
</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(elem.hasClass(supportedColors[i])){
hasClass </span>= <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
}
}
</span><span style="color: #008000;">//</span><span style="color: #008000;"> If it has been set,it will override the default color</span>
<span style="color: #0000ff;">if</span>(!<span style="color: #000000;">hasClass){
elem.addClass(settings.color);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;"> On mouseenter,show the tip,on mouseleave set the</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> tip to be hidden in half a second.</span>
<span style="color: #000000;">
elem.hover(
tip.show();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> If the user moves away and hovers over the tip again,</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> clear the prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>sly set event:</span>
<span style="color: #000000;">
scheduleEvent.clear();
},</span><span style="color: #0000ff;">function</span><span style="color: #000000;">(){
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Schedule event actualy sets a timeout (as you can</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> see from the class definition below).</span>
<span style="color: #000000;">
scheduleEvent.set(
tip.hide();
},settings.timeout);
});
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Removing the title attribute,so the regular OS titles are</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> not shown along with the tooltips.</span>
<span style="color: #000000;">
elem.removeAttr('title'<span style="color: #000000;">);
});
}
</span><span style="color: #008000;">/*</span><span style="color: #008000;">
/ Event Scheduler Class Definition
</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">function</span><span style="color: #000000;"> eventScheduler(){}
eventScheduler.prototype </span>=<span style="color: #000000;"> {
set : </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> (func,timeout){
</span><span style="color: #008000;">//</span><span style="color: #008000;"> The set method takes a function and a time period (ms) as</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> parameters,and sets a timeout</span>
<span style="color: #0000ff;">this</span>.timer =<span style="color: #000000;"> setTimeout(func,timeout);
},clear: </span><span style="color: #0000ff;">function</span><span style="color: #000000;">(){
</span><span style="color: #008000;">//</span><span style="color: #008000;"> The clear method clears the timeout</span>
<span style="color: #000000;">
clearTimeout(<span style="color: #0000ff;">this<span style="color: #000000;">.timer);
}
}
</span><span style="color: #008000;">/*</span><span style="color: #008000;">
/ Tip Class Definition
</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">function</span><span style="color: #000000;"> Tip(txt){
</span><span style="color: #0000ff;">this</span>.content =<span style="color: #000000;"> txt;
</span><span style="color: #0000ff;">this</span>.shown = <span style="color: #0000ff;">false</span><span style="color: #000000;">;
}
Tip.prototype </span>=<span style="color: #000000;"> {
generate: </span><span style="color: #0000ff;">function</span><span style="color: #000000;">(){
</span><span style="color: #008000;">//</span><span style="color: #008000;"> The generate method returns either a prev<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>sly generated element</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> stored in the tip variable,or generates it and saves it in tip for</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> later use,after which returns it.</span>
<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span>.tip || (<span style="color: #0000ff;">this</span>.tip = $('<span class="colorTip">'+<span style="color: #0000ff;">this</span>.content+
'<span class="pointyTipShadow"></span><span class="pointyTip"></span></span>'));<span style="color: #008000;">//</span><span style="color: #008000;"><a href="https://www.jb51.cc/tag/xianshi/" target="_blank" class="keywords">显示</a>的消息<a href="https://www.jb51.cc/tag/tishi/" target="_blank" class="keywords">提示</a>框是一个span<a href="https://www.jb51.cc/tag/biaoqian/" target="_blank" class="keywords">标签</a></span>
<span style="color: #000000;"> },show: <span style="color: #0000ff;">function<span style="color: #000000;">(){
<span style="color: #0000ff;">if(<span style="color: #0000ff;">this.shown) <span style="color: #0000ff;">return<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Center the tip and start a fadeIn animation</span>
<span style="color: #0000ff;">this</span>.tip.css('margin-left',-<span style="color: #0000ff;">this</span>.tip.outerWidth()/2).fadeIn('fast');
<span style="color: #0000ff;">this</span>.shown = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
},hide: </span><span style="color: #0000ff;">function</span><span style="color: #000000;">(){
</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.tip.fadeOut();
</span><span style="color: #0000ff;">this</span>.shown = <span style="color: #0000ff;">false</span><span style="color: #000000;">;
}
}
})(jQuery);
总结
提示信息的地方加个带title属性的a标签就可以了,不过有点差强人意的地方,如果其他的标签例如按钮,加一个title,就会提示错误。
原文链接:https://www.f2er.com/jquery/191586.html