javascript 对齐图像列表下的文本

gmxoilav  于 2023-02-18  发布在  Java
关注(0)|答案(1)|浏览(111)

我有一个页面,中间有一个菜单,菜单内部有图像正确对齐在一个ul中,但是我想在每个图像下添加文本,但当我尝试时,文本由于某种原因最终在中间一直到菜单的右边,为甚么?此外,感觉我应该删除所有的JavaScript代码,从头开始,并使用更简单的选项来实现我的图像在HTML文件代替。我已经连续坐了10个小时,设计我的网站和学习,所以我可能只是累了,但任何帮助是赞赏。
网页:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Map Gallery</title>
        <link rel="stylesheet" href="stylemaps.css">
    </head>

    <body onload="document.body.style.opacity='1'">

        <header>
            <h1>Map Gallery</h1>
        </header>
        
        
        <div class="menu">
            <ul id="imageList"></ul>
            
        </div>
        <div class="mappages">
            <a href="#" id="chapter1">Chapter 1</a>
            <a href="#" id="chapter2">Chapter 2</a>
            <a href="#" id="chapter3">Chapter 3</a>
            <a href="#" id="chapter4">Chapter 4</a>
        </div>

        
        <script src="fadeAnim.js"></script>
        <script src="mapscript.js"></script>
    </body>
</html>

CSS:

@charset "utf-8";
/* CSS Document */

body {
    background-image: url("../images/background-maps.jpg");
    background-repeat: no-repeat;
    background-size: cover;
    
    padding:0;
    margin:0;

    opacity: 0;
    transition: opacity 1s;

    /*animation: fadeInAnimation ease 3s;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;*/
}
/*@keyframes fadeInAnimation {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
     }
}*/


.menu{
    background-color: rgba(255,255,255,0.5);
    
    justify-content: center;
    display: flex;
    align-items: center;
    margin: 0 auto;
    width:55%;
    height: 100%;

 
}

.menu ul{
    list-style-type: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
}

.menu li{
    width: 20%;
    min-width: 200px;
    margin: 10px;
    text-align: center;
    
}

.menu img{
    width: 100%;
    height: 100%;
    object-fit: cover;
   
    
}


h1{
    
    font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
    font-weight: 600;
    font-size: 75px;
    text-align: center;
    text-shadow: 1px 1px 2px red, 0 0 0.5em blue, 0 0 0.1em blue;

    margin: auto;
}

约旦:

const chapter1 = [];
for (let i = 1; i <= 49; i++){

chapter1.push({src: `../images/maps/chapter1/${i}.jpg`, alt: `Map ${i}`});
}
const chapter2 = [{src: '../images/maps/76.jpg'}]; //not used yet
const chapter3 = [{src: '../images/maps/64.jpg'}]; //not used yet
const chapter4 = [{src: '../images/maps/98.jpg'}]; //not used yet

// Function to display images for the chosen chapter
function showImages(chapter) {
    document.querySelector('#imageList').innerHTML = '';

    // Loop through the images in the selected chapter
    chapter.forEach(image => {
      
      const li = document.createElement('li');
      li.innerHTML = `<img src="${image.src}" alt="${image.alt}">`;
      document.querySelector('#imageList').appendChild(li);
    });
  }

  // Add click event to the Chapter 1 link
  document.querySelector('#chapter1').addEventListener('click', function(e) {
    e.preventDefault();
    showImages(chapter1);
  });

  // Add click event to the Chapter 2 link
  document.querySelector('#chapter2').addEventListener('click', function(e) {
    e.preventDefault();
    showImages(chapter2);
  });

  document.querySelector('#chapter3').addEventListener('click', function(e) {
    e.preventDefault();
    showImages(chapter3);
  });

  document.querySelector('#chapter4').addEventListener('click', function(e) {
    e.preventDefault();
    showImages(chapter4);
  });

  showImages(chapter1);
w1jd8yoj

w1jd8yoj1#

欢迎来到stckoverflow@ginger,我做了一些更正,并将示例文本放置在图像下面。

