从兄弟姐妹中删除类时切换类

时间:2016-06-22 16:54:24

标签: javascript jquery addclass removeclass toggleclass

我有一张地图,当你点击弹出工具提示的点/位置时,我正在切换一个类。我遇到的问题是,当我点击另一个点时,其他兄弟工具提示不会消失。我尝试通过单击删除兄弟姐妹的类来解决这个问题,但是当我这样做时,切换停止工作,我不能再次点击该点来摆脱活动的工具提示。

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

我希望我解释说得对。这是一个codepen:http://codepen.io/anon/pen/BzQrLV

DEVICE_TRACKING
$('.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%;
}

2 个答案:

答案 0 :(得分:1)

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



$('.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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

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

  $('.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');
    }
  });