Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 随机图像;不重复?_Javascript_Jquery_Jquery Plugins - Fatal编程技术网

Javascript 随机图像;不重复?

Javascript 随机图像;不重复?,javascript,jquery,jquery-plugins,Javascript,Jquery,Jquery Plugins,首先,我对javascript不是很熟悉,所以我在这里。 我有这个代码为我的网站绘制一个随机图像。从这个角度出发,我如何才能使图像不重复?谢谢你!代码: <script type="text/javascript"> var banner_list = ['http://i1233.photobucket.com/albums/ff389/lxluigixl/Cargo/LM_LogoMark4-4-2.gif', 'http://i1233.photobucket.com

首先,我对javascript不是很熟悉,所以我在这里。 我有这个代码为我的网站绘制一个随机图像。从这个角度出发,我如何才能使图像不重复?谢谢你!代码:

<script type="text/javascript">  

var banner_list = ['http://i1233.photobucket.com/albums/ff389/lxluigixl/Cargo/LM_LogoMark4-4-2.gif',   'http://i1233.photobucket.com/albums/ff389/lxluigixl/Cargo/logobg_dome.png',  'http://i1233.photobucket.com/albums/ff389/lxluigixl/Cargo/logobg_brain.png'];    $(document).ready(function() { var ran = Math.floor(Math.random()*banner_list.length);
$(".logobg img").attr(banner_list[ran]);
});  $(document).bind("projectLoadComplete", function(e, pid){
var ran = Math.floor(Math.random()*banner_list.length);
$(".logobg img").attr("src", banner_list[ran]);
}); </script>

显示图像后,将其从阵列中拼接出来,可以使用。参数为.stipleindex、howManyToRemove、howManyToInsert。插入是可选的,因此您可以使用splice从正在显示的图像的索引开始,然后删除一个。确保在完成引用之前不要删除它。

您可以使用Array.splice,正如Robert建议的那样,使用2个数组。一个用于未使用的图像,一个用于已使用的图像。检查我的电脑


谢谢你,罗伯特。虽然我真的需要一个有时间为我写代码的人。我不是那么精通java。我已经想了好几天了,但都没用。这就是代码。banner_list.Ran,1;在您通过数组使用完图像之后,将其放在右边,在您的示例中,数组是在您设置图像的src之后。另外,Java与Javascript非常不同,仅供将来参考。噢,非常感谢robert。我是一名平面设计师,正如你所看到的,我对编程一无所知。感谢您的澄清;
var images = ["http://www.fowkesauto.com/products_pictures/nutsbolt.jpg",
              "http://i01.i.aliimg.com/photo/v0/114511763/Fasteners_Bolts_and_Nuts.jpg",
              "http://us.123rf.com/400wm/400/400/DLeonis/DLeonis0810/DLeonis081000018/3706757-bolts-and-nuts-on-white.jpg",
              "http://static3.depositphotos.com/1003288/173/i/950/depositphotos_1737203-Nuts-and-bolts.jpg"],
    usedImages = [];

setInterval(function () {changeImage();},500);

var changeImage = function () {
    var index = Math.floor(Math.random() * (images.length)),
        thisImage = images[index];

        usedImages.push(thisImage);
        images.splice(index, 1);

        if (images.length < 1) {
            images = usedImages.splice(0, usedImages.length);
        }

        $("#image").attr("src", thisImage);
}