JS预加载图像错误

JS PreLoading Images Error

本文关键字:错误 图像 加载 JS      更新时间:2023-09-26

我创建了一个数组,其中包含页面上所有图像的URL,然后将该数组发送到一个函数,该函数又加载每个图像。然后,页面将加载布局。但是,我收到错误:

Uncaught TypeError: object is not a function

任何帮助非常感谢

.JS:

$(document).ready(function () {
    preLoadImages();
    useIsotope();
});
function useIsotope() {
    var $container = $('#work').isotope({
        filter: "*"
    });
    $('#control ul li a').click(function () {
        var selector = $(this).attr('data-filter');
        $container.isotope({ filter: selector });
        return false;
    });
}
function preload(arrayOfImages) {
    $(arrayOfImages).each(function () {
        $('<img/>')[0].src = this;
    });
}
function preLoadImages() {
    var imageArray = new Array();
    $('.imgWrapper a img').each(function (index) {
        imageArray.push(this.src)
    });
    console.log(imageArray)
    preLoad(imageArray) // HERE IS THE ERROR
}

使用 preload(imageArray) 而不是 preLoad(imageArray)

JavaScript 区分大小写。(来源)

我看不到您评论的错误,但这看起来像一个错误。

您正在设置 $container = $('#work').isotope({ 过滤器:"*" });

然后尝试呼叫

$container.isotope({ filter: selector });

我想你可能想这样写。

function useIsotope(){
  var $container = $('#work');
  $container.isotope({ filter: "*" });
  $('#control ul li a').click(function(){
    var selector = $(this).attr('data-filter');
    $container.isotope({ filter: selector });
    return false;
  });
}

阅读@Guy的答案后更新,我也看到了预加载与预加载错误。