簡體   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