logoEnterprise-level component library based on Bootstrap and Blazor
gitee
version
license
download
repo
commit
build
coverage

ImageViewer 图片

图片容器,在保留原生 img 的特性下,支持懒加载,自定义占位、加载失败等

加载并显示图片文件

Demo
  • object-fit: fill 填充 默认值 使图片拉伸填满整个容器, 不保证保持原有的比例
  • object-fit: contain 包含 保持原有尺寸比例缩放 保证整个图片都可以出现在容器中,因此此参数可能会在容器内留下空白
  • object-fit: cover 覆盖 保持原有尺寸比例缩放,宽度和高度至少有一个和容器一致(尺寸小的一致)因此此参数可能会让图片部分区域不可见
  • object-fit: none 无 保持原有尺寸比例,同时保持图片原始尺寸大小,多出的部分隐藏
  • object-fit: scale-down 降低 就好像依次设置了 nonecontain 最终呈现的是尺寸比较小的那个
Fill
Contain
Cover
None
ScaleDown

可通过设置 ShowPlaceHolder 开启占位符功能,通过设置 PlaceHolderTemplate 自定义占位内容

Demo
图片加载后浏览器默认会缓存,建议 F12 关闭缓存体验此功能
默认
Loading...
自定义
加载中 ...

可通过设置 PlaceHolderTemplate 开启占位模板功能,未设置 Url 或者正在加载时显示此模板内容

Demo
图片加载后浏览器默认会缓存,建议 F12 关闭缓存体验此功能
未设置 Url
占位模板
加载时显示
占位模板

可通过设置 ErrorTemplate 开启错误模板功能,参数 Url 无法加载图片时显示此模板内容

Demo
Url 路径错误
自定义

可通过设置 PreviewList 开启预览大图的功能

Demo

Attributes

Loading...
@page "/imageviewers"

<h3>ImageViewer 图片</h3>

<h4>图片容器,在保留原生 img 的特性下,支持懒加载,自定义占位、加载失败等</h4>

<DemoBlock Title="基本用法" Introduction="加载并显示图片文件" Name="Normal">
    <ul class="ul-demo">
        <li><code>object-fit: fill</code> 填充 默认值 使图片拉伸填满整个容器, 不保证保持原有的比例</li>
        <li><code>object-fit: contain</code> 包含 保持原有尺寸比例缩放 保证整个图片都可以出现在容器中,因此此参数可能会在容器内留下空白</li>
        <li><code>object-fit: cover</code> 覆盖 保持原有尺寸比例缩放,宽度和高度至少有一个和容器一致(尺寸小的一致)因此此参数可能会让图片部分区域不可见</li>
        <li><code>object-fit: none</code> 无 保持原有尺寸比例,同时保持图片原始尺寸大小,多出的部分隐藏</li>
        <li><code>object-fit: scale-down</code> 降低 就好像依次设置了 <b>none</b> 或 <b>contain</b> 最终呈现的是尺寸比较小的那个</li>
    </ul>
    <div class="images mt-3">
        <div class="images-item">
            <div>Fill</div>
            <ImageViewer Url="_content/BootstrapBlazor.Shared/images/bird.jpeg" FitMode="ObjectFitMode.Fill" />
        </div>
        <div class="images-item">
            <div>Contain</div>
            <ImageViewer Url="_content/BootstrapBlazor.Shared/images/bird.jpeg" FitMode="ObjectFitMode.Contain" />
        </div>
        <div class="images-item">
            <div>Cover</div>
            <ImageViewer Url="_content/BootstrapBlazor.Shared/images/bird.jpeg" FitMode="ObjectFitMode.Cover" />
        </div>
        <div class="images-item">
            <div>None</div>
            <ImageViewer Url="_content/BootstrapBlazor.Shared/images/bird.jpeg" FitMode="ObjectFitMode.None" />
        </div>
        <div class="images-item">
            <div>ScaleDown</div>
            <ImageViewer Url="_content/BootstrapBlazor.Shared/images/bird.jpeg" FitMode="ObjectFitMode.ScaleDown" />
        </div>
    </div>
</DemoBlock>

