从兄弟姐妹中删除 class 的同时切换 Class

Toggle Class while removing class from siblings

我有一张地图,当您单击弹出工具提示的 dot/location 时,我会在其中切换 class。我 运行 遇到的问题是,当我单击另一个点时,其他兄弟工具提示不会消失。我试图通过单击时删除兄弟姐妹的 class 来解决此问题,但是当我这样做时,切换停止工作,我无法再次单击该点以摆脱活动工具提示。

我需要当前活动工具提示上的切换仍然有效,但我还需要兄弟工具提示也消失。

希望我解释正确。这是一个代码笔:http://codepen.io/anon/pen/BzQrLV

$('.dot').click(function() {
  $('div.toggle-active').removeClass('toggle-active');
  $(this).next().toggleClass('toggle-active');
});
#map {
  position: relative;
  width: 100%;
  max-width: 580px;
}
#map img {
  max-width: 100%;
}
/** DOTS **/

.dot {
  background-color: #fff;
  border: 1px solid #fff;
  border-radius: 50%;
  cursor: pointer;
  display: inline-block;
  height: 10px;
  position: absolute;
  width: 10px;
}
.dot:hover {
  background-color: #00A24B;
}
.dot-oregon-greshman {
  top: 15%;
  left: 11%;
}
.dot-oregon-oregon-city {
  top: 16.5%;
  left: 11%;
}
/** TOOLTIPS **/

.tooltip::before {
  content: "";
  height: 0;
  width: 0;
  border-style: solid;
  border-width: 12.5px 21.7px 12.5px 0;
  border-color: transparent #01872B transparent transparent;
  position: absolute;
  top: 50%;
  left: -6%;
  transform: translateY(-50%);
}
.tooltip {
  opacity: 0;
  background-color: #01872B;
  color: #fff;
  padding: 10px 10px 10px 20px;
  font-size: 12px;
  width: 186px;
  position: absolute;
  line-height: 14px;
  transition: all 300ms ease-in-out;
}
.tooltip.toggle-active {
  opacity: 1;
}
.tooltip p {
  margin: 3px 0;
}
.tooltip a {
  color: #fff;
}
.tooltip a:hover {
  color: #c3ecff;
  text-decoration: none;
}
.tooltip strong {
  color: #fff;
  font-size: 14px;
}
.tooltip-oregon-greshman {
  top: 10%;
  left: 16%;
}
.tooltip-oregon-oregon-city {
  top: 11.5%;
  left: 17%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class="map-section">
  <div class="map-container">
    <div id="map">
      <img src="http://openpathinvestments.com/wp-content/themes/boilerplate/images/map-blue.png" alt="">
      <div class="locations">
        <div class="dot dot-oregon-greshman"></div>
        <div class="tooltip tooltip-oregon-greshman">
          <strong>Stark Street Crossings</strong>
          <p>Greshman, Oregon 97030</p>
          <p><a href="">Property Profile</a> | <a href="">Website</a>
          </p>
        </div>
        <div class="dot dot-oregon-oregon-city"></div>
        <div class="tooltip tooltip-oregon-oregon-city">
          <strong>The Preserve</strong>
          <p>Oregon City, Oregon 97045</p>
          <p><a href="">Property Profile</a> | <a href="">Website</a>
          </p>
        </div>
      </div>
    </div>
  </div>
</div>

已更新以在显示之前检查工具提示是否已经显示。

  $('.dot').click(function() {
    var displayed = $(this).next().attr('class').match('toggle-active');
    $('div.toggle-active').removeClass('toggle-active');
    if(!displayed){
      $(this).next().toggleClass('toggle-active');
    }
  });

.not($(this).next()) 添加到您的 removeClass 语句中,这样您就不会从所有点中删除活动的 class,只是没有被点击的点。

$('.dot').click(function() {
  $('div.toggle-active').not($(this).next()).removeClass('toggle-active');
  $(this).next().toggleClass('toggle-active');
});
#map {
  position: relative;
  width: 100%;
  max-width: 580px;
}
#map img {
  max-width: 100%;
}
/** DOTS **/

.dot {
  background-color: #fff;
  border: 1px solid #fff;
  border-radius: 50%;
  cursor: pointer;
  display: inline-block;
  height: 10px;
  position: absolute;
  width: 10px;
}
.dot:hover {
  background-color: #00A24B;
}
.dot-oregon-greshman {
  top: 15%;
  left: 11%;
}
.dot-oregon-oregon-city {
  top: 16.5%;
  left: 11%;
}
/** TOOLTIPS **/

.tooltip::before {
  content: "";
  height: 0;
  width: 0;
  border-style: solid;
  border-width: 12.5px 21.7px 12.5px 0;
  border-color: transparent #01872B transparent transparent;
  position: absolute;
  top: 50%;
  left: -6%;
  transform: translateY(-50%);
}
.tooltip {
  opacity: 0;
  background-color: #01872B;
  color: #fff;
  padding: 10px 10px 10px 20px;
  font-size: 12px;
  width: 186px;
  position: absolute;
  line-height: 14px;
  transition: all 300ms ease-in-out;
}
.tooltip.toggle-active {
  opacity: 1;
}
.tooltip p {
  margin: 3px 0;
}
.tooltip a {
  color: #fff;
}
.tooltip a:hover {
  color: #c3ecff;
  text-decoration: none;
}
.tooltip strong {
  color: #fff;
  font-size: 14px;
}
.tooltip-oregon-greshman {
  top: 10%;
  left: 16%;
}
.tooltip-oregon-oregon-city {
  top: 11.5%;
  left: 17%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
HTML

<div class="map-section">
  <div class="map-container">
    <div id="map">
      <img src="http://openpathinvestments.com/wp-content/themes/boilerplate/images/map-blue.png" alt="">
      <div class="locations">
        <div class="dot dot-oregon-greshman"></div>
        <div class="tooltip tooltip-oregon-greshman">
          <strong>Stark Street Crossings</strong>
          <p>Greshman, Oregon 97030</p>
          <p><a href="">Property Profile</a> | <a href="">Website</a>
          </p>
        </div>
        <div class="dot dot-oregon-oregon-city"></div>
        <div class="tooltip tooltip-oregon-oregon-city">
          <strong>The Preserve</strong>
          <p>Oregon City, Oregon 97045</p>
          <p><a href="">Property Profile</a> | <a href="">Website</a>
          </p>
        </div>
      </div>
    </div>
  </div>
</div>