Webpack4 H5 loading page picture preload

problem description

H5 pictures of more projects, the general practice is to do a loading page, before entering the H5 need to load all the images, my previous practice is to JS there to write the path of each picture, and then use a function to create an img tag to load.
I began to learn to use wepack4 to build a project. Instead of using the previous practice of directly quoting JQ to dry, the problem now is that writing an image path in the entry file of webpack will report an error. It is no problem to introduce it in HTML. The url-loader used is only useful for packaging.

the environmental background of the problems and what methods you have tried

related codes

previously preloaded code

var imgSrc = [
    "img/loading.png",
    "img/banner.jpg",
    "img/t1.png",
    "img/t2.png",
    "img/t3.png",
    "img/t4.png",
    "img/p1-text1.png",
    "img/p1-text2.png",
    "img/p1.jpg",
    "img/flag.png",
    "img/play.png",
    "img/p2-p1.png",
    "img/p2-p2.png",
    "img/p2-p3.png",
    "img/p2-p4.png",
    "img/p2-t1.png",
    "img/p2-t2.png",
    "img/p2-t3.png",
    "img/p2-t4.png",
    "img/p2-tc.png",
    "img/p2-text1.png",
    "img/p2-text2.png",
    "img/p3-text1.png",
    "img/p3-text2.png",
    "img/p3-text4.png",
    "img/p3-text3.png",
    "img/p3-text5.png",
    "img/p4-bottom.png",
    "img/p4-text1_2.png",
    "img/p4-text2.png",
    "img/poster-2.png"
];

perload2(imgSrc, function () {
    $(".loading").hide()
    $("-sharpcontainer").show()  
    
})

//
function perload2(imgSrc, callback) {
    var imgs = [];
    var c = 0;
    for (var i = 0; i < imgSrc.length; iPP) {
        imgs[i] = new Image();
        imgs[i].src = imgSrc[i];
        imgs[i].onload = function () {
            cPP
            var num = parseInt((c / imgSrc.length) * 100);
            var poress = num + "%"
            $(".loading_num").text(poress) //
            if (c == imgSrc.length) {
                $(".loading_num").text("100%")

                if (callback) {
                    setTimeout(function () {
                        callback();
                    }, 500)
                }
            }
        }
    }
    return imgs; //
}

effect of previous projects:

webpack


main.js:

importwebpackH5loading

:

Nov.15,2021

and other bigwigs to answer


directory level take a look at
but should be able to solve


through webpack's reslove configuration.

if you use js to import pictures, webpack local developers do not have your directory. Local developers should also package a dist directory for browsers to access. This directory is a static resource that your browser can access, such as / img/.. There is certainly no such directory, and img also has a hash value, which can't be correct anyway. Why don't you try this?

var imgSrc = [
    require('./img/loading.png')
];

take a look at my solution. Now is to use a node crawl folder to collect pictures to generate js files into the project, and then introduce dynamic creation tags in index.js to introduce the js file to get the picture list, one by one request, so that the later use of the image cache. Although it has been solved, it is not beautiful. In the future, it will be modified to write a json file in the part of dealing with pictures that loader collects and uses to generate a list of pictures to the picture directory. Then write the preloaded js. https://github.com/outsourcin. in the html file through plugin.

Menu