jquery - 平铺异步加载的图像 - 卡在 <img> 和背景 URL 之间

标签 jquery html image css tiles

对于一个有很多小缩略图的网站,我想在加载每张图片时使用占位符 gif。这需要如下代码:

$('.this_img').each(function () {
    var $this_img = $(this);
    var $this_src = $(this).find('.sliderImage:first').attr('src');
    var img = new Image();
    // wrap our new image in jQuery, then:
    $(img)
        // set minimum dimensions
        .css({
            'min-height': '35px',
            'min-width': '40px'
        })
        // once the image has loaded, execute this code
        .load(function () {
                // set the image hidden by default    
                $(this).hide();

                // with the holding div #loader, apply:
                $this_img
                    // remove the loading class (so no background spinner), 
                    .removeClass('loading')
                    // then insert our image
                    .prepend(this);

                // fade our image in to create a nice effect
                $(this).fadeIn();
            })

        // if there was an error loading the image, react accordingly
        .error(function () {
            // notify the user that the image could not be loaded
        })

        // *finally*, set the src attribute of the new image to our image
        .attr('src', $this_src);
});

这很好用,在样式表中指定了占位符,但我希望能够平铺这些图像,如果它们太短或太窄,背景 CSS 是做到这一点的明显方法,但它与异步图像加载,afaik。

我的问题:是否有任何 jQuery(或 CSS3)可用于平铺 <img>标签?

或者,有没有办法异步加载图像,然后将其粘贴到背景 css 属性中?这听起来有点太神奇了,但也许行得通。

最佳答案

你能发布你的 html 标记结构吗?否则是盲目的,但是您是否尝试修改包含 div 的背景属性?

// your callback function

function () {
    // set the image hidden by default    
    $(this).hide(); 

    // with the holding div #loader, apply:
    $this_img
      // remove the loading class (so no background spinner), 
      .removeClass('loading')
      // then insert our image
     //.prepend(this)

.css('background','transparent url("'+$this_src+'") top left repeat');

    // fade our image in to create a nice effect
    $(this).fadeIn();
  }

关于jquery - 平铺异步加载的图像 - 卡在 <img> 和背景 URL 之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1198431/

相关文章:

javascript - 在选择图像时预览编辑图像的图像(即时)

android - 在Android上使用Camera2进行OpenCV图像处理

javascript - 如果我尝试对尚未完全加载的图像使用 drawImage() 会发生什么?

jquery - 如何在 jQuery 中定位较早的非同级元素?

javascript - 我们如何使用 jQuery 在浏览器中禁用(而不是隐藏)滚动条?

javascript - 如何使用javascript在html中用按钮清除文本区域?

javascript - 如何使用 VueJS 动态添加属性?

java - 强制显示 JLabel 子类

javascript - 如果不存在则申请类(class)

使用本地文件的 jQuery .getJSON 在 Firefox 3.6.13 上停止工作