简体   繁体   English

JS检测图像元素src更改并加载

[英]JS detect image element src change and loaded

I'm building a portfolio website with an image gallery. 我正在建立带有图片库的作品集网站。 The active gallery image is loaded by changing the src and alt of the image element, rather than loading a new image element and removing the old one. 通过更改图像元素的src和alt来加载活动的画廊图像,而不是加载新的图像元素并删除旧的图像元素。

I've got everything working except a loading icon for when the active image changes. 除了活动图像更改时的加载图标外,我一切正常。 I've tried using Javascript to display the icon when a thumbnail is clicked and then hiding it onLoad, but that isn't working. 我尝试使用Javascript在单击缩略图时显示图标,然后将其隐藏在onLoad上,但这不起作用。

Can anyone advise how best to detect when the image src has changed and the new image src has fully loaded? 任何人都可以建议如何最好地检测图像src何时更改并且新图像src已完全加载吗? I've copied my full JS below if it helps. 如果有帮助,我已在下面复制了完整的JS。 The function for the loader icon is at the very bottom. 加载程序图标的功能在最底部。

I'm using basic JavaScript so ideally want to stay away from any jQuery solutions if possible. 我正在使用基本的JavaScript,因此理想情况下,如果可能的话,希望远离任何jQuery解决方案。 Thanks! 谢谢!

    var galleryCloseIcon = document.getElementById('galleryClose');
    var galleryLoader = document.getElementById('galleryLoader');
    var galleryNext = document.getElementById('galleryNext');
    var galleryPrevious = document.getElementById('galleryPrevious');
    var galleryViewer = document.getElementById('galleryViewer');
    var galleryViewerBg = document.getElementById('galleryViewerBg');
    var galleryViewerExpand = document.getElementById('galleryViewerExpand');
    var galleryViewerImage = document.getElementById('galleryViewerImage');
    var imageNodes = document.querySelectorAll('.gallery__image');

    var imageIndex = 0;

    //Loop through images to find image index
        var indexLoop = function() {
        for (var i = 0; i < imageNodes.length; i++) {
            var thumbimage = imageNodes[i].src.split("-thm")[0] + ".jpg";
            if (thumbimage === galleryViewerImage.src) {
                imageIndex = i;
            }
        }
    };

    // Open Gallery
    var galleryOpen = function() {

        // Open gallery viewer
        galleryViewer.classList.add('gallery__viewer--open');
        indexLoop();

    };

    // Open gallery fullscreen on desktop
    var galleryOpenDesk = function() {
        galleryViewer.classList.add('gallery__viewer--open-desk');
    };

    // Close gallery
    var galleryClose = function() {
        galleryViewer.classList.remove('gallery__viewer--open');
        galleryViewer.classList.remove('gallery__viewer--open-desk');
    };

    // Get image data
    var imageData = function(item) {
        imageLoader(item);
        galleryViewerImage.alt = item.alt;

        // Get image src by removing -thm suffix from thumbnails
        var galleryViewerLarge = item.src.split("-thm")[0] + ".jpg";
        galleryViewerImage.src = galleryViewerLarge;
    };

    // Functions for image event listeners
    var imageClick = function() {
        var $this = this;
        imageData($this);
        galleryOpen();
    };

    // Add event listeners to images
    for (var i = imageIndex; i < imageNodes.length; i++) {
        imageNodes[i].addEventListener("click", imageClick);
    }

    // Next / previous buttons
    var changeImage = function(n) {
        if (n > imageNodes.length -1) {
            imageIndex = 0;
        } else if (n < 0) {
            imageIndex = imageNodes.length -1;
        } else {
            imageIndex = n;
        }
        imageData(imageNodes[imageIndex]);
        return imageIndex;
    };

    var imageNext = function() {
        changeImage(imageIndex + 1);
    };
    var imagePrevious = function() {
        changeImage(imageIndex -1);
    };

    // Loading image spinner
    var imageLoader = function(image) {
        galleryLoader.classList.add('gallery__viewer-icon--loader-on');
        image.onload = function() {
            galleryLoader.classList.remove('gallery__viewer-icon--loader-on');
        };
    };

Figured it out. 弄清楚了。 Instead of checking if the active image had loaded, I was checking if the node had loaded. 我没有检查活动映像是否已加载,而是在检查节点是否已加载。 If it's of use to anyone, the loader function should have been this; 如果任何人都可以使用它,则加载程序功能应该就是这个;

    var imageLoader = function(image) {
    galleryLoader.classList.add('gallery__viewer-icon--loader-on');
    galleryViewerImage.onload = function() {
        galleryLoader.classList.remove('gallery__viewer-icon--loader-on');
    };
};

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

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