【问题标题】:Rotating Icons Using Javascript position使用 Javascript 位置旋转图标
【发布时间】:2020-11-27 03:01:25
【问题描述】:

我想使用 javascript 动态定位项目,但我真的不知道该怎么做没错,但是当您需要添加更多项目时使用 css,您必须添加更多 css 代码并静态定位,代码如下:

body {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  background: white;
}

.container-circle {
  text-align: center;
  position: relative;
  height: 250px;
  width: 250px;
  border-radius: 50%;
  transition: all 1s ease;
}

.container-circle .container-circle-img {
  height: 250px;
  width: 250px;
  border-radius: 50%;
  object-fit: cover;
}

.container-circle::before {
  content: '';
  position: absolute;
  top: -3;
  left: -3;
  right: -3;
  bottom: -3;
  border-radius: 50%;
  z-index: -1;
  background-image: linear-gradient(180deg, red, blue, yellow, green);
  animation: rotate 3s linear infinite;
}

.container-circle-icons {
  text-align: center;
  position: absolute;
  content: '';
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: -1;
  border-radius: 60%;
  animation: rotate 10s linear infinite;
  padding: 0;
  margin: 0;
}

.container-circle-icons .ico {
  width: 50px;
  height: 50px;
}

.container-circle-icons .ico1 {
  content: '';
  position: absolute;
  top: -20%;
  left: 35%;
}

.container-circle-icons .ico2 {
  content: '';
  position: absolute;
  top: 50%;
  right: -30%;
}

.container-circle-icons .ico3 {
  content: '';
  position: absolute;
  left: 35%;
  bottom: -30%;
}

.container-circle-icons .ico4 {
  content: '';
  position: absolute;
  top: 50%;
  left: -60%;
}

@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
<div class="container-circle">
  <img class="container-circle-img" src="picture.jpg" alt="pic1">
  <div class="container-circle-icons">
    <img src="icon1.png" class="ico ico1" alt="">
    <img src="icon2.png" class="ico ico2" alt="">
    <img src="icon3.png" class="ico ico3" alt="">
    <img src="icon4.png" class="ico ico4" alt="">
  </div>
</div>

【问题讨论】:

  • 你的问题是什么?
  • 我的问题是我如何定位例如一个圆圈,然后在顶部放置一个图标,在底部放置一个图标,在不静态添加 css 代码的情况下使用 javascript 将图标放置在底部和 1 左右,因为 javascript 可以帮助动态定位它获取位置绝对值并在正确的位置添加图标,而无需每次想要添加更多图标时计算添加和缩放图标。
  • “旋转”是什么意思?旋转一个角度或在它们的位置之间移动?
  • 在它们的位置之间移动,我只是想知道如何使用 javascript 将这些图标定位在左上角右下角,我会让它们使用 css 旋转。

标签: javascript html jquery css sass


【解决方案1】:

元素的位置最终将由 CSS 决定,但如果您想动态添加元素并移动它们,您可以使用 javascript 设置该 CSS。

假设您使用id='test' 插入一个新元素 您可以通过以下方式动态更改其位置:

const newElement = document.getElementById('test');
element.style.left = '150%' // whatever value you want
element.style.top= '75px' // make sure you include units

【讨论】:

    猜你喜欢
    • 2011-01-21
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 2012-10-18
    • 2019-10-13
    • 1970-01-01
    相关资源
    最近更新 更多