Remove active and focus from element on second click

I am trying to make an interactive map with information about this. When you click on a point, it changes and shows some contacts. This is achieved by the fact that the element receives pseudo-classes :activeand :focus.

Is there a way to remove pseudo-classes from an element when it is clicked a second time? In fact, is it possible to close an item when pressed again?

.distribution-map {
  position: relative;
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  margin: 0 auto;
}
.distribution-map .map-point {
  cursor: pointer;
  outline: none;
  top: 20px;
  position: absolute;
  width: 40px;
  height: 40px;
  border-radius: 20px;
}
.distribution-map .map-point:active,
.distribution-map .map-point:focus {
  margin: 0;
  padding: 0;
  filter: opacity: 1;
  width: 300px;
  height: 220px;
  color: #e5e5e5;
  z-index: 1;
  -webkit-transition: opacity 0.25s ease-in-out, width 0.25s ease-in-out, height 0.25s ease-in-out;
  transition: opacity 0.25s ease-in-out, width 0.25s ease-in-out, height 0.25s ease-in-out;
}
<div class="distribution-map">
  <button class="map-point">
  </button>
</div>
Run codeHide result
+4
source share
1 answer

This can be achieved with pointer-events: none;

By adding pointer-events: none;to a focused element, you can stop the mouse from interacting with the element so that it does not focus on the next click.

The following changes are required:

  • pointer-events: none; .distribution-map .map-point:active, .distribution-map .map-point:focus

.distribution-map {
  position: relative;
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  margin: 0 auto;
}
.distribution-map .map-point {
  cursor: pointer;
  outline: none;
  top: 20px;
  position: absolute;
  width: 40px;
  height: 40px;
  border-radius: 20px;
}
.distribution-map .map-point:active,
.distribution-map .map-point:focus {
  margin: 0;
  padding: 0;
  filter: opacity: 1;
  width: 300px;
  height: 220px;
  color: #e5e5e5;
  z-index: 1;
  -webkit-transition: opacity 0.25s ease-in-out, width 0.25s ease-in-out, height 0.25s ease-in-out;
  transition: opacity 0.25s ease-in-out, width 0.25s ease-in-out, height 0.25s ease-in-out;
  pointer-events: none;
}
<div class="distribution-map">
  <button class="map-point">
  </button>
</div>
Hide result
+3

Source: https://habr.com/ru/post/1654785/


All Articles