jQuery 循环插件 - 显示来自同一组的多个图像,不重复

发布于 2024-12-13 03:18:49 字数 2505 浏览 1 评论 0原文

设置

我有一组约 20 张图像。我想一次显示 6 个图像,并使用 jQuery Cycle 插件随机旋转该组中的不同图像。我拼凑了一个解决方案,我多次调用 Cycle 插件 6 次,并将每个图像都列在 6 个 div 中。当前的解决方案大致基于 这个问题/答案

问题

我不想显示重复的图像。现在经常会同时显示 2 个甚至 3 个重复项。这看起来相当愚蠢!我假设这将涉及根据显示的图像对数组进行某种形式的重写(有办法告诉吗?),但我真的不知道如何去做。

任何帮助将不胜感激!谢谢你!

当前代码:

$(document).ready(function(){
    var NumImages = 20, // Number of logos to show
        FirstPart = '<img src="/DEV/demo/images/',
        LastPart = '.jpg" height="50" width="100" />',
        array = ['bali.sm',          'disney.sm', 'fisherprice.sm',
                 'fruit.of.loom.sm', 'gerber.sm', 'hamilton.beach.sm',
                 'hotwheels.sm',     'igloo.sm',  'magnavox.sm',
                 'matchbox.sm',      'mohawk.sm', 'playtex.sm',
                 'purina.sm',        'rca.sm',    'remington.sm',
                 'rubbermaid.sm',    'shark.sm',  'sony.sm',
                 'sunbeam.sm',       'tonka.sm'], // array with all image names
        rnd, value, i;

    for (i = 0; i < NumImages; i++) { // pick numbers
        rnd = Math.floor(Math.random() * array.length); 
        value = array.splice(rnd,1)[0]; // remove the selected number from the array and get it in another variable

        document.getElementById('pic1').innerHTML += (FirstPart + value + LastPart);
        document.getElementById('pic2').innerHTML += (FirstPart + value + LastPart);
        document.getElementById('pic3').innerHTML += (FirstPart + value + LastPart);
        document.getElementById('pic4').innerHTML += (FirstPart + value + LastPart);
        document.getElementById('pic5').innerHTML += (FirstPart + value + LastPart);
        document.getElementById('pic6').innerHTML += (FirstPart + value + LastPart);
    }
    $("#pic1").cycle({ fx: 'fade', speed: 2000, timeout: 28000, random: 1, delay: -15000 });
    $("#pic2").cycle({ fx: 'fade', speed: 2000, timeout: 28000, random: 1, delay: -10000 });
    $("#pic3").cycle({ fx: 'fade', speed: 2000, timeout: 28000, random: 1, delay: -5000 });
    $("#pic4").cycle({ fx: 'fade', speed: 2000, timeout: 28000, random: 1, delay: 0 });
    $("#pic5").cycle({ fx: 'fade', speed: 2000, timeout: 28000, random: 1, delay: 5000 });
    $("#pic6").cycle({ fx: 'fade', speed: 2000, timeout: 28000, random: 1, delay: 10000 });
});

Setup:

I have a set of ~20 images. I want to display 6 of them at a time and use the jQuery Cycle plugin to randomly rotate in different images from the set. I've cobbled together a solution where I call the Cycle plugin 6 different times and have every image listed in each of the 6 div's. The current solution is roughly based on this question/answer.

Problem:

I don't want duplicate images to show. Right now there will frequently be 2 or even 3 duplicates showing at the same time. This looks rather silly! I'm assuming this would involve some kind of re-writing of the array on the fly based on what image is showing (is there a way to tell?) but i'm really stuck on how to go about it.

Any help would be appreciated! Thank you!

Current Code:

$(document).ready(function(){
    var NumImages = 20, // Number of logos to show
        FirstPart = '<img src="/DEV/demo/images/',
        LastPart = '.jpg" height="50" width="100" />',
        array = ['bali.sm',          'disney.sm', 'fisherprice.sm',
                 'fruit.of.loom.sm', 'gerber.sm', 'hamilton.beach.sm',
                 'hotwheels.sm',     'igloo.sm',  'magnavox.sm',
                 'matchbox.sm',      'mohawk.sm', 'playtex.sm',
                 'purina.sm',        'rca.sm',    'remington.sm',
                 'rubbermaid.sm',    'shark.sm',  'sony.sm',
                 'sunbeam.sm',       'tonka.sm'], // array with all image names
        rnd, value, i;

    for (i = 0; i < NumImages; i++) { // pick numbers
        rnd = Math.floor(Math.random() * array.length); 
        value = array.splice(rnd,1)[0]; // remove the selected number from the array and get it in another variable

        document.getElementById('pic1').innerHTML += (FirstPart + value + LastPart);
        document.getElementById('pic2').innerHTML += (FirstPart + value + LastPart);
        document.getElementById('pic3').innerHTML += (FirstPart + value + LastPart);
        document.getElementById('pic4').innerHTML += (FirstPart + value + LastPart);
        document.getElementById('pic5').innerHTML += (FirstPart + value + LastPart);
        document.getElementById('pic6').innerHTML += (FirstPart + value + LastPart);
    }
    $("#pic1").cycle({ fx: 'fade', speed: 2000, timeout: 28000, random: 1, delay: -15000 });
    $("#pic2").cycle({ fx: 'fade', speed: 2000, timeout: 28000, random: 1, delay: -10000 });
    $("#pic3").cycle({ fx: 'fade', speed: 2000, timeout: 28000, random: 1, delay: -5000 });
    $("#pic4").cycle({ fx: 'fade', speed: 2000, timeout: 28000, random: 1, delay: 0 });
    $("#pic5").cycle({ fx: 'fade', speed: 2000, timeout: 28000, random: 1, delay: 5000 });
    $("#pic6").cycle({ fx: 'fade', speed: 2000, timeout: 28000, random: 1, delay: 10000 });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏有森光若流苏 2024-12-20 03:18:49

我可能会构建一个临时数组来存储随机生成的数字,然后每次检查它:

var tmparray = [];
for (var i=0; i<NumImages; i++) { // pick numbers
    rnd = -1; 
    while (tmparray.indexOf(rnd) == -1) {
        rnd = Math.floor(Math.random() * array.length); 
    }
    tmparray.push(rnd);
    value = array.splice(rnd,1)[0];

    ...

} // end for

I might construct a temporary array to store the numbers I've randomly generated, and then check it each time:

var tmparray = [];
for (var i=0; i<NumImages; i++) { // pick numbers
    rnd = -1; 
    while (tmparray.indexOf(rnd) == -1) {
        rnd = Math.floor(Math.random() * array.length); 
    }
    tmparray.push(rnd);
    value = array.splice(rnd,1)[0];

    ...

} // end for
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文