Simple Javascript Gallery - Thumbnails won't align to center

梦想的初衷 提交于 2019-12-25 06:47:26

问题


I have followed a tutorial from my University course to make a simple Javascript gallery. Everything on the page aligns to the center apart from the thumbnails. I have tried numerous code to sort it but nothing works. Even 1 thumbnail doesnt align.

http://www.imagebam.com/image/e00d83368784836

Here is the 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>

Here is the 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;
}

And this is the 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 ;

回答1:


You need to give the ul a width of 920px then give it "margin:auto;" this should align it in the centre. To centrally align something you will need to specify the width of its container.



来源:https://stackoverflow.com/questions/27277307/simple-javascript-gallery-thumbnails-wont-align-to-center

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!