任务:
保留缩略图的垂直列表。拇指指甲必须与窗口尺寸成比例。缩略图包含在一个 div 中,其尺寸使用 vw、vh 给出。每次调整大小时,Javascript 函数都会重新计算所有缩略图的宽度和高度,以便固定数量的缩略图出现在 div 的可见区域中并且尽可能大。为了保持缩略图的垂直间距不变,可见缩略图的高度相加,增加一个因子并分配给 div 的高度。
问题:
当窗口变得很窄时,拇指指甲之间的垂直空间会越来越大。为 hFit 和 hTotal 计算的值(请参阅下面的 Javascript 代码)似乎不正确,会导致不需要的覆盖或缩略图的垂直间距过大。
详细信息:
整个布局如下:
最外面的 div (.content-area) 控制整个控件的垂直对齐方式(居中)。 .content-area (.content-control) 的子项控制实际列表 (.content-data) 的布局以及将出现在该列表左侧的关闭按钮 (.close-btn-area)。
代码:
CSS:
.content-area
{
position: absolute;
left: 2vw;
top: 5vh;
width: 30vw;
height: 90vh;
display: flex;
flex-direction: column;
align-items: start;
justify-content: center;
list-style: none;
opacity: 0.0;
}
.content-control
{
position: relative;
margin: 0 0 0 0;
display: flex;
flex-direction: row;
align-items: start;
justify-content: flex-start;
overflow: hidden;
}
.content-data
{
position: relative;
margin: 0 0 0 0;
padding: 0 0 0 0;
width: auto;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
overflow: hidden;
}
#thumbs-content
{
margin: 1vmin 1vmin 1vmin 1vmin;
height: 78vh;
font-family: 'Alegreya Sans SC', Verdana, sans-serif;
font-variant: small-caps;
overflow: hidden;
color:#404040;
}
.thumb-size
{
margin: 1vmin 0;
width: 16vw;
height: 12vh;
display: flex;
justify-content: center;
align-items: center;
}
.close-btn-area
{
margin: 1vmin 1vmin 1vmin 1vmin;
width: 4vh;
height: 4vh;
display: flex;
align-items: start;
justify-content: flex-start;
cursor: pointer;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
-ms-user-select: none;
user-select: none;
}
.close-btn
{
width: 4vh;
height: 4vh;
border: 0;
}
HTML:
<div class="content-area" id="thumbs-area">
<div class="content-control" id="thumbs-control">
<div class="close-btn-area" id="close-thumbs">
<a><img class="close-btn" id="close-btn-thumbs" src="close-btn-inverted-128x128.png">
</div>
<div class="content-data" id="thumbs-data">
<article id="thumbs-content">
<div class="thumb-size"><img class="thumb" id="thumb1" src="img1.jpg"></div>
<div class="thumb-size"><img class="thumb" id="thumb2" src="img2.jpg"></div>
<div class="thumb-size"><img class="thumb" id="thumb3" src="img3.jpg"></div>
<div class="thumb-size"><img class="thumb" id="thumb4" src="img4.jpg"></div>
<div class="thumb-size"><img class="thumb" id="thumb5" src="img5.jpg"></div>
<div class="thumb-size"><img class="thumb" id="thumb6" src="img6.jpg"></div>
<div class="thumb-size"><img class="thumb" id="thumb7" src="img7.jpg"></div>
<div class="thumb-size"><img class="thumb" id="thumb8" src="im8.jpg"></div>
<div class="thumb-size"><img class="thumb" id="thumb9" src="im9.jpg"></div>
<div class="thumb-size"><img class="thumb" id="thumb10" src="img10jpg"></div>
<div class="thumb-size"><img class="thumb" id="thumb11" src="img11.jpg"></div>
</article>
</div>
</div>
</div>
JavaScript:
const nVisibleThumbs = 6;
var nTopThumb = 0;
var nThumbCount = 11; // simplified; will be computed in the actual code
var nThumbScale = 0.9;
function RecalcThumbsLayout ()
{
var elem = $('#thumbs-content');
var w = elem.width ();
var h = Math.round (elem.height () * nThumbScale);
var hFit = 0;
var wFit = 0;
var hTotal = 0;
for (i = 1; i <= nThumbCount; i = i + 1)
{
var idStr = "#thumb" + i;
var ar = Math.min (1.5, $(idStr).prop ('naturalWidth') / $(idStr).prop ('naturalHeight'));
var ph = Math.round (h / nVisibleThumbs * 0.9);
var pw = Math.round (Math.min (ph * ar, w * 0.9));
ph = Math.floor (pw / ar); // considers portrait format images
$(idStr).css ("width", pw);
$(idStr).css ("height", ph);
hTotal += ph;
if ((i > nTopThumb) && (i <= nTopThumb + nVisibleThumbs))
hFit += ph;
if (wFit < pw)
wFit = pw;
}
wFit *= 1.25; // compensate for scaling above
hFit *= 1.25; // compensate for scaling above
$('#thumbs-data').css ('width', wFit + 'px');
$('#thumbs-data').css ('height', hFit + 'px');
elem.css ('height', hTotal + 'px');
}
演示:
要查看不需要的效果,您可以转到此处:http://www.brockart.de/S ,单击“Schmuck”,然后水平调整浏览器窗口的大小。
问题:
- 我需要在 Javascript 中更改什么才能使其正常工作?
- 是否有更优雅的方法来仅使用 css/html 来做到这一点?
最佳答案
如果没有 JS 而使用更简单的 HTML 和 CSS,您可能会得到它(flexbox 和过多的容器看起来没有必要)。
自
Keep a vertical list of thumb nails. ... a fixed number of them appears in the visible area of the div
水平调整大小不应影响缩略图大小以保持图像纵横比。
因此,我们所需要做的就是根据所需的可见缩略图数量来计算和设置图像高度。
全屏运行下面的代码片段并调整窗口大小:
.content {
position: absolute;
left: 2vw;
top: 5vh;
height: 90vh;
}
.thumbs {
height: 100%;
padding: 0 1rem;
overflow-y: auto;
}
.thumbs img {
display: block;
box-sizing: border-box;
/*
since we need to fit 6 images,
set img height = 1/6 of the container height
*/
height: 16.667%;
width: auto;
border: solid 1vh transparent;
}
.close-btn {
float: left;
width: .9em;
height: .9em;
border: solid .15em;
border-radius: 55%;
color: #fff;
background: #000;
box-shadow: 0 0 2px #000;
text-align: center;
font: 400 2rem/.9 sans-serif;
}
<div class="content">
<a class="close-btn">×</a>
<div class="thumbs">
<img src="https://picsum.photos/400/200/?1">
<img src="https://picsum.photos/400/200/?2">
<img src="https://picsum.photos/400/200/?3">
<img src="https://picsum.photos/400/200/?4">
<img src="https://picsum.photos/400/200/?5">
<img src="https://picsum.photos/400/200/?6">
<img src="https://picsum.photos/400/200/?7">
<img src="https://picsum.photos/400/200/?8">
<img src="https://picsum.photos/400/200/?9">
<img src="https://picsum.photos/400/200/?0">
<img src="https://picsum.photos/400/200/?a">
<img src="https://picsum.photos/400/200/?b">
</div>
</div>
关于javascript - 如何控制子元素的垂直间距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48840838/