const chapter1 = [{src: 'https://images.unsplash.com/photo-1558383331-f520f2888351?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8OHx8c2FtcGxlfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=500&q=60'},
{src: 'https://images.unsplash.com/photo-1558383331-f520f2888351?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8OHx8c2FtcGxlfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=500&q=60'}];
/*for (let i = 1; i <= 49; i++){

chapter1.push({src: `../images/maps/chapter1/${i}.jpg`, alt: `Map ${i}`});
}*/
const chapter2 = [{src: 'https://images.unsplash.com/photo-1616020453784-a24fa9845b05?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8OXx8c2FtcGxlfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=500&q=60'}]; //not used yet
const chapter3 = [{src: 'https://images.unsplash.com/photo-1561336313-0bd5e0b27ec8?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8M3x8c2FtcGxlfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=500&q=60'}]; //not used yet
const chapter4 = [{src: 'https://images.unsplash.com/photo-1612528443702-f6741f70a049?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8NXx8c2FtcGxlfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=500&q=60'}]; //not used yet

// Function to display images for the chosen chapter
function showImages(chapter) {
    
    const img_list = document.getElementById('imageList');
    img_list.replaceChildren();// equal to innerHtml = '';
    // Loop through the images in the selected chapter
    chapter.forEach(image => {
      
      const li = document.createElement('li');
      const img = new Image();
      img.src = image.src;
      img.alt = image.alt;
      li.append(img);
      const span = document.createElement('span');
      span.textContent = "Image Title";
      li.append(span);
      img_list.appendChild(li);
    });
  }

  // Add click event to the Chapter 1 link
  document.querySelector('#chapter1').addEventListener('click', function(e) {
    e.preventDefault();
    showImages(chapter1);
  });

  // Add click event to the Chapter 2 link
  document.querySelector('#chapter2').addEventListener('click', function(e) {
    e.preventDefault();
    showImages(chapter2);
  });

  document.querySelector('#chapter3').addEventListener('click', function(e) {
    e.preventDefault();
    showImages(chapter3);
  });

  document.querySelector('#chapter4').addEventListener('click', function(e) {
    e.preventDefault();
    showImages(chapter4);
  });

window.onload = function(){
  document.body.style.opacity = 1
  showImages(chapter1);
}
@charset "utf-8";
/* CSS Document */

body {
    background-image: url("../images/background-maps.jpg");
    background-repeat: no-repeat;
    background-size: cover;
    
    padding:0;
    margin:0;

    opacity: 0;
    transition: opacity 1s;

    /*animation: fadeInAnimation ease 3s;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;*/
}
/*@keyframes fadeInAnimation {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
     }
}*/


.menu{
    background-color: rgba(255,255,255,0.5);
    
    justify-content: center;
    display: flex;
    align-items: center;
    margin: 0 auto;
    width:55%;
    height: 100%;
    padding:10px;
 
}

.menu ul{
    list-style-type: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
}

.menu li{
    width: 20%;
    min-width: 200px;
    margin: 15px;
    text-align: center;
    
}
.menu li span{
  padding:5px;
}
.menu img{
    width: 100%;
    height: 100%;
    object-fit: cover;
   
    
}
h1{
    
    font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
    font-weight: 600;
    font-size: 75px;
    text-align: center;
    text-shadow: 1px 1px 2px red, 0 0 0.5em blue, 0 0 0.1em blue;

    margin: auto;
}
.mappages{
  padding:5px;
}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Map Gallery</title>
        <link rel="stylesheet" href="stylemaps.css">
    </head>

    <body onload="document.body.style.opacity='1'">

        <header>
            <h1>Map Gallery</h1>
        </header>
        
        
        <div class="menu">
            <ul id="imageList"></ul>
            
        </div>
        <div class="mappages">
            <a href="#" id="chapter1">Chapter 1</a>
            <a href="#" id="chapter2">Chapter 2</a>
            <a href="#" id="chapter3">Chapter 3</a>
            <a href="#" id="chapter4">Chapter 4</a>
        </div>

        
        <script src="fadeAnim.js"></script>
        <script src="mapscript.js"></script>
    </body>
</html>

相关问题