【问题标题】:Changing the CSS of a div with classname using JavaScript使用 JavaScript 更改具有类名的 div 的 CSS
【发布时间】:2018-11-26 19:12:18
【问题描述】:

我想使用 JavaScript 函数更改我的 CSS 类的动画属性。我一直在尝试各种方法,但都没有奏效。请找出我哪里出错了。

CSS:

   .center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 200px;
  height: 200px;
  border-radius: 50%;
  background: transparent;
}

.center .earth {
  position: absolute;
  top: 0;
  width: 200px;
  height: 200px;
  background-image: url(https://www.publicdomainpictures.net/pictures/90000/velka/earth-map.jpg);
  margin: 3em auto;
  border-radius: 50%;
  background-size: 630px;
  animation: spin 30s linear alternate infinite;
  box-shadow: inset 20px 0 80px 6px rgba(0, 0, 0, 1);
  color: #000;
}

.center .earth .moon {
  position: absolute;
  top: calc(50% - 1px);
  left: 50%;
  width: 200px;
  height: 2px;
  transform-origin: left;
  border-radius: 50%;
  animation: rotate 10s linear infinite;
}

.center .earth .moon::before {
  content: '';
  background-image: url(https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRsvjrANMGI8aBJSFbsHteVa04rcB1IjjNsbrhm8vTLflfpiG133g);
  position: absolute;
  background-size: 200px;
  top: -25px;
  right: 0;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  animation: spin 10s linear infinite;
}

@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@keyframes spin {
  100% {
    background-position: 100%;
  }
}

HTML:

<div className="center">
      <div className="earth">
        <div className="moon" />
      </div>
    </div>

JavaScript 函数:

    function change() {
  var elem = document.getElementsByClassName('moon');
  elem.style.animation="rotate 5s linear infinite";
}

我尝试了各种方法,但没有一个能够执行修改。遍历对象也没有帮助。

【问题讨论】:

  • className 不是有效的 HTML 属性。您可能打算改为写&lt;div class="moon"&gt;&lt;/div&gt;
  • @cfillion 抱歉,我忘了说我正在使用 React。

标签: javascript html css css-selectors


【解决方案1】:

elem 是元素的 HTMLCollection,而不是单个元素,设置 HTMLCollection 的 style 属性不起作用。

相反,您需要单独更改每个元素的样式:

Array.from(document.getElementsByClassName('moon')).forEach(elem => {
    elem.style.animationDuration = "5s";
});

编辑:

这是一个 sn-p 来表明该方法有效/提供参考:

function change_spin_rate() {
  Array.from(document.getElementsByClassName('spinner')).forEach(elem => {
    elem.style.animationDuration = "1s";
  });
}

document.getElementById("spin_change").onclick = change_spin_rate;
@keyframes spin {
  from {
    transform: rotate(360deg);
  }
  to {
    transform: rotate(0deg);
  }
}

.spinner {
  display: inline-block;
  animation: spin 4s infinite linear;
}

#spin_change {
  padding: 8px 16px;
  display: inline-block;
  font-family: sans-serif;
  cursor: pointer;
  border-radius: 16px;
}

#spin_change:hover {
  background: rgba(0, 64, 255, 0.1);
}
<a class="spinner">?</a><a class="spinner">?</a><a class="spinner">?</a>

<a id="spin_change">Click me to change the spin rate for all spinners.<a>

<a class="spinner">?</a><a class="spinner">?</a><a class="spinner">?</a>

【讨论】:

  • 我添加了一个 sn-p 来演示——如果没有其他错误,它应该可以工作......你确定你的 change() 函数被调用了吗?您能否在 change() 函数 (console.log(elem.style.animationDuration)) 中打印已修改元素的样式规则,并在 Web 检查器中检查它们(右键单击 .moon 元素之一并选择“检查元素”以查看关联的CSS 规则)? JS 样式修改应该出现在元素的style 属性中。
  • 这就是我调用更改函数的方式。
  • 从该评论中不清楚您在onClick 属性中到底有什么,但它应该是onclick="change()"onclick="change" 不起作用,文字文本 onclick={change} 也不起作用。您可以通过在change() 函数定义的开头添加console.log() 并查看它是否显示在控制台中来检查change() 函数是否正在执行。
  • 非常感谢您的回复。它被执行。我尝试了 alert() 并且它有效。我很确定 onClick={change} 是我们在 React JSX 中调用函数的方式。在这里查看整个代码:github.com/avinashredyy/Moon-Orbiting-Earth
【解决方案2】:

试试这个:

function change() {
  let elems = document.getElementsByClassName("moon");
  elems.forEach(elem => elem.style.animation = "rotate 5s linear infinite");
}

【讨论】:

    【解决方案3】:

    document.getElementsByClassName 返回多个元素。您需要遍历它们并将更改应用到每个元素。

    function change() {
      Array.from(document.getElementsByClassName("moon")).forEach((elem) => {
        elem.style.animation = "rotate 5s linear infinite";
      });
    }
    

    document.getElementsByClassName 不返回数组。你必须在上面使用Array.from,然后才能调用Array.prototype.forEach

    顺便说一句,&lt;div className="moon" /&gt; 应该是 &lt;div class="moon" /&gt;,除非你正在编写 React JSX。

    【讨论】:

    • 此函数无法修改动画。是的,我正在写 JSX。
    • 我查看了您的代码:github.com/avinashredyy/Moon-Orbiting-Earth/blob/master/src/…。应该是##onClick={this.change.bind(this)}##.
    • 当我进行此更改时,它给了我一个错误提示“无法读取未定义的属性'绑定'”
    • 对不起。我没注意你在哪里定义了 change 函数。应该是像render这样的类方法。
    猜你喜欢
    • 1970-01-01
    • 2012-09-30
    • 2016-02-08
    • 1970-01-01
    • 2017-09-18
    • 2022-11-01
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    相关资源
    最近更新 更多