버튼을 누른 후 자바 스크립트를 사용하여 직사각형을 노란색에서 흰색으로 점진적으로 색을 변이 시키려고합니다. 슬프게도 내 코드가 작동하지 않습니다. 코드의 문제점과 해결 방법을 알아내는 데 도움을 줄 수 있습니까?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 –