Poshy Tip,基于jQuery的提示插件 返回示例页面

示例

使用本插件替换浏览器默认的title提示:

鼠标移上来看效果

$('#demo-basic').poshytip();

样式设置(Classes)

使用poshy tip插件,设置样式非常的方便,下面是一些例子(在您下载下来的压缩包的“src”文件夹里都能找到):

.tip-yellow

$('#demo-tip-yellow').poshytip();

.tip-violet

$('#demo-tip-violet').poshytip({
	className: 'tip-violet',
	bgImageFrameSize: 9
});

.tip-darkgray

$('#demo-tip-darkgray').poshytip({
	className: 'tip-darkgray',
	bgImageFrameSize: 11,
	offsetX: -25
});

.tip-skyblue

$('#demo-tip-skyblue').poshytip({
	className: 'tip-skyblue',
	bgImageFrameSize: 9,
	offsetX: 0,
	offsetY: 20
});

.tip-yellowsimple (没有使用背景图片的Poshy Tip提示效果)

$('#demo-tip-yellowsimple').poshytip({
	className: 'tip-yellowsimple',
	showTimeout: 1,
	alignTo: 'target',
	alignX: 'center',
	offsetY: 5,
	allowTipHover: false
});

.tip-twitter (ala Twitter)

$('#demo-tip-twitter').poshytip({
	className: 'tip-twitter',
	showTimeout: 1,
	alignTo: 'target',
	alignX: 'center',
	offsetY: 5,
	allowTipHover: false,
	fade: false,
	slide: false
});

.tip-green

$('#demo-tip-green').poshytip({
	className: 'tip-green',
	offsetX: -7,
	offsetY: 16,
	allowTipHover: false
});

Poshy Tip的表单提示 (注意提示的位置)

给表单加上Poshy Tip提示效果也非常简单。只是将触发方式改成了“focus/blur”(i.e. showOn: 'focus'),定位点相对于表单元素即可(i.e. alignTo: 'target')。即使浏览器的窗口大小改变了,代码也会自适应,提示插件会自动跟随表单元素(e.g. 缩放窗口,查看效果).


$('#demo-form-name').poshytip({
	className: 'tip-yellowsimple',
	showOn: 'focus',
	alignTo: 'target',
	alignX: 'right',
	alignY: 'center',
	offsetX: 5,
	showTimeout: 100
});


$('#demo-form-email').poshytip({
	className: 'tip-yellowsimple',
	showOn: 'focus',
	alignTo: 'target',
	alignX: 'left',
	alignY: 'center',
	offsetX: 5,
	showTimeout: 100
});


$('#demo-form-site').poshytip({
	className: 'tip-yellowsimple',
	showOn: 'focus',
	alignTo: 'target',
	alignX: 'inner-left',
	offsetX: 0,
	offsetY: 5,
	showTimeout: 100
});


$('#demo-form-subject').poshytip({
	className: 'tip-yellowsimple',
	showOn: 'focus',
	alignTo: 'target',
	alignX: 'center',
	alignY: 'bottom',
	offsetX: 0,
	offsetY: 5,
	showTimeout: 100
});

内容的异步加载

Poshy Tip支持使用函数返回的提示内容,剧本也经过更新回调函数作为参数传递给这个函数。通过使用这个回调函数,你可以很容易地更新异步的提示内容后已显示。剧本也重新计算和更新提示的位置时,它的内容更新。

示例

1秒后更新内容

$('#demo-async-timeout').poshytip({
	content: function(updateCallback) {
		window.setTimeout(function() {
			updateCallback('Tooltip content updated!');
		}, 1000);
		return 'Loading...';
	}
});

加载Flickr内容

一个更复杂的加载一些Flickr图像的例子:

flowers, closeup, sunset, architecture, Plovdiv, old, town, Nesebar, depeche

