微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何为正方形的点生成随机位置?

如何解决如何为正方形的点生成随机位置?

我必须制作一个灰色的Square,其中包含多个带有特定类别的点。当我单击类别1按钮时,将仅显示cat1类的点。如果用户单击全部,所有点将可见。 Square应该是敏感的,可以在手机上滑动(例如地图或其他内容)。圆点是空跨度。

.plan__category-btns {
  display:flex;
  list-style: none;
}
.plan__category-btns li {
  margin: 10px;
  
}

.plan__dot {
  display:block; 
  width:5px; 
  height:5px;
  border-radius: 50%;
}
.plan__dot.cat1 {
  background: red;
}
.plan__dot.cat2 {
  background: green;
}
.plan__dot.cat3 {
  background: yellow;
}
.plan__dot.cat4 {
  background: blue;
}

.square {
  width: 100%;
  height: 400px;
  background: #eaeaea;
}
<section id='plan'>
  <ul class='plan__category-btns'>
      <li>
          <button class='plan__all'>All</button>
      </li>
      <li>
          <button class='plan__one'>Category 1</button>
      </li>
      <li>
          <button class='plan__two'>Category 2</button>
      </li>
      <li>
          <button class='plan__three'>Category 3</button>
      </li>
      <li>
          <button class='plan__four'>Category 4</button>
      </li>
  </ul>

</section>

<div class="square">
      <span class='plan__dot cat1'></span>
      <span class='plan__dot cat1'></span>
      <span class='plan__dot cat1'></span>
      <span class='plan__dot cat2'></span>
      <span class='plan__dot cat2'></span>
      <span class='plan__dot cat2'></span>
      <span class='plan__dot cat2'></span>
      <span class='plan__dot cat2'></span>
      <span class='plan__dot cat3'></span>
      <span class='plan__dot cat3'></span>
      <span class='plan__dot cat3'></span>
      <span class='plan__dot cat4'></span>

</div>

解决方法

您将随机显示这样的相关点

const rndBtw = (min,max) => Math.floor(Math.random() * (max - min + 1) + min);

const div = document.querySelector(".square");
const divTop = div.offsetTop;
const divRight = div.offsetWidth
const divBottom = div.offsetTop + div.offsetHeight;

// position - you may need to tweak this - it was done very quickly
[...document.querySelectorAll(".plan__dot")]
.forEach(dot => {
  dot.style.left = rndBtw(0,divRight)+"px";
  dot.style.top = rndBtw(divTop,divBottom)+"px";
});

// show clicked categories
document.getElementById("plan").addEventListener("click",function(e) {
  const tgt = e.target;
  if (tgt.classList.contains("plan")) {
    const cat = tgt.dataset.cat;
    [...document.querySelectorAll(".plan__dot")]
    .forEach(dot => dot.classList.toggle("hide",cat !== "all" && !dot.classList.contains("cat" + cat)));
  }
})
.plan__category-btns {
  display: flex;
  list-style: none;
}

.plan__category-btns li {
  margin: 10px;
}

.plan__dot {
  display: block;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  position: absolute;
}

.plan__dot.cat1 {
  background: red;
}

.plan__dot.cat2 {
  background: green;
}

.plan__dot.cat3 {
  background: yellow;
}

.plan__dot.cat4 {
  background: blue;
}

.square {
  width: 100%;
  height: 400px;
  background: #eaeaea;
}

.hide {
  display: none;
}
<section id='plan'>
  <ul class='plan__category-btns'>
    <li>
      <button class='plan' data-cat='all'>All</button>
    </li>
    <li>
      <button class='plan' data-cat='1'>Category 1</button>
    </li>
    <li>
      <button class='plan' data-cat='2'>Category 2</button>
    </li>
    <li>
      <button class='plan' data-cat='3'>Category 3</button>
    </li>
    <li>
      <button class='plan' data-cat='4'>Category 4</button>
    </li>
  </ul>

</section>

<div class="square">
  <span class='plan__dot cat1'></span>
  <span class='plan__dot cat1'></span>
  <span class='plan__dot cat1'></span>
  <span class='plan__dot cat2'></span>
  <span class='plan__dot cat2'></span>
  <span class='plan__dot cat2'></span>
  <span class='plan__dot cat2'></span>
  <span class='plan__dot cat2'></span>
  <span class='plan__dot cat3'></span>
  <span class='plan__dot cat3'></span>
  <span class='plan__dot cat3'></span>
  <span class='plan__dot cat4'></span>

</div>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。