javascript - JS InfiniteRotator 随机顺序

标签 javascript jquery html css

我的网站上有一个图像旋转器,但我希望图像以随机顺序出现,这样相同的图像并不总是最先出现。我有旋转器工作,但我不知道如何随机化它。我的代码在 JS Bin 上:http://jsbin.com/dogimawuli/edit?html,output

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Test</title>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js?ver=3.0.1"></script>
  <script>
  $(window).load(function() {   

    var InfiniteRotator = 
  {
    init: function()
    {
        //initial fade-in time (in milliseconds)
        var initialFadeIn = 1000;

        //interval between items (in milliseconds)
        var itemInterval = 4000;

        //cross-fade time (in milliseconds)
        var fadeTime = 50;

        //count number of items
        var numberOfItems = $('.rotating-item').length;

        //set current item
        var currentItem = 0;

        //show first item
        $('.rotating-item').eq(currentItem).fadeIn(initialFadeIn);

        //loop through the items        
        var infiniteLoop = setInterval(function(){
            $('.rotating-item').eq(currentItem).fadeOut(fadeTime);

            if(currentItem == numberOfItems -1){
                currentItem = 0;
            }else{
                currentItem++;
            }
            $('.rotating-item').eq(currentItem).fadeIn(fadeTime);

        }, itemInterval);   
     }  
  };

  InfiniteRotator.init();

});
  </script>
  <style>
    #rotating-item-wrapper {position: relative;width: 468px;height: 60px;float: right;}
    .rotating-item {display: none;position: absolute;top: 0;left: 0;}
  </style>
</head>
<body>
    <div id="rotating-item-wrapper">
      <div style="width:400px;height:60px;background:red;"class="rotating-item">1</div>
      <div style="width:400px;height:60px;background:green;"class="rotating-item">2</div>
      <div style="width:400px;height:60px;background:blue;"class="rotating-item">3</div>
   </div>
</body>
</html>

最佳答案

试试这个:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Test</title>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js?ver=3.0.1"></script>
  <script>
  $(window).load(function() {   
      debugger;
    var InfiniteRotator = 
  {
    init: function()
    {
        //initial fade-in time (in milliseconds)
        var initialFadeIn = 1000;

        //interval between items (in milliseconds)
        var itemInterval = 4000;

        //cross-fade time (in milliseconds)
        var fadeTime = 50;

        //count number of items
        var numberOfItems = $('.rotating-item').length;

        //set current item
        var currentItem = Math.floor(Math.random() * numberOfItems)


        //show first item
        $('.rotating-item').eq(currentItem).fadeIn(initialFadeIn);

        //loop through the items        
        var infiniteLoop = setInterval(function(){
            $('.rotating-item').eq(currentItem).fadeOut(fadeTime);

            nextItem = currentItem;
            while(currentItem == nextItem) {
                nextItem = Math.floor(Math.random() * numberOfItems);
            }
            currentItem = nextItem;
            $('.rotating-item').eq(currentItem).fadeIn(fadeTime);

        }, itemInterval);   
     }  
  };

  InfiniteRotator.init();

});
  </script>
  <style>
    #rotating-item-wrapper {position: relative;width: 468px;height: 60px;float: right;}
    .rotating-item {display: none;position: absolute;top: 0;left: 0;}
  </style>
</head>
<body>
    <div id="rotating-item-wrapper">
      <div style="width:400px;height:60px;background:red;"class="rotating-item">1</div>
      <div style="width:400px;height:60px;background:green;"class="rotating-item">2</div>
      <div style="width:400px;height:60px;background:blue;"class="rotating-item">3</div>
   </div>
</body>
</html>

基本上你正在循环下一个与当前图像不同的随机数,缺点是一些图像将“饥饿”直到随机数选择该图像编号。

关于javascript - JS InfiniteRotator 随机顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39900973/

相关文章:

javascript - 对象中特定属性值的增变器

javascript - 加载 iframe 后重新加载 iframe

html - 更改 Bootstrap 按钮的颜色

html - 阻止 table 和 td 元素获得焦点

javascript - 如何使用 javascript 进行日计时器?

javascript - Typeahead 和第一个单元格选择器

javascript - 获取值为 true 的 JavaScript 对象的所有键

javascript - Material-UI 对话框样式操作按钮

javascript - 在特定时间段内页面加载时弹出窗口

jquery - 在事件上使用 jQuery 获取被点击的元素?