Bạn kéo xuống cuối Blog mình, ở phần Footer bên phải là thấy demo nhé hihi!
demo nhá:
Bạn chỉ cần thêm đoạn code này là được!
Các làm: vào bố cục => thêm tiện ích => chọn tiện ích HTML/JavaScript:
Thêm đoạn code này vào:
<div style="text-align: center; padding: 20px;">
<canvas id="canvasCounter3" width="280" height="100" style="border: 2px dashed #333;"></canvas>
</div>
<script>
(function() {
const canvas = document.getElementById('canvasCounter3');
const ctx = canvas.getContext('2d');
const startDate = new Date(2023, 3, 10, 0, 0, 0); // Thay bằng ngày bắt đầu blog (tháng tính từ 0)
function draw() {
const now = new Date();
let diff = now - startDate;
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
diff %= (1000 * 60 * 60 * 24);
const hours = Math.floor(diff / (1000 * 60 * 60));
diff %= (1000 * 60 * 60);
const minutes = Math.floor(diff / (1000 * 60));
diff %= (1000 * 60);
const seconds = Math.floor(diff / 1000);
// Xóa canvas và vẽ lại nền
ctx.fillStyle = '#fffbcc';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#333';
ctx.font = 'bold 16px Courier New';
ctx.textAlign = 'center';
ctx.fillText(`🦊 Blog đã hoạt động:`, canvas.width / 2, 30);
ctx.fillText(`${days} ngày ${hours}h ${minutes}m ${seconds}s`, canvas.width / 2, 60);
}
draw();
setInterval(draw, 1000); // Cập nhật mỗi giây
})();
</script>
0 Nhận xét