<DemoBlock Title="占位内容" Introduction="可通过设置 <code>ShowPlaceHolder</code> 开启占位符功能,通过设置 <code>PlaceHolderTemplate</code> 自定义占位内容" Name="PlaceHolder">
    <div>图片加载后浏览器默认会缓存,建议 <kbd>F12</kbd> 关闭缓存体验此功能</div>
    <div class="images img-ph mt-3">
        <div class="images-item">
            <div>默认</div>
            <ImageViewer Url="_content/BootstrapBlazor.Shared/images/image-ph.jpeg" ShowPlaceHolder="true" />
        </div>
        <div class="images-item">
            <div>自定义</div>
            <ImageViewer Url="_content/BootstrapBlazor.Shared/images/image-ph.jpeg" ShowPlaceHolder="true">
                <PlaceHolderTemplate>
                    <div class="bb-img-holder">
                        <div class="bb-img-loading">加载中 ...</div>
                    </div>
                </PlaceHolderTemplate>
            </ImageViewer>
        </div>
    </div>
</DemoBlock>

<DemoBlock Title="占位模板" Introduction="可通过设置 <code>PlaceHolderTemplate</code> 开启占位模板功能,未设置 <code>Url</code> 或者正在加载时显示此模板内容" Name="PlaceHolderTemplate">
    <div>图片加载后浏览器默认会缓存,建议 <kbd>F12</kbd> 关闭缓存体验此功能</div>
    <div class="images img-ph mt-3">
        <div class="images-item">
            <div>未设置 Url</div>
            <ImageViewer Url="">
                <PlaceHolderTemplate>
                    <div class="bb-img-holder">
                        <div class="bb-img-loading">占位模板</div>
                    </div>
                </PlaceHolderTemplate>
            </ImageViewer>
        </div>
        <div class="images-item">
            <div>加载时显示</div>
            <ImageViewer Url="_content/BootstrapBlazor.Shared/images/image-ph.jpeg" ShowPlaceHolder="true">
                <PlaceHolderTemplate>
                    <div class="bb-img-holder">
                        <div class="bb-img-loading">占位模板</div>
                    </div>
                </PlaceHolderTemplate>
            </ImageViewer>
        </div>
    </div>
</DemoBlock>

<DemoBlock Title="加载失败" Introduction="可通过设置 <code>ErrorTemplate</code> 开启错误模板功能,参数 <code>Url</code> 无法加载图片时显示此模板内容" Name="ErrorTemplate">
    <div class="images img-ph mt-3">
        <div class="images-item">
            <div>Url 路径错误</div>
            <ImageViewer Url="_content/BootstrapBlazor.Shared/images/error-image.jpeg" HandleError="true" />
        </div>
        <div class="images-item">
            <div>自定义</div>
            <ImageViewer Url="_content/BootstrapBlazor.Shared/images/error-image.jpeg">
                <ErrorTemplate>
                    <div class="bb-img-holder">
                        <div class="bb-img-loading">加载失败</div>
                    </div>
                </ErrorTemplate>
            </ImageViewer>
        </div>
    </div>
</DemoBlock>

<DemoBlock Title="大图预览" Introduction="可通过设置 <code>PreviewList</code> 开启预览大图的功能" Name="PreviewList">
    <div class="images img-ph mt-3">
        <div class="images-item">
            <ImageViewer Url="_content/BootstrapBlazor.Shared/images/bird.jpeg" PreviewList="PreviewList" />
        </div>
    </div>
</DemoBlock>

<AttributeTable Items="@GetAttributes()" />
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

namespace BootstrapBlazor.Shared.Samples;

