在进入正题之前,先提提 img 标签的 alt 属性和 title 属性。alt 属性是规定在图像无法显示时的替代文本,在 IE6 和 IE7 下,当鼠标移动到 img 元素上时,会显示 alt 属性的值,这种行为并不正确,只有当图像无法显示时,才会显示出替代文本;而 title 属性是规定关于元素的额外信息,这些信息在鼠标移到元素上时显示一段提示文本,即 Tooltip。
以前没没注意 img 标签的 title 属性,经常性不加,现在要改正。好了,回到正题。这个方法是按照本人的喜欢而写的,可能有点蛋疼,囧。先看 Demo,然后是我的思路:
- 遍历图片;
- 判断图片是否存在
title属性; - 在图片后面插入一个用于显示标题
title的元素,并把它们用span标签包起来,因为想要把标题显示在图片上方; - 计算图片的左边距离(包括左内、外边距和左边框宽度)和下边距离,用来作为标题容器的
left值和bottom值; - 获取图片的宽度,让标题显示的宽度与图片宽度一致;
- 设置标题容器的透明度并隐藏;
- 最后设置鼠标滑入滑出效果。
然后是代码,CSS 代码可以查看 Demo 文件:
$(window).load(function() {
//遍历图片;
$('#content img').each(function(index) {
//如果图片存在 title 属性;
if (this.title) {
//赋值 imgTitle 为图片的 title;
var imgTitle = this.title;
//去除图片 title 的值;
this.title = '';
//用一个 span 标签把图片包起来,并在图片后面插入一个放置图片 title 的 p 元素;
$(this).wrap('<span id="img_wrapper"></span>').after('<p id="tooltip">'+imgTitle+'</p>');
//图片左内边距值;
var imgPaddingLeft = parseFloat($(this).css('paddingLeft'), 10);
//获取图片左边框值;
var imgBorderLeft = parseFloat($(this).css('borderLeftWidth'), 10);
//获取图片左外边距值;
var imgMarginLeft = parseFloat($(this).css('marginLeft'), 10);
//获取图片下内边距值;
var imgPaddingBottom = parseFloat($(this).css('paddingBottom'), 10);
//获取图片下边框值;
var imgBorderBottom = parseFloat($(this).css('borderBottomWidth'), 10);
//获取图片下外边距值;
var imgMarginBottom = parseFloat($(this).css('marginBottom'), 10);
//赋值、获取标题的 left 属性值;
var imgPositionLeft = imgPaddingLeft + imgBorderLeft + imgMarginLeft + 'px';
//赋值、获取标题的 bottom 属性值;
var imgPositionBottom = imgPaddingBottom + imgBorderBottom + imgMarginBottom +'px';
//如果图片有浮动,则把浮动去除并转移到父元素 #img_wrapper 上;
var imgFloat = $(this).css('float');
var $img_wrapper = $(this).parent('#img_wrapper');
if (imgFloat !== 'none') {
$(this).css('float', 'none');
$img_wrapper.css({'float': imgFloat});
}
//获取图片的宽度;
var imgWidth = $(this).width() + 'px';
//赋值、获取标题的最高高度;
var tipHeight = parseFloat($(this).height(), 10);
tipHeight *= 0.5;
var $tooltip = $(this).next('#tooltip');
//设置标题 p 元素的显示位置、宽度、透明度,并隐藏;
$tooltip
.css({'max-height': tipHeight, 'width': imgWidth, 'left': imgPositionLeft, 'bottom': imgPositionBottom})
.fadeTo('opacity', 0.8).hide();
//鼠标滑过 #img_wrapper 时显示 title,滑出则隐藏。
$img_wrapper.hover(function() {
$tooltip.fadeIn('slow');
}, function() {
$tooltip.fadeOut('slow');
});
}
});
});
//遍历图片;
$('#content img').each(function(index) {
//如果图片存在 title 属性;
if (this.title) {
//赋值 imgTitle 为图片的 title;
var imgTitle = this.title;
//去除图片 title 的值;
this.title = '';
//用一个 span 标签把图片包起来,并在图片后面插入一个放置图片 title 的 p 元素;
$(this).wrap('<span id="img_wrapper"></span>').after('<p id="tooltip">'+imgTitle+'</p>');
//图片左内边距值;
var imgPaddingLeft = parseFloat($(this).css('paddingLeft'), 10);
//获取图片左边框值;
var imgBorderLeft = parseFloat($(this).css('borderLeftWidth'), 10);
//获取图片左外边距值;
var imgMarginLeft = parseFloat($(this).css('marginLeft'), 10);
//获取图片下内边距值;
var imgPaddingBottom = parseFloat($(this).css('paddingBottom'), 10);
//获取图片下边框值;
var imgBorderBottom = parseFloat($(this).css('borderBottomWidth'), 10);
//获取图片下外边距值;
var imgMarginBottom = parseFloat($(this).css('marginBottom'), 10);
//赋值、获取标题的 left 属性值;
var imgPositionLeft = imgPaddingLeft + imgBorderLeft + imgMarginLeft + 'px';
//赋值、获取标题的 bottom 属性值;
var imgPositionBottom = imgPaddingBottom + imgBorderBottom + imgMarginBottom +'px';
//如果图片有浮动,则把浮动去除并转移到父元素 #img_wrapper 上;
var imgFloat = $(this).css('float');
var $img_wrapper = $(this).parent('#img_wrapper');
if (imgFloat !== 'none') {
$(this).css('float', 'none');
$img_wrapper.css({'float': imgFloat});
}
//获取图片的宽度;
var imgWidth = $(this).width() + 'px';
//赋值、获取标题的最高高度;
var tipHeight = parseFloat($(this).height(), 10);
tipHeight *= 0.5;
var $tooltip = $(this).next('#tooltip');
//设置标题 p 元素的显示位置、宽度、透明度,并隐藏;
$tooltip
.css({'max-height': tipHeight, 'width': imgWidth, 'left': imgPositionLeft, 'bottom': imgPositionBottom})
.fadeTo('opacity', 0.8).hide();
//鼠标滑过 #img_wrapper 时显示 title,滑出则隐藏。
$img_wrapper.hover(function() {
$tooltip.fadeIn('slow');
}, function() {
$tooltip.fadeOut('slow');
});
}
});
});
这里使用 $(window).load() ,因为需要图片完全加载后才能计算出图片宽度值,所以不用 $(document).ready();另外一个考虑是,当 img 有浮动的时候,去掉其浮动,然后给其父元素 span 添加浮动;还有一个考虑是当图片加载失败或不存在时的处理,这个死活折腾不出来,囧rz,暂时不管了,就算加载失败多个提示也没什么,但 firefox 3.6 下却连 alt 也不显示了,原因不明。
其实还有另一个简单的 Tooltip 方法,下次再写吧。

