【问题标题】:Jquery - loop through each button to show the hidden divJquery - 遍历每个按钮以显示隐藏的 div
【发布时间】:2022-01-16 15:29:34
【问题描述】:

我有六个带有六个按钮的框,当我悬停每个单独的按钮时,我想显示一个附加到每个框的单独隐藏 div。如您所见,我已经为第一个框完成了它(现在,所有按钮都附加在它上面),但我想知道循环遍历每个单独按钮以显示相应隐藏 div 的理想/正确方法。

$(document).ready(function() {
  $(".hiddenOne, .hiddenTwo, .hiddenThree, .hiddenFour, .hiddenFive, .hiddenSix").hide();
  $(".button").each(function(index) {
    $(".button").hover(function() {
      $(".hiddenOne").fadeIn();
    });
    $(".hiddenOne").mouseleave(function() {
      $(".hiddenOne").fadeOut();
    });
  });
});
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.centerThis {
  text-align: center;
  margin-top: 20px;
}

.center2 {
  text-align: center;
  background-color: red;
}

.container {
  padding-top: 50px;
  display: flex;
  /* establish flex container */
  flex-wrap: wrap;
  /* enable flex items to wrap */
  justify-content: space-around;
}

.button {
  background: red;
  color: white;
  padding: 10px;
}

.buttonTwo {
  background: red;
  color: white;
  padding: 10px;
}

.buttonThree {
  background: red;
  color: white;
  padding: 10px;
}

.buttonFour {
  background: red;
  color: white;
  padding: 10px;
}

.buttonFive {
  background: red;
  color: white;
  padding: 10px;
}

.buttonSix {
  background: red;
  color: white;
  padding: 10px;
}

.white {
  color: white;
  text-align: center;
  padding-top: 50px;
}

.hiddenOne {
  height: 300px;
  padding-top: 20px;
  background-color: red;
  color: white;
}

.hiddenTwo {
  height: 300px;
  background-color: red;
  color: white;
}

.hiddenThree {
  height: 300px;
  background-color: red;
  color: white;
}

.hiddenFour {
  height: 300px;
  background-color: red;
  color: white;
}

.hiddenFive {
  height: 300px;
  background-color: red;
  color: white;
}

.hiddenSix {
  height: 300px;
  background-color: red;
  color: white;
}

.itemWithin {
  flex: 0 35%;
  height: 300px;
  margin-bottom: 2%;
  background-color: grey;
}

.itemWithin2 {
  flex: 0 35%;
  height: 300px;
  background-color: grey;
  margin-bottom: 2%;
}

.itemWithin3 {
  flex: 0 35%;
  height: 300px;
  background-color: grey;
  margin-bottom: 2%;
}

.itemWithin4 {
  flex: 0 35%;
  height: 300px;
  background-color: grey;
  margin-bottom: 2%;
}

.itemWithin5 {
  flex: 0 35%;
  height: 300px;
  background-color: grey;
  margin-bottom: 2%;
}

.itemWithin6 {
  flex: 0 35%;
  height: 300px;
  background-color: grey;
  margin-bottom: 2%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="itemWithin">
    <div class="hiddenOne">
      <h2 class="center2">Mining & Resources</h2>
      <p class="white">
        Here are the details when view more is hovered
      </p>
    </div>

    <h2 class="centerThis">Mining & Resources</h2>
    <button class="button">View More</button>
  </div>

  <div class="itemWithin2">
    <div class="hiddenTwo">
      <h2 class="center2">Defence</h2>
    </div>
    <h2 class="centerThis">Defence</h2>
    <button class="button">View More</button>

  </div>

  <div class="itemWithin3">
    <h2 class="centerThis">Energy & Water</h2>
    <button class="button">View More</button>

  </div>

  <div class="itemWithin4">
    <h2 class="centerThis">Public & Private Infrastucture</h2>
    <button class="button">View More</button>
  </div>

  <div class="itemWithin5">
    <h2 class="centerThis">Commercial & Residential Building</h2>
    <button class="button">View More</button>
  </div>

  <div class="itemWithin6">
    <h2 class="centerThis">Industrial Manufacturing</h2>
    <button class="button">View More</button>
  </div>
</div>

【问题讨论】:

  • 您有多确定 jQuery 是实现这种效果的最佳方式,而不仅仅是 CSS?
  • 我正在开发一个 jquery 解决方案,待命
  • 如果按钮被隐藏的 div 覆盖,您打算如何单击该按钮?
  • 该按钮不是隐藏 Div 的一部分。 @AlexanderNied,可以通过 CSS 做到这一点,但网站的其余部分也使用 JQuery 来制作基本动画,所以这是出于一致性和结构原因。

标签: javascript html jquery css


【解决方案1】:

你可以这样试试:

$(document).ready(function() {
        $(".hiddenOne").hide();

        $(".button").hover(function () {
            $(this).parent().find('.hiddenOne').fadeIn();
        })

        $(".hiddenOne").mouseleave(function () {
            $(this).fadeOut();
        })
    });
* {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
    }

    .centerThis {
        text-align: center;
        margin-top: 20px;
    }

    .center2 {
        text-align: center;
        background-color: red;
    }

    .container {
        padding-top: 50px;
        display: flex;
        /* establish flex container */
        flex-wrap: wrap;
        /* enable flex items to wrap */
        justify-content: space-around;
    }

    .button {
        background: red;
        color: white;
        padding: 10px;
    }
    
    .white {
        color: white;
        text-align: center;
        padding-top: 50px;
    }

    .hiddenOne {
        height: 300px;
        padding-top: 20px;
        background-color: red;
        color: white;
    }
    
    .itemWithin {
        flex: 0 35%;
        height: 300px;
        margin-bottom: 2%;
        background-color: grey;
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
    <div class="itemWithin">
        <div class="hiddenOne">
            <h2 class="center2">Mining & Resources</h2>
            <p class="white">
                Here are the details when view more is hovered
            </p>
        </div>

        <h2 class="centerThis">Mining & Resources</h2>
        <button class="button">View More</button>
    </div>

    <div class="itemWithin">
        <div class="hiddenOne">
            <h2 class="center2">Mining & Resources</h2>
            <p class="white">
                Here are the details when view more is hovered
            </p>
        </div>

        <h2 class="centerThis">Mining & Resources</h2>
        <button class="button">View More</button>
    </div>
</div>

【讨论】:

    猜你喜欢
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多