【问题标题】:Retrieving and setting dynamically loaded image sizes via js通过js获取和设置动态加载的图片大小
【发布时间】:2020-07-26 22:05:05
【问题描述】:

考虑这个 javascript 代码:

const profileImageURL = 'https://api.images.com/image/' + id +'/profile';
let profileImage = [];

async function getProfileImage() {
    const response = await fetch(profileImageURL);
    profileImage = await response.json();
};

还有这个苗条的模板代码:

<div class="thumbImg">
    {#await getProfileImage()}
    <div class='spinnerHolder'
        in:fade={{duration: 500}} 
        out:fade={{duration: 500}} 
        on:introstart={() => doneLoading = false} 
        on:outroend={() => doneLoading = true}
        >
        <svg class="spinner" viewBox="0 0 50 50">
            <circle class="path" cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
        </svg>
    </div>
    {:then value}
        {#if doneLoading}
            <img transition:fade alt='profile-image' src={profileImage.url}/>
        {/if}
    {/await}
</div>

和css:

.thumbImg {
    width: 64px;
    height: 64px;
    display:inline-block;
    background-color: var(--border-color);
    border: none;
    border-radius: var(--large-radius);
    overflow: hidden;

    img {
        min-width: 64px;
        border-radius: var(--large-radius);
        border-image-width: 0;
    }
}

以上加载的图像到达不同的宽度和高度。一些肖像和一些风景。它们都需要显示在一个 64px x 64px 的正方形 1:1 框中,侧面或底部没有空白。如何保持这些动态加载的图像的纵横比,以便它们根据需要填充宽度或高度?理想情况下,它们也会被偏移,因此它们在 1:1 框中居中。

问题的视觉示例:

【问题讨论】:

  • 尝试为 .thumbImgimg 标签 - max-width: 64pxmax-height: 64px 类执行此操作
  • ja.. 我试过了,但它不起作用。如果图像是纵向的,那么你会在侧面得到空白:(
  • 你试过对img标签做绝对定位吗?
  • 你到底是什么意思?
  • 现在看到图终于明白问题所在了。试着把 - min-width: 100%.

标签: javascript html css image svelte


【解决方案1】:

根据下面 Rich 的评论更新了答案

您可以同时使用object-fitobject-position CSS 属性来微调图像在其内容框中的显示方式:

.thumbImg {
    width: 64px;
    height: 64px;
    display:inline-block;
    background-color: var(--border-color);
    border: none;
    border-radius: var(--large-radius);
    overflow: hidden;

    img {
        height: 64px;
        width: 64px;
        object-fit: cover; // this will make the image cover the content box, clipping horizontally if the original image is landscape orientation, or vertically if it is portrait orientation
        object-position: 50% 50%; // this will center the image in the content box (i.e. clip on the left & right if source is landscape and clip top & bottom if source is portrait)
    }
}

有了这个,你就不需要再修改你的代码了。

原始答案,使用 CSS 背景图片

您可以通过使用背景图像 CSS 属性而不是 img 标记来实现所需的定位,例如:

.avatar {
    height: 64px;
    width: 64px;
    background-size: cover; // this will make the image cover the div, clipping horizontally if the original image is landscape orientation, or vertically if it is portrait orientation
    background-position: 50% 50%; // this will center the image (i.e. clip on the left & right if source is landscape and clip top & bottom if source is portrait)
}

有了这个,你所要做的就是用&lt;div class="avatar"&gt;标签替换你的&lt;img&gt;标签并动态设置背景图像源:

<div class="thumbImg">
    {#await getProfileImage()}
    <div class='spinnerHolder'
        in:fade={{duration: 500}} 
        out:fade={{duration: 500}} 
        on:introstart={() => doneLoading = false} 
        on:outroend={() => doneLoading = true}
        >
        <svg class="spinner" viewBox="0 0 50 50">
            <circle class="path" cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
        </svg>
    </div>
    {:then value}
        {#if doneLoading}
            <div
                transition:fade
                class="avatar"
                alt="Profile image"
                style="background-image: url('{profileImage.url}')"
            />
        {/if}
    {/await}
</div>

当然,缺点是您不再使用本机图像标签,也许这对您很重要。从好的方面来说,它无需使用基于 javascript 的 DOM 操作即可工作。

【讨论】:

  • 太棒了!这正是我需要的!
  • 你不需要使用背景,你可以在普通的&lt;img&gt;上使用object-fitobject-position的组合
  • 答案已更新,谢谢@RichHarris。我不知道这些属性,这真的很有帮助。
【解决方案2】:

您可以使用百分比值使图像相对于父 div 的宽度和高度,也可以将图像设置为所需的最大宽度和最大高度

.thumbImg {
    max-width: 64px;
    max-height: 64px;
    display:inline-block;
    background-color: var(--border-color);
    border: none;
    border-radius: var(--large-radius);
    overflow: hidden;

    img {
        max-width: 100%; //relative to the parent DIV
        border-radius: var(--large-radius);
        border-image-width: 0;
    }
}

【讨论】:

  • 嗨..谢谢你,但这仍然不起作用:(
  • 我想答案可能会使用 JS :D
  • @TimmyLee 最后一个想法。您是否对 img 选择器使用了 100% 的最小宽度?
猜你喜欢
  • 2014-06-08
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 2018-05-08
  • 1970-01-01
  • 2021-08-12
  • 1970-01-01
  • 2016-01-13
相关资源
最近更新 更多