2013-06-03 13 views
-1

我想寫一個JavaScript函數,將有效地拉下菜單,並使其使用css填充屬性更大。試圖改變使用javascript的單元格填充

這裏是原始的HTML(我想要的部分是在navbar.php):

<!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>Lux</title> 
<link href="css/style.css" rel="stylesheet" type="text/css" /> 
<link rel="shortcut icon" href="favicon2.ico" type="image/x-icon"> 
<link rel="icon" href="favicon2.ico" type="image/x-icon"> 
<script type="text/javascript" src="js/slideshow.js"></script> 
<script type="text/javascript" src="js/pageFunctions.js"></script> 

</head> 

<body onload="setUpSlideShow(); openPage()"> 
    <?php include 'navbar.php' ?> 
    <div class="content"> 
      <div id="slideshow"> 
       <div id="slides"> 
        <div class="slide"><a href="under_construction.php"><img src="images/tshirt.jpg" class="mainImage" /></a></div> 
        <div class="slide"><a href="under_construction.php"><img src="images/MOON.jpg" class="mainImage" /></a></div> 
        <div class="slide"><a href="under_construction.php"><img src="images/MOON2.jpg" class="mainImage" /></a></div> 
       </div> 
       <div id="slides-controls"> 
        <a href="#"></a> 
        <a href="#"></a> 
        <a href="#"></a> 
       </div> 
      </div> 
    </div> 
</body> 
</html> 

然後navbar.php如下:

<div id="headbar"> 
     <div class="headholder"> 
      <ul class="navbar"> 
       <a href="/lux/"><li>Home</li></a> 
       <a href="clothing.php"><li>Clothing</li></a> 
       <a href="about.php"><li>About</li></a> 
       <a href="contact.php"><li>Contact</li></a> 
      </ul> 
     </div> 
</div> 

爲headbar的CSS是:

#headbar { 
width: 100%; 
background-color: #999999; 
padding: 35px 0 0 0; 
text-align: center; 
} 

而且最後的的JavaScript代碼:

function openPage() { 
    var i; 
    for(i=0;i<30;i++) { 
     document.getElementById('headbar').style.paddingTop+=1; 
    } 
} 
+0

好的,看起來不錯。但是,你可以使用jQuery嗎? –

+2

什麼是問題/問題? –

+0

@JamesMontagne它只是不工作,文件上傳,但它只是沒有執行,所以我認爲這可能是我的代碼 –

回答

2

一個更好的選擇是試試這個:

function openPage() { 
    var i, el = document.getElementById('headbar'); 
    for(i=0;i<30;i++) { 
     el.style.paddingTop = el.style.paddingTop + i; 
    } 
} 

或者你也可以使用jQuery的.animate()方法。

+1

我也只會使用var聲明一次:var i,el = document.getElementById('headbar');然後在循環中引用你的var:el.style.paddingTop = el.style.paddingTop + 1; –

+0

@MichaelFreake Yup,更新。好的。 –

相關問題