首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用javascript加载智能图像

用javascript加载智能图像
EN

Stack Overflow用户
提问于 2013-10-18 08:16:24
回答 1查看 271关注 0票数 1

这里是我的场景,我们有一个猫的主要图像(假设它的1000 10乘以1000 10)和10只猫的缩略图(100 10乘100 10)。当选择拇指时,主图像会发生变化。目前,这些图像正在使用

代码语言:javascript
运行
复制
$('#showcase .thumbnail').each(function(index,el){
    var mainImg = $(el)[0].src.replace('small','large');
    var img = new Image();
    img.src = mainImg;
}); 

这将加载我的缩略图的大版本。

我有一个<select>标签,有四个<option>标签,当选择一个选项时,10个缩略图图像会和主图像一样发生变化。用户对猫没有兴趣,几乎马上就会选择狗狗选项。

问题

猫图像能被推到图片列表的后面下载并排序狗图像吗?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-25 13:24:49

让我们假设select看起来如下:

代码语言:javascript
运行
复制
<select id="select">
    <option value="">Please select</option>
    <option value="dogs">Dogs</option>
    <option value="cats">Cats</option>
    <option value="fishes">Fishes</option>
</select>

并且每个映像都有一个与值(例如<img class="cat" src=... />)相对应的类名,下面是一些代码(没有对其进行彻底测试):

代码语言:javascript
运行
复制
// is something selected in the dropdown
var selection = false;
// observe the dropdown
$('#select').change(function(){
    // the user selected something -> store it
    if(this.value) {
        selection = this.value;
    }
    // back to default
    else {
        selection = false;
    }
});

// store the images
var images = $('#showcase .thumbnail');
// stack for the unreplaced images
var stack = [];

// first run
function replaceImages(images, currentIndex) {
    // we're finished
    if (imageURLarray.length == 0 || images.length == currentIndex) {
        return false;
    }

    // there's no selection at all or we have a selection and the current image has the corresponding class -> replace it
    if(!selection || (selection && $(images[currentIndex]).hasClass(selection))) {
        var mainImg = $(images[currentIndex])[0].src.replace('small','large');
        var img = new Image();
        img.src = mainImg;
        // wait for loading. If we wouldn't wait, all image src's would be replaced in a second and our prioritizing magic wouldn't happen
        img.onload = function(e){
            // image has loaded -> next image
            replaceImages(images, currentIndex++);
        }
    }
    // there's a selection and the class doesn't match -> store the image in the stack
    else {
        stack.push(currentIndex);
    }
}

// run the function
replaceImages(images, 0) {

// process the stack, i.e. the non-replaced images
$(stack).each(function(index, val){
    var mainImg = $(images[val]).src.replace('small','large');
    var img = new Image();
    img.src = mainImg;
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19444774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档