简体   繁体   English

在ASP.NET C#中的图像加载上加载临时图像

[英]Loading Temp Image on Image load in ASP.NET C#

I am loading some images on my page (from File Path) and they can be heavy, so I created .min versions and try to load miniature first and then load original image when it is ready. 我正在页面上(从文件路径)加载一些图像,这些图像可能很重,因此我创建了.min版本,并尝试先加载缩影,然后在就绪时加载原始图像。

However I cannot seem to be able to render the original file. 但是我似乎无法渲染原始文件。

Here is how I am trying to achieve this: 这是我试图实现的方法:

<script>
$(document).ready(function () {
    [].forEach.call(document.querySelectorAll('img[data-src]'), function (img) {
        img.setAttribute('src', img.getAttribute('data-src'));
        img.onload = function () {
            img.removeAttribute('data-src');
        };
    });
});

<div class="featuredImageBox" runat="server">
        <img class="center-fit" src="~/@Model.Owner/min/@Model.Featured1A"  data-src="~/@Model.Owner/@Model.HomeFeatured1A"/>
    </div>

the miniature from src loads correctly, however the data-src original file does not load. src的缩图可以正确加载,但是data-src原始文件无法加载。 Is this because I am loading image sdynamically? 这是因为我正在动态加载图像吗?

I fetch images using SQL DB where I store filenames and path. 我使用存储文件名和路径的SQL DB提取图像。

Is it possible to overcome this problem? 有可能克服这个问题吗?

You are calling .forEach on an empty array, so nothing will be processed. 您在一个空数组上调用.forEach ,因此将不进行任何处理。 You will need to operate on all images with a data-src attribute: 您将需要对所有具有data-src属性的图像进行操作:

$(document).ready(function () {
    document.querySelectorAll('img[data-src]').forEach(function (img) {
        img.setAttribute('src', img.getAttribute('data-src'));
        img.onload = function () {
            img.removeAttribute('data-src');
        };
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM