Rilevamento Zoom Browser

by murgiaMarco

HTML

<div class="container">
        <h1>Livello di Zoom</h1>
        <div class="zoom-value" id="zoomDisplay">Caricamento...</div>
        <button onclick="alert(`Zoom attuale: ${getZoomLevel()}x`)">Mostra Zoom con Funzione</button>
    </div>

CSS

body {
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
            background: #f4f4f4;
        }
        .container {
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.2);
            text-align: center;
        }
        h1 {
            margin-bottom: 10px;
        }
        .zoom-value {
            font-size: 2em;
            color: #0078d7;
            margin: 10px 0;
        }
        button {
            padding: 10px 15px;
            font-size: 1em;
            background: #0078d7;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background: #005fa3;
        }

JavaScript

// Funzione per ottenere il livello di zoom
        function getZoomLevel() {
            return window.devicePixelRatio.toFixed(2);
        }

        // Aggiorna il display in tempo reale
        function updateZoomDisplay() {
            document.getElementById('zoomDisplay').textContent = getZoomLevel() + 'x';
        }

        // Aggiorna al caricamento
        updateZoomDisplay();

        // Aggiorna quando cambia il zoom (resize intercetta anche zoom)
        window.addEventListener('resize', updateZoomDisplay);