【问题标题】:Hide div by clicking another div jQuery通过单击另一个 div jQuery 隐藏 div
【发布时间】:2020-07-29 09:13:05
【问题描述】:

我有一个 div,当单击它时,高度会发生变化,以便其余内容变得可见。 但是我在这个 div 中有一个按钮,我想将其用作“关闭按钮”,将高度更改回原来的值,以便再次隐藏内容,但它不起作用。 代码如下:

$('.scrollClick').click(function() {
  $(this).css("height", "350px");
});
$('.acceptCta').click(function() {
  $('.scrollClick').css("height", "27px");
});
.scrollClick{
  font-family:inherit;
  background-color: #000E52;
  color: #fff;
  text-align: center;
  display: inline-block;
  position: fixed;
  z-index: 9999;
  cursor: pointer;
  right: 20px;
  padding: 5px;
  border-radius: 5px;
  box-shadow: 2px 2px 2px rgba(0,0,0,0.4);
  transition: all 0.5s ease;
  font-size: 16px;
  max-width: 240px;
  box-sizing: border-box;
  text-align: center;
  height: 27px;
  overflow: hidden;
}

.dropContentBox{
  display: inline-block;
  color: #fff;
  transition: all 0.5s ease;
  text-align: center;
}

.acceptCta{
  display: inline-block;
  position: relative;
  background-color: #7CBD2B;
  color: #333;
  font-size: 14px;
  font-weight: 600;
  padding: 10px 25px;
  box-sizing: border-box;
  border-radius: 3px;
  box-shadow: 2px 2px 2px rgba(0,0,0,0.15);
  transition: all 0.5s ease;
  z-index:10;
}
.acceptCta:hover{
  background-color: #88D41B;
}
.acceptCta:hover .arrow{
  opacity: 1.0;
}

.arrow {
  border: solid #333;
  border-width: 0 3px 3px 0;
  display: inline-block;
  opacity: 0;
  padding: 3px;
  transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  transition: all 0.5s ease;
}

.bouncer {
  position: relative;
  border: solid #fff;
  border-width: 0 3px 3px 0;
  display: inline-block;
  padding: 3px;
  transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  transition: border-color 0.5s ease;
  animation: bouncer 2s infinite;

}

@keyframes bouncer {
  0% {
    bottom: 0px;
  }

  20% {
    bottom: 7px;
  }

  40% {
    bottom: 0px;
  }

  60% {
    bottom: 7px;
  }

  80% {
    bottom: 0px;
  }

  100% {
    bottom: 0px;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="scrollClick">
  Viktig informasjon! Les her&ensp;<i class="bouncer"></i>
  <div class="dropContentBox"><img src="/content/cnm/137449/600x600-banner-include-info-dishwasher-2019-no.png?$banner$">
    <div class="acceptCta">Jeg har lest og forstått&ensp;<i class="arrow"></i></div>
  </div>
    </div>
</div>

正如您在我的 jQuery 代码中看到的,我希望 .scrollClick 类在单击 .acceptCta div 时返回到它的原始高度。 我究竟做错了什么?类型

【问题讨论】:

  • 您的 .scrollClick DIV 环绕 .acceptCta。所以你实际上是在每次点击 .acceptCta 按钮时触发 $('.acceptCta').click 然后 $('.scrollClick').click 。
  • 正如我所怀疑的,谢谢! @MaZoli 为我解决了这个问题,它现在可以按预期完美运行!

标签: javascript html jquery css


【解决方案1】:

问题是点击第二个按钮也会触发点击第一个按钮,因为第二个按钮第一个按钮内。您可以重组您的 html 或停止传播。

$('.scrollClick').click(function() {
  $(this).css("height", "350px");
});
$('.acceptCta').click(function(e) {
  e.stopPropagation();
  $('.scrollClick').css("height", "27px");
});

【讨论】:

  • 正是我所怀疑的,但我不知道如何解决它。非常感谢!它成功了:D
  • 不客气。最好的做法是稍微重组 html 结构。在“informasjon”周围创建一个 div,并将其作为一个按钮。您可以使用 $(this).parent().toggleClass('active');并在 CSS 中设置高度。
  • 那么我必须将该类移到 .scrollClick div 之外,或者?因为单击第一次单击时会出现一个图像和一个按钮。您的意思是将所有内容作为一个按钮?使用它不会有任何问题,因为它在您的解决方案中,对吧?因为我不知道如何处理您的第二个建议:D 这个 div 仅在移动设备上可见,如果这有什么不同的话。这张图片在移动屏幕上被推得很远,其中包含我希望每个人都知道的重要信息,这就是为什么我制作了一个必须按下才能关闭它的 cta。
  • 我做了一个小提琴,所以它可能更容易理解。 jsfiddle.net/nvxykgu9/1
  • 不用担心。不客气……如果你继续学习,你也会很快掌握 jQuery 和 Javascript!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-24
  • 2014-10-28
  • 2015-10-15
  • 2016-11-27
相关资源
最近更新 更多