DIV CSS 佈局教程網

用 JavaScript 做出從右側緩緩浮出的網頁浮動廣告
編輯:關於JavaScript     

側邊滑出的浮動廣告是早期網站常見的配置。這個範例示範了如何讓一個貼在右側的區塊,在頁面載入數秒後以緩衝(easing)方式滑出,並可點擊收合。

範例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>從右向左緩緩浮出的網頁浮動層廣告</title>

<script language="javascript">
var $ = function (d) { return document.getElementById(d); };

var CLS = {
    create: function () {
        return function () {
            this.$ADD = function (fn) { CLS.add(this, fn); };
            this.init.apply(this, arguments);
        }
    },
    add: function (obj, fn) { fn.apply(obj, arguments); },
    enterFrame: function () {
        this.onEnterFrame = function () {};
        this.$PLAY = function (g) {
            this.enterFrameP = this.enterFrameP || 10;
            this.CLStimeIndex = CLS.ontimes.length;
            CLS.ontimes.push(this);
            window.clearTimeout(this.enterFrameTimeout);
            window.clearInterval(this.enterFrameInterval);

            if (g) {
                this.enterFrameTimeout = window.setTimeout(
                    'CLS.ontimes[' + this.CLStimeIndex + '].enterFrameInterval='
                    + 'window.setInterval("CLS.ontimes[' + this.CLStimeIndex + '].onEnterFrame()",'
                    + "'" + this.enterFrameP + "')", parseInt(g * 1000));
            } else {
                this.enterFrameInterval = window.setInterval(
                    "CLS.ontimes[" + this.CLStimeIndex + "].onEnterFrame()", this.enterFrameP);
            }
        }
        this.$STOP = function () { window.clearInterval(this.enterFrameInterval); }
        this.$SET = function (p) { this.enterFrameP = p; }
    },
    ontimes: new Array()
};

CLS.Marquee = CLS.create();

CLS.Marquee.prototype = {
    init: function (button, box, speed) {
        this.box = $(box);
        this.tit = $(button);
        this.kong = $("kong");
        this.onOpen = true;
        this.show = false;
        this.time = 0;
        this.speed = speed;

        this.kong.style.height = this.box.offsetHeight + "px";
        this.Maxw = this.box.offsetWidth - this.tit.offsetWidth;
        this.box.style.right = -this.box.offsetWidth + "px";
        this.boxTop = this.kong.offsetTop;

        var _t = this;
        this.tit.onclick = function () {
            this.show = true;
            if (_t.onOpen) {
                _t.onEnterFrame = _t.close;
                _t.onOpen = false;
            } else {
                _t.onEnterFrame = _t.open;
                _t.onOpen = true;
            }
            _t.$PLAY();
        };

        this.$ADD(CLS.enterFrame);
        this.onEnterFrame = this.open;
        this.$PLAY();
    },

    open: function () {
        this.tit.innerHTML = "-";
        var _r = parseInt(this.box.style.right);
        var _b = (0 - _r) / 30;                 // 每次只走剩餘距離的 1/30,形成緩衝

        this.box.style.right = (_r + _b) + "px";
        this.kong.style.top = (document.documentElement.scrollTop + this.boxTop) + "px";

        if (_b == 0 && !this.show) {
            this.time += 10;
            if (this.time >= this.speed * 1000) {
                this.show = true;
                this.onOpen = false;
                this.$STOP();
                this.onEnterFrame = this.close;
                this.$PLAY();
            }
        }
    },

    close: function () {
        this.tit.innerHTML = "+";
        var _r = parseInt(this.box.style.right);
        var _b = (-this.Maxw - _r) / 5;

        this.box.style.right = Math.round(_r + _b) + "px";
        this.kong.style.top = (document.documentElement.scrollTop + this.boxTop) + "px";
    }
};

window.onload = function () {
    // tit 是點擊按鈕的 Id,abc 是浮動塊的 Id,10 是顯示時長(秒)
    setTimeout(function () { new CLS.Marquee("tit", "abc", 10); }, 3000);
};
</script>

<style type="text/css">
#abc {
    border: 1px solid #003399;
    position: absolute;
    height: 150px;
    width: 220px;
    right: -100%;
}
#abc #tit {
    background-color: #0066FF;
    position: relative;
    height: 100%;
    width: 20px;
    color: #FFFFFF;
    text-align: center;
    float: left;
}
#kong {
    position: absolute;
    width: 100%;
    top: 100px;
    overflow: hidden;
    right: 0px;
}
.right {
    float: right;
    width: 190px;
    padding: 5px;
}
</style>
</head>
<body style="margin:0px;">
<!-- 外層包一個容器,避免出現橫向捲軸 -->
<div id="kong">
  <div id="abc">
    <div id="tit">-</div>
    <div class="right">
      <a href="/">網頁上從右邊緩緩彈出的廣告框效果</a>
    </div>
  </div>
</div>

右側廣告 3 秒後彈出
</body>
</html>

緩衝動畫的關鍵

這個範例最值得學的是 easing 的算法:_b = (0 - _r) / 30。每次移動「剩餘距離的 1/30」,離目標越近步伐越小,視覺上就有先快後慢的減速感。這是無需任何函式庫就能做出自然動畫的經典技巧。

現代重寫

今天完全不需要這套框架。用 CSS transition 就能達到同樣(甚至更平順)的效果:

#ad {
    position: fixed;
    right: 0;
    top: 100px;
    width: 220px;
    background: #0066FF;
    color: #fff;
    transform: translateX(100%);
    transition: transform 0.8s cubic-bezier(0.22, 1, 0.36, 1);
}
#ad.show {
    transform: translateX(-20px);
}
window.addEventListener('load', function () {
    setTimeout(function () {
        document.getElementById('ad').classList.add('show');
    }, 3000);
});

cubic-bezier 就是 easing 函式的視覺化參數,比手算除法精準得多。另外,position: fixed 會自動隨捲動固定在視窗上,不必像原範例那樣每次影格都重算 scrollTop。

最後提醒:自動彈出的浮動廣告對使用者體驗與 SEO 都有負面影響,Google 對「遮擋主要內容的插頁式廣告」有明確的排名降級政策,尤其行動版。若真要使用,請確保有明顯的關閉鈕、不遮擋首屏內容,並尊重 prefers-reduced-motion 設定。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved