Flow Flash AD using JavaScript and SWFObject

This article shows how to create a flow flash ad by using javascript together with swfobject.

@see getScroll.js
@see http://code.google.com/p/swfobject/

[codesyntax lang=”html4strict”]

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
    <title>flash flow ad using swfobject</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="getScroll.js"></script>
    <script type="text/javascript" src="swfobject.js"></script>
</head>
<body>
    <!--main content-->
    <div style="height:2000px;">content</div>

    <!--ad-->
    <div id="ad-flow" style="top:5px; left:100px; position:absolute;">
        <div id="ad-test"></div>
        <a style="text-align:left; display:block; background:#eee; width:120px;" href="javascript:void(0)" onclick="javascript:closeAd();" hidefocus="true">关闭</a>
    </div>

    <!--the flow ad script -->
    <script type="text/javascript" src="ad-flow.js"></script>
</body>
</html>

[/codesyntax]

The ad-flow.js goes here :

[codesyntax lang=”javascript”]

// to close the ad and stop interval heart beat
function closeAd(id)
{
    document.getElementById('ad-flow').style.visibility = "hidden";
    document.getElementById('ad-test').style.visibility = "hidden";
    window.clearInterval(int);
}

// global value to log the last position
var lastY = 0;

// the heart beat function
function heartBeat()
{
    // get current top position
    var curY = getScroll().t;

    // speed : the bigger the faster
    var speed = 0.05;
    percent = speed * (curY - lastY);
    if (percent > 0) percent = Math.ceil(percent);
    else percent = Math.floor(percent);

    // keep stay there!
    document.getElementById("ad-flow").style.top = parseInt(document.getElementById("ad-flow").style.top)
                                                                   + percent + "px";

    // log last position
    lastY = lastY + percent;
}

// beat every 1 microsecond
window.setInterval("heartBeat()", 1);

// flash ad embed by swfobject
var flashvars = {};
var params = {
    // important for getUrl() in flash
    allowscriptaccess : "always"
};
var attributes = {};
swfobject.embedSWF(
    "http://www.kimbs.cn/static/ad-test.swf",
    "ad-test",
    "120",
    "270",
    "9.0.0",
    "expressInstall.swf",
    flashvars,
    params,
    attributes
);

[/codesyntax]

Posted in Flash, JavaScript | Tagged , | Leave a comment