Javascript 使工具提示在每次单击时弹出并淡出
我正在开发一个工具提示函数,我是javascript新手:Javascript 使工具提示在每次单击时弹出并淡出,javascript,jquery,Javascript,Jquery,我正在开发一个工具提示函数,我是javascript新手: $( function() { var targets = $( '[rel~=tooltip]' ), target = false, tooltip = false, title = false; targets.bind( 'click', function() { target = $( this ); tip = t
$( function()
{
var targets = $( '[rel~=tooltip]' ),
target = false,
tooltip = false,
title = false;
targets.bind( 'click', function()
{
target = $( this );
tip = target.attr('title');
tooltip = $( '<div id="tooltip"></div>' );
if( !tip || tip == '' )
return false;
target.removeAttr( 'title' );
tooltip.css( 'opacity', 0 )
.html( tip )
.appendTo( 'body' );
var init_tooltip = function()
{
if( $( window ).width() < tooltip.outerWidth() * 1.5 )
tooltip.css( 'max-width', $( window ).width() / 2 );
else
tooltip.css( 'max-width', 340 );
var pos_left = target.offset().left + ( target.outerWidth() / 2 ) - ( tooltip.outerWidth() / 2 ),
pos_top = target.offset().top - tooltip.outerHeight() - 20;
if( pos_left < 0 )
{
pos_left = target.offset().left + target.outerWidth() / 2 - 20;
tooltip.addClass( 'left' );
}
else
tooltip.removeClass( 'left' );
if( pos_left + tooltip.outerWidth() > $( window ).width() )
{
pos_left = target.offset().left - tooltip.outerWidth() + target.outerWidth() / 2 + 20;
tooltip.addClass( 'right' );
}
else
tooltip.removeClass( 'right' );
if( pos_top < 0 )
{
var pos_top = target.offset().top + target.outerHeight();
tooltip.addClass( 'top' );
}
else
tooltip.removeClass( 'top' );
tooltip.css( { left: pos_left, top: pos_top } )
.animate({ top: '+=10', opacity: 1 }, 50);
tooltip.fadeOut(6000);
};
init_tooltip();
$( window ).resize( init_tooltip );
});
});
$(函数()
{
变量目标=$(“[rel~=tooltip]”),
目标=错误,
工具提示=false,
标题=假;
targets.bind('click',function()
{
目标=$(本);
tip=target.attr('title');
工具提示=$('');
如果(!tip | | tip=='')
返回false;
target.removeAttr('title');
css('opacity',0)
.html(提示)
.appendTo(‘body’);
var init_tooltip=函数()
{
如果($(窗口).width()$(窗口).width())
{
pos_left=target.offset().left-工具提示.outerWidth()+目标.outerWidth()/2+20;
addClass('right');
}
其他的
工具提示.removeClass('right');
如果(位置顶部<0)
{
var pos_top=target.offset().top+target.outerHeight();
addClass('top');
}
其他的
工具提示:removeClass('top');
css({left:pos_left,top:pos_top})
.animate({top:'+=10',不透明度:1},50);
工具提示:淡出(6000);
};
初始化工具提示();
$(窗口)。调整大小(初始工具提示);
});
});
当前,当单击元素时,它会显示,然后淡出。这是理想的行为。但是,您只能在重新加载页面时单击该选项一次。如何使函数在每次单击时显示工具提示和命运?只需快速查看代码,您就可以删除标题
目标。删除标题('title')
。因此,下次单击时没有要显示的标题 只要快速查看一下您的代码,您就可以删除标题target.removeAttr('title')
。因此,下次单击时没有要显示的标题