水平居中,不要垂直偏移其他内容

时间:2011-02-10 21:54:51

标签: javascript html positioning centering

我有一个基于Flash的网站。 我想用javascript(在网站上水平居中)将广告定位在它上面。

<script type="text/javascript">
var pw_d=document;
pw_d.projectwonderful_adbox_id = "00000";
pw_d.projectwonderful_adbox_type = "0";
</script>

flash网站全部在1个闪存容器中。我打算在Flash网站上留下视觉空白,让广告悬停在上面。

我不知道执行此操作所需的html或javascript。 仍然在寻找,这似乎有点普遍。

我假设我将使用div(s)

参考站点:

http://www.charbythevampirate.com/shell.html?page=comics&comic=24

3 个答案:

答案 0 :(得分:1)

这是我嘲笑的事情。

http://jsfiddle.net/meNVU/

基本上,您希望将图像绝对置于css中,然后将topleft属性设置为50%。之后,将margin-leftmargin-top分别设置为宽度的一半* -1和高度的一半* -1。然后,只需将z-index设置为嵌入闪存的<object>为0,将图像的z-index设置为1.这应该(并确实)让您走上正确的轨道。< / p>

答案 1 :(得分:0)

将您的脚本包裹在div

<div style="width:auto;margin 0 auto;padding-bottom: 10px">
<script type="text/javascript">
var pw_d=document;
pw_d.projectwonderful_adbox_id = "00000";
pw_d.projectwonderful_adbox_type = "0";
</script>
</div>

padding-bottom: 10px;会在闪光灯剩余部分之前留出一些空间,或者你可以取出它,或增加像素数。

答案 2 :(得分:0)

Matt McDonald的这个例子:https://stackoverflow.com/users/342129/matt-mcdonald

http://jsfiddle.net/meNVU/

*我的最终代码

shell.css

body {background-color: #000000;}

div#ad
{
    width: 728px;
    height: 90px;
    background-color: #000000;
    margin-left: -365px;
    margin-top: 77px;
    position: absolute;
    left: 50%;
    z-index: 1;   
}

shell.html(需要将我的flash对象包装在javascript中,以便使用flash调整大小,对于浏览器滚动条)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Charby The Vampirate</title>

<link rel="stylesheet" 
type="text/css" href="shell.css" />


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
<script type="text/javascript">

var myQueryString=document.location.search;
if (myQueryString[0]='?')
     {
        myQueryString=myQueryString.substr(1, myQueryString.length-1);
     }
var flashvars = { 
    dat: escape(myQueryString)
};
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1
if(is_chrome == true){
    var params = {
      allowFullScreen: "true", 
      menu: "false", 
      wmode: "transparent" 
    };
}else{
    var params = {
      allowFullScreen: "true", 
      menu: "false", 
      wmode: "window" 
    };

}
var attributes = {
  id: "resized", 
  name: "resized", 
  wmode: "window" 
};

swfobject.embedSWF("shell.swf", "resized", "100%", "100%", "10.0.0", "expressInstall.swf", flashvars, params, attributes);
</script>



<div id="ad">
    <!-- Beginning of Project Wonderful ad code: -->
    <!-- Ad box ID: 38564 -->
    <script type="text/javascript">
    <!--
    var pw_d=document;
    pw_d.projectwonderful_adbox_id = "43066";
    pw_d.projectwonderful_adbox_type = "5";
    //-->
    </script>
    <script type="text/javascript" src="http://www.projectwonderful.com/ad_display.js"></script>
    <!-- Ending of Project Wonderful ad code: -->
</div>

</head>

<body>
<div id="resized" align="center"></div>
</body>
<script>
function ResizeFlash(newHeight){

    $("#resized").css("height",newHeight);
}
</script>
</html>