简体   繁体   English

div颜色在.animate()中不变

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

I am trying to change the colour of a div randomly on each mouse click but its not changin colour: 我试图在每次单击鼠标时随机更改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);
});

Through debugging I can confirm that newColour has been set and animate() has been called on the div . 通过调试,我可以确认已设置newColour并在div上调用了animate()

Also I have jQueryUI referenced to its script in the local project directory <script src="jQueryUI.js"></script> 另外,我在本地项目目录<script src="jQueryUI.js"></script>

I suspect that it maybe something to do with the div's css properties overriding but not too sure where to go from here. 我怀疑这可能与div's css属性覆盖有关,但并不太确定从这里到哪里。

Here is the JSFiddle that displays the problem 这是显示问题的JSFiddle

you need to use backgroundColor and not color in order to change the box background. 您需要使用backgroundColor而不是color来更改框背景。 also i loaded jquery and jquery UI scripts from CDN and not locally. 我也从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);
});

I forked your JSFiddle scripts and created a new one. 我分叉了您的JSFiddle脚本并创建了一个新脚本。 You can find it in here 你可以在这里找到

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的颜色 - JQuery changing colour of Div 使用JS更改Div的颜色 - Changing Colour of a Div with JS 使用jQuery更改div的颜色 - Changing colour of a div using jQuery 用变化的内容动画div的高度? - Animate height of div with changing content? 使用 React 中的按钮单击更改 div 颜色样式 - Changing div colour styling with button click in React 使用jQuery更改div的背景色 - Using jQuery to changing background colour of a 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 for changing a <div> colour from a button press javascript遍历div更改背景颜色 - javascript iterate through div changing the background colour 更改在 HTML5 canvas Adobe Animate 中单击的形状的颜色 - Changing colour of the shape clicked in HTML5 canvas Adobe Animate JS:更改显示后对div进行动画处理 - JS: Animate div after changing it's display
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM