【问题标题】:How to hide and show bootstrap 4 cards by hovering over navigation menu through css?如何通过 css 将鼠标悬停在导航菜单上来隐藏和显示 bootstrap 4 卡?
【发布时间】:2018-09-24 14:03:31
【问题描述】:

嗨,我试图通过将鼠标悬停在菜单列表上来显示和隐藏卡片的特定部分,我可以使用 css 隐藏卡片,但不能通过 display:block 特定类的 css 属性来显示它...

HTML

导航菜单

<div class="d-flex justify-content-center">   
    <ul class="nav">
    <li class="nav-item">
      <a class="nav-link itemOne"  href="#">Product 1</a>
    </li>
    <li class="nav-item">
      <a class="nav-link itemTwo" href="#">Product 2</a>
    </li>
    </ul>
   </div>

卡片部分

  <div class="card  item1" style="width:12rem;">
  <img class="card-img-top item" src="../image1" alt="Card image cap">
  <div class="card-body item1">
  <h5 class="card-title item1">Card title</h5>
  <p class="card-text  item1">This is a wider card with supporting text 
  below as a natural lead-in to additional content. This content is a little 
  bit longer.</p>
 </div>
</div>
<div class="card item2" style="width:12rem;">
<img class="../image2" alt="Card image cap">
<div class="card-body item2">
 <h5 class="card-title item2">Card title</h5>
 <p class="card-text item2">This is a wider card with supporting text below 
  as a natural lead-in to additional content. This content is a little bit 
  longer. 
 </p>
</div>
</div>

CSS

.item1{
display:none;
 }
.item2{
 display:none;
 }
.itemOne:hover .item1{   //Not displaying item 1
 display:block;
 }
.itemTwo:hover .item2{    //Not displaying item 2
 display:block;
 }

【问题讨论】:

  • 您是否尝试在不使用 javascript 的情况下完成此操作?
  • @Jay Jordan 是的,但是如果你有任何使用 javascript 的解决方案,那将是一个很大的帮助
  • 见下面的小提琴。

标签: javascript jquery html css bootstrap-4


【解决方案1】:

我会使用 jQuery 来完成这个。

CSS:

.item1, .item2 {
  display: none;
}

jQuery

$('.itemOne').hover(function() {
    $('.item1').toggle();
});

$('.itemTwo').hover(function() {
    $('.item2').toggle();
});

这是example

【讨论】:

  • 问题仍然存在,它不显示卡片,但 css 部分正在工作
  • 它是否像您在小提琴中所期望的那样工作,或者也不在那里?在该示例中,我能够让卡片在悬停时显示。
  • 你使用的是什么版本的 bootstrap 和 jQuery?
  • bootstrap -4 和 jQuery 3.3.1
  • 我链接的示例是否像您期望的那样工作?
猜你喜欢
  • 2014-09-21
  • 1970-01-01
  • 1970-01-01
  • 2018-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多