Demo地址错误
@Bee君: 忘了放链接了,汗。
Demo又错鸟。。。
@keke: 习惯性?囧
Demo 链接错了。。。
如果是在 WP 后台上传图片的话,那一定会有 title 属性,我一般都在后台上传图片,呵呵!
@Max Lee: 嗯,修复了。其实个人很少弄图片。这个适合图片展示的童鞋。
demo在哪= =
@snowxh: 已经修复,囧。
Demo修复了麻烦@一下。
@Yafei: 修复链接了,~~o(>_<)o ~~
这个demo在哪里?
@万戈: 唉,链接已好了。
提醒一下。。图片加载失败会触发onerror事件哦。。。
@QiQiBoY: 唉,其实是触发了,看会 jQuery API,是 IE,它让我很烦恼。
呃,技术博。。。。
@柳亚: 渣技术,有待提高。献丑了
呵呵,啊哦!这个不错啊!
@joyla: 这个还要改进一下
不错,学习啦。
@ForRSS: 互相学习
看了demo,果然好啊,正好有个想做个娱乐博客,可能用到,搜藏。
@阿七: 这个还需要改进一下,然后再发个最常用的简易版
@Alan: 继续关注Ing…现在进你博客好慢。。
很COOL啊~~支持加进主题。。。哈哈
@闲云: 加进主题不大好,必定会有人喜欢有人不喜欢的。你可以自己加的
我们真折腾,什么都要折腾下,乐在其中
@zwwooooo: 哈哈,你是折腾中的前辈。
擦擦擦,居然还有一篇啊,这是第一篇啊,我了个去啊。