简体   繁体   English

上/下移动时颜色变化

[英]Color changing while moving Up/Down

I have a little problem with that JS..我对那个JS有点问题..

-Have a divs and this is changing automatic in row where they are changing randomly... - 有一个divs ,这是在随机更改的行中自动更改...

  • I need to change the color to green for the the div which is moving up..我需要将向上移动的div的颜色更改为绿色。
  • And for div which is moving down to change for red..而对于向下移动以更改为红色的div ..

I tried to do that but i did just that while moving up and down change same color..我试图这样做,但我只是在上下移动时改变了相同的颜色。

So should i did a class or id to check the divs position and in JS set something like :所以我应该做一个classid来检查 div 位置并在 JS 中设置如下内容:

... check the position of div ... if moving up like change the position to lower value change 'green' ... else change to lower value like from '2' to '5' change for 'red' ... check div 的位置 ... if向上移动,例如将位置更改为较低的值更改“绿色”... else更改为较低的值,例如从 '2' 到 '5' 更改为 'red'

Here is the sample how its work now: CLICK HERE FOR SAMPLE这是它现在如何工作的示例:单击此处获取示例

Here is the main file for JS:MAIN JS FILE for MixItUp这是 JS 的主文件:MixItUp 的MAIN JS FILE

 $(document).ready(function () { var mixit = $('#Container').mixItUp({ load: { sort: 'random' }, layout: { containerClass: 'list', display: 'block' } }); function loop() { mixit.mixItUp('sort', 'random'); }; var looper = setInterval(loop, 20000); });
 *{ -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } body { background: #68b8c4; } .mix.category-1 { height: 50px; } #Container .mix { border: 1px solid black; margin-top: 1px; background-color: white; } .container{ padding: 20px 0 0; text-align: center; font-size: 0.1px; margin-left: 35%; -webkit-backface-visibility: hidden; } .container:after{ content: ''; display: inline-block; width: 100%; } .container .mix, .container .gap{ display: inline-block; width: 49%; } .container .mix{ text-align: center; margin-bottom: 0; display: none; } .container .mix:after{ content: attr(data-myorder); color: black; font-size: 16px; display: inline-block; vertical-align: top; padding: 4% 6%; font-weight: 700; } .container .mix:before{ content: ''; display: inline-block; padding-top: 60%; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="http://cdn.jsdelivr.net/jquery.mixitup/latest/jquery.mixitup.min.js?v=2.1.9"></script> <div id="Container" class="container"> <div class="mix category-1" data-myorder="1"></div> <div class="mix category-1" data-myorder="2"></div> <div class="mix category-1" data-myorder="3"></div> <div class="mix category-1" data-myorder="4"></div> <div class="mix category-1" data-myorder="5"></div> <div class="mix category-1" data-myorder="6"></div> <div class="mix category-1" data-myorder="7"></div> <div class="mix category-1" data-myorder="8"></div> </div>

Here is the corrected js:这是更正后的js:

$(document).ready(function () {
  var mixit = $('#Container').mixItUp({
    load: {
      sort: 'random'
    },
    layout: {
      containerClass: 'list',
      display: 'block'
    }
  });

  function loop() {
    var arr = [];
    i = 0;
    $('.mix').each(function(){
      arr[i++] = $(this).data('myorder');
    });
    mixit.mixItUp('sort', 'random');

    mixit.on('mixEnd', function(e, state){
      var arr2 = [];
    i2 = 0;
    $('.mix').each(function(){
      arr2[i2++] = $(this).data('myorder');
    });
    for(i=0; i<i2; i++){
      for(j=0; j<i2; j++){
        if(arr[i]==arr2[j]){
          if(i<j){
            $('.mix').eq(j).css("background-color", "red");
          }
          if(i>j){
            $('.mix').eq(j).css("background-color", "green");
          }
        }
      }
    }
    });
  };

  var looper = setInterval(loop, 3000);
});

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM