使用Jquery如何通过鼠标悬停来移除display none(CSS)?

huangapple go评论142阅读模式
英文:

How do I Remove(using a mouseover) a display none(CSS) with Jquery?

问题

我正在制作一个卡片NFT项目,以进行练习,当我鼠标悬停在上面时,应该从HTML类“nft”中移除display:none。我尝试了很多方法,包括仅使用CSS,但没有取得结果。如果有人可以帮助我或提供解释,我将非常感激。

  1. <!-- begin snippet: js hide: false console: true babel: false -->
  2. <!-- language: lang-js -->
  3. $(document).ready(function(){
  4. $('.nft').on('mouseover', function() {
  5. $(this).toggle();
  6. });
  7. });
  8. <!-- language: lang-css -->
  9. .img-text{
  10. display: flex;
  11. flex-direction: column;
  12. justify-content: space-between;
  13. margin: 10px 25px;
  14. }
  15. .nft{
  16. width: 250px;
  17. height: 250px;
  18. background-color: cyan;
  19. border-radius: 15px;
  20. position: absolute;
  21. display: none;
  22. opacity: .5;
  23. cursor: pointer;
  24. }
  25. .eye{
  26. position: relative;
  27. top: 100px;
  28. left: 40%;
  29. }
  30. .nft-image{
  31. max-height: 250px;
  32. max-width: 250px;
  33. border-radius: 15px;
  34. }
  35. <!-- language: lang-html -->
  36. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  37. <div class="img-text">
  38. <div class="nft">
  39. <img src="./img/icon-view.svg" alt="" class="eye">
  40. </div>
  41. <img class="nft-image" src="./img/image-equilibrium.jpg" alt="NFT">
  42. <h1 class="heading">Equilibrium #3429</h1>
  43. </div>
  44. <!-- end snippet -->

我尝试使用CSS的:hover伪类、在HTML中添加和移除类以及使用jQuery,但没有取得结果。

谢谢!

英文:

i am making a card nft project to practice where when i mouseover it should remove the display:none from the HTML class="nft" . I tried many things also only using CSS but i didn't achieve the results. If anyone could help me or give a explanation to make help I really apreciate.
HTML:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. $(document).ready(function(){
  2. $(&#39;.nft&#39;).on(&#39;mouseover&#39;, function() {
  3. $(this).toggle();
  4. });
  5. });

<!-- language: lang-css -->

  1. .img-text{
  2. display: flex;
  3. flex-direction: column;
  4. justify-content: space-between;
  5. margin: 10px 25px;
  6. }
  7. .nft{
  8. width: 250px;
  9. height: 250px;
  10. background-color: cyan;
  11. border-radius: 15px;
  12. position: absolute;
  13. display: none;
  14. opacity: .5;
  15. cursor: pointer;
  16. }
  17. .eye{
  18. position: relative;
  19. top: 100px;
  20. left: 40%;
  21. }
  22. .nft-image{
  23. max-height: 250px;
  24. max-width: 250px;
  25. border-radius: 15px;
  26. }

<!-- language: lang-html -->

  1. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
  2. &lt;div class=&quot;img-text&quot;&gt;
  3. &lt;div class=&quot;nft&quot;&gt;
  4. &lt;img src=&quot;./img/icon-view.svg&quot; alt=&quot;&quot; class=&quot;eye&quot;&gt;
  5. &lt;/div&gt;
  6. &lt;img class=&quot;nft-image&quot; src=&quot;./img/image-equilibrium.jpg&quot; alt=&quot;NFT&quot;&gt;
  7. &lt;h1 class=&quot;heading&quot;&gt;Equilibrium #3429&lt;/h1&gt;
  8. &lt;/div&gt;

<!-- end snippet -->

I tried using Hover with CSS, add and removing classes in html and using jquery but no result

Thank you

答案1

得分: 1

  1. 你可能想以另一种方式使其不可见,例如:
  2. &lt;!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false --&gt;
  3. &lt;!-- 语言: lang-js --&gt;
  4. $(document).ready(function () {
  5. let show = true
  6. $(&#39;.nft&#39;).on(&#39;mouseover&#39;, function () {
  7. $(this).css(&#39;background-color&#39;, show ? &#39;cyan&#39; : &#39;&#39;)
  8. show = !show
  9. })
  10. })
  11. &lt;!-- 语言: lang-css --&gt;
  12. .img-text {
  13. display: flex;
  14. flex-direction: column;
  15. justify-content: space-between;
  16. margin: 10px 25px;
  17. }
  18. .nft {
  19. width: 250px;
  20. height: 250px;
  21. border-radius: 15px;
  22. position: absolute;
  23. opacity: 0.5;
  24. cursor: pointer;
  25. }
  26. .eye {
  27. position: relative;
  28. top: 100px;
  29. left: 40%;
  30. }
  31. .nft-image {
  32. max-height: 250px;
  33. max-width: 250px;
  34. border-radius: 15px;
  35. }
  36. &lt;!-- 语言: lang-html --&gt;
  37. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js&quot;&gt;&lt;/script&gt;
  38. &lt;div class=&quot;img-text&quot;&gt;
  39. &lt;div class=&quot;nft&quot;&gt;
  40. &lt;img src=&quot;./img/icon-view.svg&quot; alt=&quot;&quot; class=&quot;eye&quot; /&gt;
  41. &lt;/div&gt;
  42. &lt;img class=&quot;nft-image&quot; src=&quot;./img/image-equilibrium.jpg&quot; alt=&quot;NFT&quot; /&gt;
  43. &lt;h1 class=&quot;heading&quot;&gt;Equilibrium #3429&lt;/h1&gt;
  44. &lt;/div&gt;
  45. &lt;!-- 结束代码片段 --&gt;
英文:

You'd want to make it invisible in another way such as this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
$(document).ready(function () {
let show = true
$('.nft').on('mouseover', function () {
$(this).css('background-color', show ? 'cyan' : '')
show = !show
})
})

<!-- language: lang-css -->
.img-text {
display: flex;
flex-direction: column;
justify-content: space-between;
margin: 10px 25px;
}

.nft {
width: 250px;
height: 250px;
border-radius: 15px;
position: absolute;
opacity: 0.5;
cursor: pointer;
}

.eye {
position: relative;
top: 100px;
left: 40%;
}
.nft-image {
max-height: 250px;
max-width: 250px;

border-radius: 15px;
}

<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="img-text">
<div class="nft">
<img src="./img/icon-view.svg" alt="" class="eye" />
</div>
<img class="nft-image" src="./img/image-equilibrium.jpg" alt="NFT" />
<h1 class="heading">Equilibrium #3429</h1>
</div>

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月10日 19:18:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定