在页面调整大小时,在flexbox中将标题与图像对齐

huangapple go评论59阅读模式
英文:

align title over images in flexbox when page resized

问题

我想在一个flexbox内的图像列表上有4个链接,但当我调整页面大小时,链接穿过图像,我认为这是因为图像和链接不是“相关的”,因此它们之间的填充和边距不起作用,但我不知道如何修复它。我猜我必须为它们都添加特定的position命令来使它们相关,但我不知道如何。

<!-- 开始代码片段:js 隐藏:false 控制台:true babel:false -->

<!-- 语言:lang-js -->
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' }];
const chapter3 = [{ src: '../images/maps/64.jpg' }];
const chapter4 = [{ src: '../images/maps/98.jpg' }];

// 用于显示所选章节的图像的函数
function showImages(chapter) {

    const img_list = document.getElementById('imageList');
    img_list.replaceChildren();

    // 循环遍历所选章节中的图像
    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";

        li.append(span);
        img_list.appendChild(li);
    });
}

// 为第1章链接添加点击事件
document.querySelector('#chapter1').addEventListener('click', function (e) {
    e.preventDefault();
    showImages(chapter1);
});

// 为第2章链接添加点击事件
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);
}
<!-- 语言:lang-css -->
@charset "utf-8;
/* CSS文档 */

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

    padding: 0;
    margin: 0;

    opacity: 0;
    transition: opacity 1s;
}

.menu {
    background-color: rgba(255, 255, 255, 0.5);

    justify-content: center;
    display: flex;
    margin: 0 auto;
    width: 55%;
    height: 100%;
    padding: 10px;
    position: relative;
}

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

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

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

.menu li span {

    padding: 5px;
}

a {

    font-size: 20px;
    font-weight: 200;
    padding: 5px;

}

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;
    padding: 5px;
}

.mappages {
    padding: 5px;
    position: absolute;
    top: 8px;
    left: 16px;
}
<!-- 语言:lang-html -->
<header>
    <h1>地图画廊</h1>
</header>

<div class="menu">
    <ul id="imageList"></ul>
    <div class="mappages">
        <a href="#" id="chapter1">第1章</a>
        <a href="#" id="chapter2">第2章</a>
        <a href="#" id="chapter3">第3章</a>
        <a href="#" id="chapter4">第4章</a>
    </div>
</div>
英文:

i want to have 4 links over a list of images inside a flexbox but when i resize the page the links go through the images, i believe its because the images and the links arent "related" and therefore padding and margin doesnt work between them, but i dont know how to fix it. im guessing i have to add specific position: commands to them both to make them related but i dont know.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

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

// Function to display images for the chosen chapter
function showImages(chapter) {
	
	const img_list = document.getElementById(&#39;imageList&#39;);
	img_list.replaceChildren();

    // Loop through the images in the selected chapter
    chapter.forEach(image =&gt; {
      
		const li = document.createElement(&#39;li&#39;);
		const img = new Image();
		
		img.src= image.src;
		img.alt=image.alt;
		li.append(img);
		
		const span = document.createElement(&#39;span&#39;);
		span.textContent = &quot;Image&quot;;
		
		li.append(span);
		img_list.appendChild(li);
    });
  }

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

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

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

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

window.onload =  function(){
	document.body.style.opacity = 1
	showImages(chapter1);
}

<!-- language: lang-css -->

@charset &quot;utf-8&quot;;
/* CSS Document */

body {
	background-image: url(&quot;../images/background-maps.jpg&quot;);
	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;
    margin: 0 auto;
    width:55%;
    height: 100%;
	padding:10px;
	position: relative;

    
}


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

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

}

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

.menu li span{
	
	padding: 5px;
}

a{
	
	font-size: 20px;
	font-weight: 200;
	padding:5px;
	
}



h1{
	
	font-family: &#39;Gill Sans&#39;, &#39;Gill Sans MT&#39;, Calibri, &#39;Trebuchet MS&#39;, 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;
	padding: 5px;
}

.mappages{
	padding: 5px;
	position: absolute;
	top: 8px;
	left: 16px;
	
}

<!-- language: lang-html -->

        &lt;header&gt;
            &lt;h1&gt;Map Gallery&lt;/h1&gt;
        &lt;/header&gt;
        
        
        &lt;div class=&quot;menu&quot;&gt;
            &lt;ul id=&quot;imageList&quot;&gt;&lt;/ul&gt; 
			&lt;div class=&quot;mappages&quot;&gt;
				&lt;a href=&quot;#&quot; id=&quot;chapter1&quot;&gt;Chapter 1&lt;/a&gt;
				&lt;a href=&quot;#&quot; id=&quot;chapter2&quot;&gt;Chapter 2&lt;/a&gt;
				&lt;a href=&quot;#&quot; id=&quot;chapter3&quot;&gt;Chapter 3&lt;/a&gt;
				&lt;a href=&quot;#&quot; id=&quot;chapter4&quot;&gt;Chapter 4&lt;/a&gt;
        	&lt;/div&gt;
            
        &lt;/div&gt;

<!-- end snippet -->

答案1

得分: 0

  1. 把 "mappages" 放在 "imageList" 上方,并移除 "mappages" 的绝对定位。同时,需要给 "menu" 添加 "flex-wrap: wrap"。

  2. 如果你想保持元素的当前顺序,那么仍然可以使用绝对定位,但你需要给父元素 "menu" 添加至少 50-60px 的上内边距来解决重叠问题。

英文:
  1. Put "mappages" above the "imageList" and remove the absolute position from "mappages". Also you have to add "flex-wrap: wrap" to "menu".

<!-- begin snippet: js hide: true console: false babel: false -->

<!-- language: lang-html -->

&lt;div class=&quot;menu&quot;&gt;
  &lt;div class=&quot;mappages&quot;&gt;&lt;/div&gt;
  &lt;ul id=&quot;imageList&quot;&gt;&lt;/ul&gt;
&lt;/div&gt;

<!-- end snippet -->

or

  1. If you want to keep the order of the elements as it's now, then you can still use absolute position but you have to add padding-top at least 50-60px to the parent's element "menu" to fix the overlapping

huangapple
  • 本文由 发表于 2023年2月8日 19:04:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定