var flickrFeedsCache = {};
$('#demo-async-flickr > a').poshytip({
	className: 'tip-darkgray',
	bgImageFrameSize: 11,
	alignY: 'bottom',
	content: function(updateCallback) {
		var rel = $(this).attr('rel');
		if (flickrFeedsCache[rel] && flickrFeedsCache[rel].container)
			return flickrFeedsCache[rel].container;
		if (!flickrFeedsCache[rel]) {
			flickrFeedsCache[rel] = { container: null };
			var tagsComma = rel.substring(4).replace('-', ',');
			$.getJSON('http://api.flickr.com/services/feeds/photos_public.gne?tags=' + tagsComma + '&tagmode=all&format=json&jsoncallback=?',
				function(data) {
					var container = $('<div/>').addClass('flickr-thumbs');
					$.each(data.items, function(i, item) {
						$('<a/>')
							.attr('href', item.link)
							.append($('<img/>').attr('src', item.media.m))
							.appendTo(container)
							.data('tip', '<strong>' + (item.title || '(no title)') + '</strong><br />by: ' + item.author.match(/\((.*)\)/)[1]);
						if (i == 4)
							return false;
					});
					// add tips for the images inside the main tip
					container.find('a').poshytip({
						content: function(){return $(this).data('tip');},
						className: 'tip-yellowsimple',
						showTimeout: 100,
						alignTo: 'target',
						alignX: 'center',
						alignY: 'bottom',
						offsetY: 5,
						allowTipHover: false,
						hideAniDuration: 0
					});
					// store the content in the cache
					// and call updateCallback() to update the content in the main tooltip
					updateCallback(flickrFeedsCache[rel].container = container);
				}
			);
		}
		return 'Loading images...';
	}
});

跟随鼠标

如果设置followCursor: true, 确保幻灯片动画效果更好,这是更好的(即slide: false) 所以它不会与代码移动光标提示与冲突。

跟随鼠标移动的Poshy Tip

$('#demo-follow-cursor').poshytip({
	followCursor: true,
	slide: false
});

API示例 - 手动触发提示

你不用在给网页标签加上Poshy Tip的时候,直接使用 hover、focus、blur等等就触发了,可以使用showOn: 'none',屏蔽触发,然后,通过其他元素的点击等事件来触发它:

这个Poshy Tip不是自动触发的,可以点击下面的按钮,手动触发

$('#demo-manual-trigger').poshytip({
	content: '这是Poshy Tip的提示效果',
	showOn: 'none',
	alignTo: 'target',
	alignX: 'inner-left',
	offsetX: 0,
	offsetY: 5
});
$('#button-show').click(function() { $('#demo-manual-trigger').poshytip('show'); });
$('#button-show-delayed').click(function() { $('#demo-manual-trigger').poshytip('showDelayed', 2000); });
$('#button-hide').click(function() { $('#demo-manual-trigger').poshytip('hide'); });
$('#button-hide-delayed').click(function() { $('#demo-manual-trigger').poshytip('hideDelayed', 2000); });
$('#button-update').click(function() { $('#demo-manual-trigger').poshytip('update', 'I am a new content :)'); });
$('#button-disable').click(function() { $('#demo-manual-trigger').poshytip('disable'); });
$('#button-enable').click(function() { $('#demo-manual-trigger').poshytip('enable'); });
$('#button-destroy').click(function() { $('#demo-manual-trigger').poshytip('destroy'); });

使用 Live 事件

您可以通过设置参数liveEvents: true来开启 Live 事件。请注意,在这种情况下,该方法(除“销毁”)不能可靠地工作。他们只会工作的元素,提示已被初始化(即显示至少一次)。生活事件在jQuery 1.4.2 +支持。

Hover for a tooltip

$('#demo-live-events > a').poshytip({
	liveEvents: true
});
$('#button-live-events').click(function() {
	$('#demo-live-events').append(', <a title="这是Poshy Tip的提示效果" href="#">Hover for a tooltip</a>');
});

参数

