【发布时间】:2019-01-16 02:08:14
【问题描述】:
是否可以将鼠标悬停在某些文本上并显示图像?一旦您的鼠标离开文字,图像就会消失?
<p>I have a word, when I hover over it, I see an image </p>
当您将鼠标放在“image”一词上时,它会显示“image1.jpg”
【问题讨论】:
是否可以将鼠标悬停在某些文本上并显示图像?一旦您的鼠标离开文字,图像就会消失?
<p>I have a word, when I hover over it, I see an image </p>
当您将鼠标放在“image”一词上时,它会显示“image1.jpg”
【问题讨论】:
这可以用纯 HTML 来完成:
<a href="#">Hover over me<img src="whatever.gif" /></a>
和 CSS:
a img { display:none; }
a:hover img { display:block; }
【讨论】:
因为您希望通过将鼠标悬停在未包含在元素中的一段文本上来实现功能,所以这变得有点复杂,但无论如何都不是不可能的。以下内容采用您的“简单”html,并在您希望具有 hover 功能的文本周围创建 span 标签:
$('p').each(
function(){
var text = $(this).text();
var spanText = text.replace('image','<span class="imgText">image</span>');
$(this).html(spanText);
});
$('.imgText').live(
'mouseenter',
function(){
$('<img src="path/to/img.png" />')
.appendTo($(this));
});
$('.imgText').live(
'mouseleave',
function(){
$(this).find('img').remove();
});
【讨论】:
$('.yourTextClass').hover(function() {
// mouseOver function
$('#yourImg').show();
}, function() {
// mouseOut function
$('#yourImg').hide();
});
如果你想要一些基本的动画,你也可以使用fadeIn()和fadeOut来代替show和hide。
【讨论】:
【讨论】:
类似this:
HTML:
<p id="text">Foo</p>
<div id="image_to_show" style="display:none"><img src="image1.jpg"/></div>
JavaScript:
$('#text').mouseenter(function(){
$('#image_to_show').fadeIn();
}).mouseleave(function(){
$('#image_to_show').fadeOut();
});
【讨论】: