Twitter

@carrielis 可以的

jQuery 给图片添加标题提示

in Javascript // 28 Comments

在进入正题之前,先提提 img 标签的 alt 属性和 title 属性。alt 属性是规定在图像无法显示时的替代文本,在 IE6 和 IE7 下,当鼠标移动到 img 元素上时,会显示 alt 属性的值,这种行为并不正确,只有当图像无法显示时,才会显示出替代文本;而 title 属性是规定关于元素的额外信息,这些信息在鼠标移到元素上时显示一段提示文本,即 Tooltip

以前没没注意 img 标签的 title 属性,经常性不加,现在要改正。好了,回到正题。这个方法是按照本人的喜欢而写的,可能有点蛋疼,囧。先看 Demo,然后是我的思路:

  1. 遍历图片;
  2. 判断图片是否存在 title 属性;
  3. 在图片后面插入一个用于显示标题 title 的元素,并把它们用 span 标签包起来,因为想要把标题显示在图片上方;
  4. 计算图片的左边距离(包括左内、外边距和左边框宽度)和下边距离,用来作为标题容器的 left 值和 bottom 值;
  5. 获取图片的宽度,让标题显示的宽度与图片宽度一致;
  6. 设置标题容器的透明度并隐藏;
  7. 最后设置鼠标滑入滑出效果。

然后是代码,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');
        });
    }
    });
});

这里使用 $(window).load() ,因为需要图片完全加载后才能计算出图片宽度值,所以不用 $(document).ready();另外一个考虑是,当 img 有浮动的时候,去掉其浮动,然后给其父元素 span 添加浮动;还有一个考虑是当图片加载失败或不存在时的处理,这个死活折腾不出来,囧rz,暂时不管了,就算加载失败多个提示也没什么,但 firefox 3.6 下却连 alt 也不显示了,原因不明。

其实还有另一个简单的 Tooltip 方法,下次再写吧。

28 responses to jQuery 给图片添加标题提示

  1. Max Lee / September 5, 2010 / Reply

    Demo 链接错了。。。
    如果是在 WP 后台上传图片的话,那一定会有 title 属性,我一般都在后台上传图片,呵呵!

Leave a Reply