content String, DOM element, Function, jQuery
可能的值有: '[title]', 'string', element, function(updateCallback){...}, jQuery
Content to display.
className String
Class for the tips.
bgImageFrameSize Number
包围在Poshy Tip周围的背景图片的尺寸,(如果在CSS中设置了的话)
showTimeout Number
显示提示前的超时(1000毫秒 == 1秒).
hideTimeout Number
隐藏提示前的超时
timeOnScreen Number
自动隐藏之前显示的时间
showOn String
可能的值有: 'hover', 'focus', 'none'
提示触发的事件。如果您想手动触发,请设置为'none'(即通过调用 'show' 'hide' 方法).
liveEvents Boolean
使用 live 事件。

请注意,使用的方法(除“销毁”)在使用Live事件时不能可靠工作。他们只会工作的元素,提示已被初始化(即显示至少一次)。生活事件在jQuery 1.4.2 +支持。
alignTo String
可能的值有: 'cursor', 'target'
Align/position the tip relative to
alignX String
可能的值有: 'right', 'center', 'left', 'inner-left', 'inner-right'
设置提示框相对于鼠标或者触发元素的水平位置 - values 'inner-*' matter if alignTo:'target'
alignY String
可能的值有: 'bottom', 'center', 'top', 'inner-bottom', 'inner-top'
设置提示框相对于鼠标或者触发元素的垂直位置 - values 'inner-*' matter if alignTo:'target'
offsetX Number
提示框相对于默认位置的水平偏移 - doesn't matter if alignX:'center'
offsetY Number
提示框相对于默认位置的垂直偏移 - doesn't matter if alignY:'center'
keepInViewport Boolean
Reposition the tooltip if needed to make sure it always appears inside the viewport.
allowTipHover Boolean
Allow hovering the tip without hiding it onmouseout of the target - matters only if showOn:'hover'
followCursor Boolean
如果要设置提示框跟着鼠标动,只需要开启 showOn:'hover' and alignTo:'cursor'
fade Boolean
显示/隐藏开关
slide Boolean
展开/收起开关
slideOffset Number
展开动画的偏移量
showAniDuration Number
显示动画执行的时间
hideAniDuration Number
隐藏动画执行的事件
refreshAniDuration Number
Animation duration when updating the tooltip asynchronously.

Methods

.poshytip('show')
Manually show the tooltip. Make sure the alignTo option is set to 'target' in order the tooltip to be properly positioned when you trigger it.
.poshytip('showDelayed', [ timeout ] )
Manually show the tooltip with a delay. Make sure the alignTo option is set to 'target' in order the tooltip to be properly positioned when you trigger it.
timeout (optional) number - Custom timeout in milliseconds. If not passed, the showTimeout option value is used by default.
.poshytip('hide')
Manually hide the tooltip.
.poshytip('hideDelayed', [ timeout ] )
Manually hide the tooltip with a delay.
timeout (optional) number - Custom timeout in milliseconds. If not passed, the hideTimeout option value is used by default.
.poshytip('update', content, [ dontOverwriteOption ] )
Update the tooltip content at any time.
content '[title]', 'string', element, function(updateCallback){...}, jQuery
dontOverwriteOption (optional) A boolean flag - if set to true the content will be updated just temporary while the tooltip is active. The next time it is shown again, the default content will be used.
Examples:
- Update content permanently (works even when the tooltip is not active):
.poshytip('update', 'New permanent content')
- Update content temporary while the tooltip is active:
.poshytip('update', 'Temporary content', true)
.poshytip('disable')
Disable the tooltip.
.poshytip('enable')
Enable the tooltip.
.poshytip('destroy')
Destroy completely the tooltip functionality.

Notes

License

Like jQuery, Poshy Tip is dual licensed under the MIT and GPL licenses.

Download

Download link: http://vadikom.com/files/?file=poshytip/poshytip-1.2.zip

Git

The Poshy Tip source code is also available at GitHub:

git clone git://github.com/vadikom/poshytip.git

Support

Post your questions/suggestions in the support forums.

If you appreciate this script, you can support me by donating a small amount through PayPal or just by spreading the word about it. Your support is highly appreciated!