ボタンを押した後、Javascriptを使って矩形を黄色から白に徐々に変えようとしています。残念ながら私のコードは動作しません。コードの何が間違っているのか、それを修正する方法を教えてください。徐々に消えてJSを使用して色を付ける
私はちょうどJavascriptを勉強し始めました。申し訳ありませんが、この質問がばかだと。前もって感謝します。
これは私のコードです:
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas> // create canvas to work with
<button onclick="fade(ctx)">Change the colour!</button>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d"); //set context
ctx.rect(20, 20, 150, 100); // draw a rectangle
ctx.stroke(); // with border
ctx.fillStyle="#FFFF00"; // fill with yellow
ctx.fillRect(20,20,150,100);
function fade(ctx) { // fade function responsible for changing colour
var dom = getElementById(ctx), level = 1; // new object based on rectangle object, initial iterator is set to 1
function step() { // inner step function
var h = level.toString(16);
dom.fillStyle = '#FFFF' + h + h; // construct a new colour using h variable
if (level < 15) {
level += 1;
setTimeout(step, 100); // do this after every 100 ms
}
}
setTimeout(step, 100);
}
</script>
</body>
</html>
なぜCSSを使用しないのですか? –
私はJavascriptを勉強しようとしており、内部の関数とタイムアウトを処理する方法を学びたいと思っています。色を徐々に変えることがCSSを使ってどのようにできるのかよく分かりません。 –
ここのCSSのみ:http://codepen.io/impressivewebs/pen/zohgt –