简体   繁体   English

仅在必要时使用jquery隐藏和显示文本

[英]Hiding and showing the text only when it is necessary with jquery

I have a link and some text. 我有一个链接和一些文字。 Whenever I hover over the link the text should be displayed with a time interval of 1200 secs and should be hidden immediately when I remove the cursor from the link. 每当我将鼠标悬停在链接上时,文本应以1200秒的时间间隔显示,并且当我从链接中移除光标时应立即隐藏文本。 So, according to what I have written, whenever I hover over the link the text is getting displayed after 1200 secs and after the text got displayed when I remove the cursor from the link the text is getting hidden which is fine. 因此,根据我所写的内容,每当我将鼠标悬停在链接上时,文本将在1200秒后显示,并且在文本显示后,当我从链接中删除光标时,文本被隐藏,这很好。 but whenever I keep the cursor on the link and remove the cursor from the link before the text is getting displayed, the text is still being shown, which I don't want to be shown. 但每当我将光标放在链接上并在文本显示之前从链接中删除光标时,文本仍然显示,我不想显示。 The text should be hidden immediately when I remove the cursor from the link. 当我从链接中删除光标时,应立即隐藏文本。

Is there any way to do that. 有没有办法做到这一点。 I have provided the code that I have written below: Please take a look at it, Thanks in advance :) 我提供了下面编写的代码:请看一下,提前谢谢:)

 $('a').hover(function(){ setTimeout(function(){ $('.show-hide').css("visibility", "visible")}, 1200); }, function(){ $('.show-hide').css("visibility", "hidden"); }); 
 .show-hide{ visibility : hidden; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#"><p> Hover here </p></a> <p class="show-hide"> This should be shown when hovered </p> <p class="show-hide"> This should be shown when hovered </p> 

It is happening because the show() function executes when you hover over it and even when you leave the mouse before 1.2 seconds, you are not preventing it, it will show the text once the delay is completed. 它发生的原因是当你将鼠标悬停在它上面时执行show()函数,即使你在1.2秒之前离开鼠标,你也没有阻止它,它会在延迟完成后显示文本。

You need to execute hide() when mouseleave . 您需要在mouseleave时执行hide() that way it will stop the show() function. 这样它就会停止show()函数。 Try the snippet below: 试试下面的代码:

 $(document).ready(function(){ $('.show-hide').hide(); //This is for showing the text with delay 1.2 sec $('a').mouseenter(function(){ $('.show-hide').show(1200); }); //This is for hiding the text with no delay $('a').mouseleave(function(){ $('.show-hide').hide(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#"><p>Hover here</p></a> <p class="show-hide"> This should be shown when hovered </p> <p class="show-hide"> This should be shown when hovered </p> 

You can just use normal css transition to achieve this effect. 您可以使用正常的css转换来实现此效果。 try using this code, 尝试使用此代码,

 a:hover ~ .show-hide{ visibility:visible; tranistion:all ease-out; transition-delay:1.2s; } .show-hide{ visibility:hidden; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#"><p> Hover here </p></a> <p class="show-hide"> This should be shown when hovered </p> <p class="show-hide"> This should be shown when hovered </p> 

Can you check this. 你能看看吗 I just taken one flag for keeping visibility. 我只拿了一面旗子来保持知名度。

var isDivShow = "";
$('a').hover(function(){
    isDivShow = "visible";
    setTimeout(function(){
    $('.show-hide').css("visibility", isDivShow)}, 1200);
}, 
function(){
  isDivShow = "hidden";
   $('.show-hide').css("visibility", isDivShow);
});

.show-hide{
  visibility : hidden;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#"><p> Hover here </p></a>


<p class="show-hide"> This should be shown when hovered </p>

<p class="show-hide"> This should be shown when hovered </p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM