繁体   English   中英

div颜色在.animate()中不变

[英]div colour not changing in .animate()

我试图在每次单击鼠标时随机更改div的颜色,但不能更改颜色:

div.on('mousedown', function () {
    var newColor = '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
    points += 1;
    div.animate({color: newColor}, 2000);
    $('#total-points').text(points);
});

通过调试,我可以确认已设置newColour并在div上调用了animate()

另外,我在本地项目目录<script src="jQueryUI.js"></script>

我怀疑这可能与div's css属性覆盖有关,但并不太确定从这里到哪里。

这是显示问题的JSFiddle

您需要使用backgroundColor而不是color来更改框背景。 我也从CDN而不是本地加载了jquery和jquery UI脚本。

    div.on('mousedown', function () {
    var newColor = '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
    points += 1;
    div.animate({backgroundColor: newColor}, 2000);
    $('#total-points').text(points);
});

我分叉了您的JSFiddle脚本并创建了一个新脚本。 你可以在这里找到

JavaScript 用于更换<div>按下按钮的颜色</div><div id="text_translate"><p>当我的 html 和 JS 代码在同一个文件中时,我有点不确定为什么我的代码似乎不起作用。 当 html 和 JS 分开时,似乎工作正常。 有人可以指出我的错误....我是新手!</p><p> <strong>HTML:</strong></p><pre> &lt;div class="main"&gt; &lt;div class="light"&gt;&lt;/div&gt; &lt;button onclick="chngCol()" id="burn"&gt;Burn!&lt;/button&gt; &lt;/div&gt;</pre><p> <strong>JavaScript:</strong></p><pre> chngCol() { if(document.getElementByClass('light').style.background == "#00ffff") { document.getElementByClass('light').style.background = "#ffff00"; } else if(document.getElementByClass('light').style.background == "ffff00") { document.getElementByClass('light').style.background = "#ff00ff"; } else if(document.getElementByClass('light').style.background == "#ff00ff") { document.getElementByClass('light').style.background = "#00ffff"; } }</pre><p> <strong>CSS:</strong></p><pre> .light{ width: 50px; height: 50px; background-color:#00ffff; }</pre><p> 所有代码都在同一个文档中,带有适当的标签,但是我在第一个 { 调用 chngCol.</p></div>

[英]JavaScript for changing a <div> colour from a button press

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery更改Div的颜色 使用JS更改Div的颜色 使用jQuery更改div的颜色 用变化的内容动画div的高度? 使用 React 中的按钮单击更改 div 颜色样式 使用jQuery更改div的背景色 JavaScript 用于更换<div>按下按钮的颜色</div><div id="text_translate"><p>当我的 html 和 JS 代码在同一个文件中时,我有点不确定为什么我的代码似乎不起作用。 当 html 和 JS 分开时,似乎工作正常。 有人可以指出我的错误....我是新手!</p><p> <strong>HTML:</strong></p><pre> &lt;div class="main"&gt; &lt;div class="light"&gt;&lt;/div&gt; &lt;button onclick="chngCol()" id="burn"&gt;Burn!&lt;/button&gt; &lt;/div&gt;</pre><p> <strong>JavaScript:</strong></p><pre> chngCol() { if(document.getElementByClass('light').style.background == "#00ffff") { document.getElementByClass('light').style.background = "#ffff00"; } else if(document.getElementByClass('light').style.background == "ffff00") { document.getElementByClass('light').style.background = "#ff00ff"; } else if(document.getElementByClass('light').style.background == "#ff00ff") { document.getElementByClass('light').style.background = "#00ffff"; } }</pre><p> <strong>CSS:</strong></p><pre> .light{ width: 50px; height: 50px; background-color:#00ffff; }</pre><p> 所有代码都在同一个文档中,带有适当的标签,但是我在第一个 { 调用 chngCol.</p></div> javascript遍历div更改背景颜色 更改在 HTML5 canvas Adobe Animate 中单击的形状的颜色 JS:更改显示后对div进行动画处理
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM