使用jquery获取两个数组的比率

时间:2014-01-17 15:37:30

标签: jquery arrays

我正在努力开发一个分数栏,用户可以看到他的分数,并看到他可以获得的最高奖项的进展。

问题在于这个酒吧有8个奖项,每个奖项都有不同的奖品。你可以通过只收集200分赢得小奖,或者你可以通过收集50000分赢得旅行。

现在的问题是我必须在酒吧上显示所有八个奖项里程碑,每个奖励里程碑之间的差异空间相同

enter image description here

正如你可以看到用户赢得7800分的图片他达到7500的里程碑现在主要的问题是我必须得到这些点之间的比例将瓶子放在准确的位置上我没有做到:(我试过了这么辛苦但没有发生

你可以在这里看到小提琴:http://jsfiddle.net/Udwq9/2/

HTML

<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>

<div class="barCont">
      <div class="innerLight"></div>
      <div class="innerDark"> </div>
      <div class="seek">
        <div class="youRhere">
          <p>You are here:<br />
            <span id="Points">7800 Points</span></p>
          <br />
        </div>
        <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N//AABEIARYARgMBIgACEQEDEQH/xAAcAAACAwEBAQEAAAAAAAAAAAAFBgAEBwEDCAL/xABGEAABAwMCAgYGBgYIBwEAAAABAgMRAAQFEiEGMQcTIkFRYRRxgZGhsRUyQsHR8CNSYnKy4SU0U3OCksLxJDNEVGR0gzX/xAAaAQABBQEAAAAAAAAAAAAAAAAEAAECAwUG/8QAKREAAgICAgECBgIDAAAAAAAAAAECAwQREjEhIkEFEzIzUWEU8BUjgf/aAAwDAQACEQMRAD8A3CpUqUhCPxvlci3fJtLF5y3YRpDjiBzUrlv4Uv22QbVKbq/yhc5/85Y8N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Kg6L+ICZ9JxnP+2c/V1fqVcs+iDPP6dd/jUTG4W4eYn9UVKlIQcsugu5WQb7PtJQRMM2xJ+KvXTdguiLhnFrC7pL+Sc/8pQ0/5UwPfNcqUwh/ZabZaQ0yhKG0CEoSICR4AVypUphH/9k=" width="17" height="74" alt=" " /> </div>
      <div class="mileStone" style="">200

      </div>
      <div class="mileStone" style="">300

      </div>
      <div class="mileStone" style="">450

      </div><div class="mileStone" style="">750

      </div>
      <div class="mileStone" style="">7500

      </div>
      <div class="mileStone" style="">15000

      </div>
      <div class="mileStone" style="">20000

      </div><div class="mileStone" style="">50000

      </div>
    </div>

SCRIPT

var TopScore = 50000;
    var MileStone = new Array("200", "300", "450", "750", "7500", "15000", "20000", "50000");
    var barWidth = $('.innerLight').width();
    var ArrayLength = MileStone.length;
    var milestonepos = barWidth/ArrayLength;
    var milestoneposMain = milestonepos/ArrayLength;
    var Points = $('#Points').text();
    var yourPoints = parseInt(Points);
    var pos = 0;
    var bottlePos = 0;
    var posTwo = 0;
    var posTwoMain = 0;

    for(var i=0; i <= ArrayLength; i++){    
        var pos = pos + milestonepos;
        $('.mileStone').eq(i).animate({ left: '' + pos + 'px' });
        //alert(pos)        
        }
    var pos = 0;
    for(var j=0; j<= ArrayLength; j++){
        if(yourPoints >= MileStone[j])
        {   

        var pos = pos + milestonepos;

             /*var step1 = yourPoints - MileStone[j];
             var step2 = MileStone[j+1] - yourPoints;
             var step3 = step2/step1;
             var step4 = step3*100;*/
            // var pointsRatio = yourPoints - posRatio;
             //var posTwoMain = parseFloat(posTwo.style.width)
             //alert(parseInt(posRatio));
             //alert(parseInt(pointsRatio));
            // var pos = pos + pointsRatio;
            }

        }
        $('.seek').animate({ left: '' + parseInt(pos) + 'px' }, 1000);

3 个答案:

答案 0 :(得分:3)

这似乎并不太难,但7800是一个不值得测试的值,因为它在7500到15000的范围内从7500开始没什么。(所以在JSfiddle中我把它改为12700)

http://jsfiddle.net/Udwq9/9/

一旦里程碑大于用户的分数,我修改为第二个for-loop中断。

for(var j=0; j < ArrayLength; j++)
{
    if(yourPoints >= MileStone[j])
    {   
        pos = pos + milestonepos;
    }
    else
    {
        prevMilestone = j ? MileStone[j-1] : 0;
        pos += parseInt( (yourPoints-prevMilestone)/(MileStone[j]-prevMilestone) * milestonepos);
        break;
    }
}

答案 1 :(得分:1)

使用以下循环计算所需的偏移量(从数组末尾开始循环):

var pos2 = 0;

for(var j=ArrayLength-1; j >= 0; j--){
     var currentMilestone = parseInt( MileStone[j]);

    //reaching "full" milestone
    if(yourPoints >= currentMilestone)
    {   
        pos2 = milestonepos * (j+1);

        var diff = yourPoints - currentMilestone;
        console.log("diff: " + diff);

        //counting offset to move forward from "full" milestone
        if(j < ArrayLength - 1) {
            var nextMilestone = MileStone[j+1];
            var diffInPoints =  nextMilestone - currentMilestone;
            var subDiff = (milestonepos * diff) / diffInPoints;
            pos2 += subDiff;
        }
        break;
    }
}

console.log("pos2: " + pos2);
$('.seek').animate({ left: '' + parseInt(pos2) + 'px' }, 1000);

这是 Demo

答案 2 :(得分:0)

只需将margin-left: -8px;更改为margin-left: 8px;即可.barCont .seek 这是:http://jsfiddle.net/Udwq9/3/