[英]Simple Javascript Gallery - Thumbnails won't align to center
我遵循了大學課程中的教程,制作了一個簡單的JavaScript庫。 除縮略圖外,頁面上的所有內容均與中心對齊。 我嘗試了許多代碼來對其進行排序,但是沒有任何效果。 甚至1個縮略圖都無法對齊。
http://www.imagebam.com/image/e00d83368784836
這是html:
<div id="gallery">
<div id="homegallimage">
<img id="1" src="../images/homegallery/1.jpg" alt="Web Site Example 1" />
</div>
<ul id="thumbnails">
<li><img src="../images/homegallery/1.jpg" alt="Web Site Example 1" class="thumbnail"></li>
<li><img src="../images/homegallery/2.jpg" alt="Web Site Example 2" class="thumbnail"></li>
<li><img src="../images/homegallery/3.jpg" alt="Web Site Example 3" class="thumbnail"></li>
<li><img src="../images/homegallery/4.jpg" alt="Web Site Example 4" class="thumbnail"></li>
<li><img src="../images/homegallery/5.jpg" alt="Web Site Example 5" class="thumbnail"></li>
<li><img src="../images/homegallery/6.jpg" alt="Web Site Example 6" class="thumbnail"></li>
<li><img src="../images/homegallery/7.jpg" alt="Web Site Example 7" class="thumbnail"></li>
<li><img src="../images/homegallery/8.jpg" alt="Web Site Example 8" class="thumbnail"></li>
<li><img src="../images/homegallery/9.jpg" alt="Web Site Example 9" class="thumbnail"></li>
</ul>
</div>
這是CSS:
.content
{
color: #000;
background-color:#fff;
text-align: center;
}
.thumbnail
{
width: 100px;
height: 50px;
border: solid 4px #6ed8f2;
}
#thumbnails li
{
display: inline-block;
list-style-type: none;
}
#homegallimage img
{
border: solid 8px #6ed8f2;
}
這是JavaScript:
// changes source on image to that provided within the thumbnail image tag
function showPic(i_element) {
var source = i_element.getAttribute("src") ;
var alt = i_element.getAttribute("alt") ;
var i = document.createElement("img") ;
i.setAttribute("src",source) ;
i.setAttribute("alt",alt) ;
var placeholder = document.getElementById("homegallimage") ;
//alert(placeholder.childNodes.length) ;
placeholder.removeChild(placeholder.childNodes[0]);
placeholder.appendChild(i) ;
}
// add the onclick event to the DOM
function addImages() {
var item = document.getElementById("thumbnails").getElementsByTagName("img") ;
for (var i=0 ; i<item.length ; i++) {
item[i].onmouseover = function() {showPic(this) ; } ;
}
}
// specifies the method to run onload
window.onload = addImages ;
您需要給ul一個920px的寬度,然后給它“ margin:auto;”。 這應該使其居中對齊。 要使某物居中對齊,您需要指定其容器的寬度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.