jQuery悬停:淡入隐藏的div,同时淡出“默认”div 一
$(function() {
$('#wrap').hover(
function() {
$('#wrap .image').fadeOut(100, function() {
$('#wrap .text').fadeIn(100);
});
},
function() {
$('#wrap .text').fadeOut(100, function() {
$('#wrap .image').fadeIn(100);
});
}
);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="wrap">
<div class="image">
<img src="http://example.com/images/image.png">
</div>
<div class="text hide">
<p>Text text text</p>
</div>
</div>
我有两个 div(其中一个用 CSS 隐藏),它们在悬停时在公共空间内交替淡入和淡出。
我正在应用这个 jQuery 代码来淡出图像 - 并在悬停时淡入文本,
但问题是文本 div 变得粘稠并且不会淡出 - 总是鼠标移动太快。
你知道在哪里可以解决这个问题吗?
我设置了一个在线测试: http://paragraphe.org/stickytext/test.html
感谢您的任何提示
$(function() {
$('#wrap').hover(
function() {
$('#wrap .image').fadeOut(100, function() {
$('#wrap .text').fadeIn(100);
});
},
function() {
$('#wrap .text').fadeOut(100, function() {
$('#wrap .image').fadeIn(100);
});
}
);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="wrap">
<div class="image">
<img src="http://example.com/images/image.png">
</div>
<div class="text hide">
<p>Text text text</p>
</div>
</div>
I have two divs (one of them hidden with CSS), which I'm fading in and out in alternance inside a common space, on hover.
And I was applying this jQuery code to fade out the image - and fading in the text, on hover
But the problem is that the text div gets sticky and won't fade out - always that the mouse movement is too quick.
Do you know where can it be the solution to this?
I set up an online test : http://paragraphe.org/stickytext/test.html
Thanks for any tip
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您的包装器上没有宽度和高度,您可能会得到一些奇怪的结果,因为一旦图像元素被删除,它就会缩小。 要查看,请在包装器周围添加边框和固定高度/宽度。 或者至少对图像和文本 div 使用相同的高度。
编辑
删除了不适用于您要完成的任务的代码示例。
If your wrapper doesn't have a width AND height on it you may get some strange results as it shrinks once the image element is removed. To see, add a border and fixed height / width around the wrapper. Or at least use the same height for the image and text divs.
EDITED
Removed a code example that wasn't applicable to what you're trying to accomplish.
尝试在悬停功能上使用 .stop() ,这将防止您将鼠标快速移动到 div 上的竞争条件
try using .stop() on your hover-out function, which will prevent the race-condition where you rapidly move your mouse over the divs
尝试使用队列:
jQuery 队列是针对每个元素的,所以我在这里要做的是在图像队列下启动文本效果。
让我给你另一个建议。 如果您的目的是将这种效果应用于各种图像,请使用 class 而不是 id。
这样你只需要调用一次。
Try using the queue:
The jQuery queue is per element, so what I'm trying to do here is to launch the text effects under de image queue.
And let me give you another suggestion. If your intention is to apply this effect to various images use class instead of id.
This way you only need to call this once.
您的代码永远不会淡出文本 div。 悬停事件的两个功能都会淡出图像并淡入文本。
Your code never fades out the text div. Both functions of the hover event will fade out the image and fade in the text.
两个
hover
函数中的代码相同。 在第二种情况下你不应该更换选择器吗?You have the same code in both
hover
functions. Shouldn't you replace the selectors in second case?感谢所有提示:
我尝试使用 mouseenter / mouseleave 事件,因为它们似乎详细查找它们正在处理的 div (如此处所示),但没有任何改变。 因此,看到所有常用的 jQuery 指令都没有响应,我已经按照 CSS 的建议进行了检查。
这就是窍门。
我所拥有的:
正在设置一个巨大的空白空间,浏览器将其解释为光标“未保持静止”(我注意到飞越该空白空间使文本能够正确响应)。
所以我改为:
单独使用“p”标签,并将第二个块放置在 div 中,但后面没有任何空白空间。
然后,关于代码片段,它将与 mouseenter / moseleave 和 hover 一起使用:
这是改进的版本 http://paragraphe.org/nice/test.html
thanks for all the tips :
I tried to use the mouseenter / mouseleave events, since they seem to look in detail for the divs they are dealing with (as seen here), but nothing changed. So seeing that all the usual jQuery instructions weren't responding I've checked out as suggested my CSS.
And there was the trick.
What I had :
was setting up a huge, empty space that the browser was interpreting as "not left still" by the cursor (I noticed that overflying that empty space was making the text to respond properly).
So I changed to that :
Which lets the "p" tag alone, and positions the second block up in the div, but without a trace of empty space behind it.
Then, concerning the snippet, it will work as well with mouseenter / moseleave and hover :
This is the improved version http://paragraphe.org/nice/test.html