/// <summary>
/// Images 示例类
/// </summary>
public partial class ImageViewers
{
    private List<string> PreviewList { get; } = new();

    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        PreviewList.AddRange(new string[]
        {
            "_content/BootstrapBlazor.Shared/images/ImageList1.jpeg",
            "_content/BootstrapBlazor.Shared/images/ImageList2.jpeg"
        });
    }

    private static IEnumerable<AttributeItem> GetAttributes() => new AttributeItem[]
    {
        new AttributeItem() {
            Name = nameof(ImageViewer.Url),
            Description = "图片 Url",
            Type = "string",
            ValueList = " — ",
            DefaultValue = " — "
        },
        new AttributeItem() {
            Name = nameof(ImageViewer.Alt),
            Description = "原生 alt 属性",
            Type = "string",
            ValueList = " — ",
            DefaultValue = " — "
        },
        new AttributeItem() {
            Name = nameof(ImageViewer.ShowPlaceHolder),
            Description = "是否显示占位符 适用于大图片加载",
            Type = "bool",
            ValueList = "true|false",
            DefaultValue = "false"
        },
        new AttributeItem() {
            Name = nameof(ImageViewer.HandleError),
            Description = "加载失败时是否显示错误占位符",
            Type = "bool",
            ValueList = "true|false",
            DefaultValue = "false"
        },
        new AttributeItem() {
            Name = nameof(ImageViewer.PlaceHolderTemplate),
            Description = "占位模板 未设置 Url 或者正在加载大图时生效",
            Type = "RenderFragment",
            ValueList = "true|false",
            DefaultValue = "false"
        },
        new AttributeItem() {
            Name = nameof(ImageViewer.ErrorTemplate),
            Description = "错误模板 图片路径错误时生效",
            Type = "RenderFragment",
            ValueList = "true|false",
            DefaultValue = "false"
        },
        new AttributeItem() {
            Name = nameof(ImageViewer.FitMode),
            Description = "原生 object-fit 属性",
            Type = "ObjectFitMode",
            ValueList = "fill|contain|cover|none|scale-down",
            DefaultValue = "fill"
        },
        new AttributeItem() {
            Name = nameof(ImageViewer.ZIndex),
            Description = "原生 z-index 属性",
            Type = "int",
            ValueList = " — ",
            DefaultValue = "2050"
        },
        new AttributeItem() {
            Name = nameof(ImageViewer.PreviewList),
            Description = "预览大图链接集合",
            Type = "List<string>",
            ValueList = " — ",
            DefaultValue = " — "
        },
        new AttributeItem() {
            Name = nameof(ImageViewer.OnLoadAsync),
            Description = "图片加载成功时回调方法",
            Type = "Func<string, Task>",
            ValueList = " — ",
            DefaultValue = " — "
        },
        new AttributeItem() {
            Name = nameof(ImageViewer.OnErrorAsync),
            Description = "图片加载失败时回调方法",
            Type = "Func<string, Task>",
            ValueList = " — ",
            DefaultValue = " — "
        }
    };
}

B station related video link

No

交流群

QQ Group:BootstrapAdmin & Blazor 795206915 675147445 Welcome to join the group discussion
img
Themes
Bootstrap
Motronic
Ant Design (完善中)
DevUI (制作中)
LayUI (完善中)
An error has occurred. This application may no longer respond until reloaded. Reload
Seems like the connection with the server has been lost. It can be due to poor or broken network. Please hang on while we're trying to reconnect...
Oh snap! Failed to reconnect with the server. This is typically caused by a longer network outage, or if the server has been taken down. You can try to reconnect, but if that does not work, you need to reload the page.
Oh man! The server rejected the attempt to reconnect. The only option now is to reload the page, but be prepared that it won't work, since this is typically caused by a failure on the server.
Bootstrap Blazor Component library updated to 7.0.8-beta05

Bootstrap Blazor at present has more than 120 components. This component is based on Bootstrap Blazor An enterprise-level component library that provides several types of common components such as layout, navigation, form, data, notification, icon, voice, etc. Each component has been carefully designed with modularity, responsiveness and excellent performance. Starting from more practical scenarios, meeting the needs of various scenarios, greatly reducing the time cost of developers, greatly shortening the development cycle, greatly improving development efficiency, and providing a set of General Rights Management System Example project。Bootstrap Blazor Products are maintained by a professional full-time technical team, with efficient response speed, diversified solutions, long-term support, and enterprise-level support. At present, it has been used in many well-known state-owned enterprises, and the project is running stably with a maximum of 1200 people online. On the right is the QR code of the Chinese Blazor QQ community with the largest number of people in China, welcome to scan and join the group.

component updated to 6.6.0 Change log [portal] If the component brings you convenience, please help to light up the project Star github gitee

QQGroup
QQ 795206915