Typecho给网站加个fps帧率显示美化

  • 🍂轩落阁🍃 丨2025年09月29日 丨152阅读 丨0评论
  • AI摘要:

    🍂轩落の阁🍃AI

    mg557pzq.png

    前言

    在现代网站开发中,FPS(每秒传输帧数)显示功能是衡量页面交互流畅度与渲染性能的重要指标,尤其对包含动画效果、滚动交互、3D 元素或复杂 UI 的网站而言,具有不可替代的实用价值。
    FPS 数值直观反映了浏览器每秒钟渲染页面的次数,理想状态下 60FPS 意味着页面动画流畅自然,用户操作无卡顿感;而当数值低于 30FPS 时,往往伴随明显的延迟或掉帧,直接影响使用体验。在网站中集成 FPS 显示功能后,数值通常以小巧的数字面板形式悬浮于页面角落(如左上角或右上角),既不干扰核心内容浏览,又能实时反馈当前性能状态。

    教程

    复制下面代码

    <!-- 引入FPS计数器 -->
    <script>
    document.addEventListener('DOMContentLoaded', function() {
        const fpsCounter = document.createElement('div');
        fpsCounter.style.position = 'fixed';
        fpsCounter.style.top = '10px';
        fpsCounter.style.left = '10px';
        fpsCounter.style.backgroundColor = 'transparent';
        fpsCounter.style.color = 'white';
        fpsCounter.style.padding = '3px 8px';
        fpsCounter.style.borderRadius = '3px';
        fpsCounter.style.fontFamily = 'Arial, sans-serif';
        fpsCounter.style.fontSize = '16px';
        fpsCounter.style.zIndex = '9999';
        fpsCounter.textContent = 'FPS: 0';
        document.body.appendChild(fpsCounter);
        let frameCount = 0;
        let lastTime = performance.now();
        let fps = 0;
        function updateFPS() {
            const currentTime = performance.now();
            frameCount++;
            if (currentTime - lastTime >= 1000) {
                fps = Math.round(frameCount * 1000 / (currentTime - lastTime));
                fpsCounter.textContent = `FPS: ${fps}`;
                if (fps >= 50) {
                    fpsCounter.style.color = '#4CAF50';
                } else if (fps >= 30) {
                    fpsCounter.style.color = '#FFC107';
                } else {
                    fpsCounter.style.color = '#F44336';
                }
                frameCount = 0;
                lastTime = currentTime;
            }
            requestAnimationFrame(updateFPS);
        }
        updateFPS();
    });
    </script>

    然后根据自己的typecho后台在合适的位置添加,一般都在头部的head里嵌入。当然底部也可以自行添加

    1赞
    打赏

    —— 评论区 ——

    请登录后发表评论
    立即登录
    LOGIN
    站点公告
    网站介绍: 网站用途为记录与分享个人日常生活中的美好瞬间和创作灵感。内容将围绕个人真实生活片段展开,例如日常旅行见闻、生活里的小确幸(如手工制作、家庭聚餐、季节风景记录)、读书感悟、日常穿搭或宠物等生活化场景 ,仅用作个人分享
    人生倒计时