萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

JavaScript 彈窗在任意解析度下居中/靠左/靠右顯示實作

前言

彈窗定位是前端常見需求,尤其要在各種螢幕解析度、捲軸位置下保持「居中」、「靠左」、「靠右」三種模式。本文提供一套完整的原生 JS + jQuery 實作,涵蓋視窗尺寸計算、捲軸偏移補償、動畫彈出與遮罩層。

核心計算邏輯

function init() {
    windowHeight = $(window).height();
    windowWidth  = $(window).width();
    popHeight    = $(".window").height();
    popWidth     = $(".window").width();   // 注意原代碼拼寫 popWidht
    scrollTop    = $(window).scrollTop();
    scrollLeft   = $(window).scrollLeft();
}

居中座標

var popY = (windowHeight - popHeight) / 2 + scrollTop;
var popX = (windowWidth  - popWidth)  / 2 + scrollLeft;

靠左座標

var popY = windowHeight + scrollTop - popHeight - 10;
var popX = scrollLeft - 5;

靠右座標

var popY = windowHeight - popHeight + scrollTop - 10;
var popX = windowWidth - popWidth + scrollLeft - 10;

完整 JS 模組化寫法(ES6 風格)

const Popup = (function() {
    let $win = $(window), $doc = $(document), timeout;

    function initDim() {
        return {
            winH: $win.height(), winW: $win.width(),
            popH: $(".window").outerHeight(), popW: $(".window").outerWidth(),
            scrollT: $win.scrollTop(), scrollL: $win.scrollLeft()
        };
    }

    function animateShow($el, top, left) {
        clearTimeout(timeout);
        timeout = setTimeout(() => {
            $el.css({top, left}).animate({opacity: "show"}, 300);
            bindClose($el);
        }, 300);
    }

    function bindClose($el) {
        $el.find(".title img").one("click", function() {
            $(this).closest(".window").hide("slow");
        });
    }

    return {
        center: function() {
            const d = initDim();
            const top  = (d.winH - d.popH) / 2 + d.scrollT;
            const left = (d.winW - d.popW) / 2 + d.scrollL;
            animateShow($("#center"), top, left);
        },
        left: function() {
            const d = initDim();
            const top  = d.winH + d.scrollT - d.popH - 10;
            const left = d.scrollL - 5;
            animateShow($("#left"), top, left);
        },
        right: function() {
            const d = initDim();
            const top  = d.winH - d.popH + d.scrollT - 10;
            const left = d.winW - d.popW + d.scrollL - 10;
            animateShow($("#right"), top, left);
        }
    };
})();

// 綁定按鈕
$("#btn_center").click(Popup.center);
$("#btn_left").click(Popup.left);
$("#btn_right").click(Popup.right);

// 捲動時重新定位(可選)
$win.scroll(() => {
    if ($(".window:visible").length) {
        // 重新計算並 animate
    }
});

CSS 樣式

.window {
    width: 250px;
    background-color: #3FF;
    padding: 2px;
    margin: 5px;
    position: absolute;
    display: none;
}
.content { height: 150px; background: #FFF; padding: 2px; font-size: 14px; overflow: auto; }
.title { padding: 2px; color: #999; font-size: 14px; }
.title img { float: right; cursor: pointer; }

HTML 結構

<input type="button" value="彈出居中的窗口" id="btn_center">
<input type="button" value="彈出居右的窗口" id="btn_right">
<input type="button" value="彈出居左的窗口" id="btn_left">

<div class="window" id="center">
    <div class="title"><img src="close.gif">標題</div>
    <div class="content">內容區塊</div>
</div>
<div class="window" id="right">...</div>
<div class="window" id="left">...</div>

注意事項

  • 原代碼 popWidht 拼寫錯誤,已修正為 popWidth。
  • 避免重複綁定 scroll 事件,建議在彈出時綁定、關閉時解綁。
  • 若頁面有多個彈窗,請改用 class 而非 ID 選擇器,並維護實例池。
copyright © 萬盛學電腦網 all rights reserved