Javascript 使用jQuery和CSS创建声音条

Javascript 使用jQuery和CSS创建声音条,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在尝试使用jquery创建动画声条(js fiddle here:) HTML jQuery function fluctuate(bar) { var hgt = Math.random() * 10; hgt += 1; var t = hgt * 30; bar.animate({ height: hgt }, t, function() { fluctuate($(this)); }); } $(".

我正在尝试使用jquery创建动画声条(js fiddle here:)

HTML

jQuery

function fluctuate(bar) {
    var hgt = Math.random() * 10;
    hgt += 1;
    var t = hgt * 30;

    bar.animate({
        height: hgt
    }, t, function() {
        fluctuate($(this));
    });
}

$(".bar").each(function(i) {
    fluctuate($(this));
});
我希望声音条的高度是40px

但是,这种方法将它们收缩到c.15px,因此基线会上下移动,我希望它是固定的


我不明白为什么会发生此错误。

在原始代码中,您返回的高度是[0,1]范围内的整数,然后将该值加1。然后将该值缩放30。您应该返回[0,40]范围内的随机整数,如下所示:

因此,您的代码可以如下所示:

function fluctuate(bar) {
    var height = Math.floor(Math.random() * 40) + 1;
    //Animate the equalizer bar repeatedly
    bar.animate({
        height: height
    }, function() {
        fluctuate($(this));
    });
}

$(".bar").each(function(i) {
    fluctuate($(this));
});
或者,如果需要浮点值,请使用:

var height = (Math.random() * (40.000)).toFixed(4)

编辑:下面是一个功能正常的JSFIDLE,它显示了如何处理基线问题:。此解决方案不要求您单独隔开每个均衡器条。

在原始代码中,您返回的高度是范围内的整数[0,1),然后向值中添加1。然后将该值缩放30。应返回一个介于[0,40]范围内的随机整数,如下所示:

因此,您的代码可以如下所示:

function fluctuate(bar) {
    var height = Math.floor(Math.random() * 40) + 1;
    //Animate the equalizer bar repeatedly
    bar.animate({
        height: height
    }, function() {
        fluctuate($(this));
    });
}

$(".bar").each(function(i) {
    fluctuate($(this));
});
或者,如果需要浮点值,请使用:

var height = (Math.random() * (40.000)).toFixed(4)

编辑:下面是一个功能正常的JSFIDLE,它显示了如何处理基线问题:。此解决方案不要求您单独隔开每个均衡器条。

在原始代码中,您返回的高度是范围内的整数[0,1),然后向值中添加1。然后将该值缩放30。应返回一个介于[0,40]范围内的随机整数,如下所示:

因此,您的代码可以如下所示:

function fluctuate(bar) {
    var height = Math.floor(Math.random() * 40) + 1;
    //Animate the equalizer bar repeatedly
    bar.animate({
        height: height
    }, function() {
        fluctuate($(this));
    });
}

$(".bar").each(function(i) {
    fluctuate($(this));
});
或者,如果需要浮点值,请使用:

var height = (Math.random() * (40.000)).toFixed(4)

编辑:下面是一个功能正常的JSFIDLE,它显示了如何处理基线问题:。此解决方案不要求您单独隔开每个均衡器条。

在原始代码中,您返回的高度是范围内的整数[0,1),然后向值中添加1。然后将该值缩放30。应返回一个介于[0,40]范围内的随机整数,如下所示:

因此,您的代码可以如下所示:

function fluctuate(bar) {
    var height = Math.floor(Math.random() * 40) + 1;
    //Animate the equalizer bar repeatedly
    bar.animate({
        height: height
    }, function() {
        fluctuate($(this));
    });
}

$(".bar").each(function(i) {
    fluctuate($(this));
});
或者,如果需要浮点值,请使用:

var height = (Math.random() * (40.000)).toFixed(4)

编辑:这里有一个功能正常的JSFIDLE,显示了如何处理基线问题:。此解决方案不要求您单独隔开每个均衡器条。

要阻止基线上下移动,您需要使用CSS执行一些不同的操作。可能有几种可能的解决方案,如下所示一个


基本上CSS总是从顶行开始工作,而不是从基线开始。但是使用绝对定位,你可以将声条“下沉”到
.eq
div的底部。然后你必须将声条分开隔开,但我认为这会使你的结果更接近你想要的结果。

要阻止基线上下移动,你需要使用do CSS有一些不同的东西。可能有几种可能的解决方案,这里有一种


基本上CSS总是从顶行开始工作,而不是从基线开始。但是使用绝对定位,你可以将声条“下沉”到
.eq
div的底部。然后你必须将声条分开隔开,但我认为这会使你的结果更接近你想要的结果。

要阻止基线上下移动,你需要使用do CSS有一些不同的东西。可能有几种可能的解决方案,这里有一种


基本上CSS总是从顶行开始工作,而不是从基线开始。但是使用绝对定位,你可以将声条“下沉”到
.eq
div的底部。然后你必须将声条分开隔开,但我认为这会使你的结果更接近你想要的结果。

要阻止基线上下移动,你需要使用do CSS有一些不同的东西。可能有几种可能的解决方案,这里有一种


基本上CSS总是从顶行开始工作,而不是从基线开始。但是使用绝对定位,您可以将声条“下沉”到
.eq
分区的底部。然后您必须将声条分开隔开,但我认为这将使您的结果更接近您想要的结果。

这里是一个没有javascript的示例,只有CSS

请查看此文件和sass css:

$bars-number: 40; // this must reflect markup changes in html too
$bar-color: blue;

.animate-bars {
  display: flex;
  align-items: center;
  width: 300px;
  height: 40px;
  overflow: hidden;
  background-color: lighten($bar-color, 45%);

  div {
    flex: 1 0 auto;
    height: 100%;
    margin: 0 -2px;
    background-color: $bar-color;
    animation: animate-bar 300ms linear infinite alternate;
    transform-origin: bottom;
    opacity: 0;

    &:first-child {
      margin-left: 0;
    }
    &:last-child {
      margin-right: 0;
    }

    @for $i from 1 to $bars-number + 1 {
      &:nth-child(#{$i}) {
        animation-duration: (350 + random(450)) + ms;
        animation-delay: random(500) + ms;
      }
    }
  }
}

@keyframes animate-bar {
  0% {
    transform: scaleY(0);
    opacity: .8;
  }
  100% {
    transform: scaleY(100%);
    opacity: .4;
  }
}

下面是一个没有javascript的示例,只有css

请查看此文件和sass css:

$bars-number: 40; // this must reflect markup changes in html too
$bar-color: blue;

.animate-bars {
  display: flex;
  align-items: center;
  width: 300px;
  height: 40px;
  overflow: hidden;
  background-color: lighten($bar-color, 45%);

  div {
    flex: 1 0 auto;
    height: 100%;
    margin: 0 -2px;
    background-color: $bar-color;
    animation: animate-bar 300ms linear infinite alternate;
    transform-origin: bottom;
    opacity: 0;

    &:first-child {
      margin-left: 0;
    }
    &:last-child {
      margin-right: 0;
    }

    @for $i from 1 to $bars-number + 1 {
      &:nth-child(#{$i}) {
        animation-duration: (350 + random(450)) + ms;
        animation-delay: random(500) + ms;
      }
    }
  }
}

@keyframes animate-bar {
  0% {
    transform: scaleY(0);
    opacity: .8;
  }
  100% {
    transform: scaleY(100%);
    opacity: .4;
  }
}

下面是一个没有javascript的示例,只有css

请查看此文件和sass css:

$bars-number: 40; // this must reflect markup changes in html too
$bar-color: blue;

.animate-bars {
  display: flex;
  align-items: center;
  width: 300px;
  height: 40px;
  overflow: hidden;
  background-color: lighten($bar-color, 45%);

  div {
    flex: 1 0 auto;
    height: 100%;
    margin: 0 -2px;
    background-color: $bar-color;
    animation: animate-bar 300ms linear infinite alternate;
    transform-origin: bottom;
    opacity: 0;

    &:first-child {
      margin-left: 0;
    }
    &:last-child {
      margin-right: 0;
    }

    @for $i from 1 to $bars-number + 1 {
      &:nth-child(#{$i}) {
        animation-duration: (350 + random(450)) + ms;
        animation-delay: random(500) + ms;
      }
    }
  }
}

@keyframes animate-bar {
  0% {
    transform: scaleY(0);
    opacity: .8;
  }
  100% {
    transform: scaleY(100%);
    opacity: .4;
  }
}

下面是一个没有javascript的示例,只有css

请查看此文件和sass css:

$bars-number: 40; // this must reflect markup changes in html too
$bar-color: blue;

.animate-bars {
  display: flex;
  align-items: center;
  width: 300px;
  height: 40px;
  overflow: hidden;
  background-color: lighten($bar-color, 45%);

  div {
    flex: 1 0 auto;
    height: 100%;
    margin: 0 -2px;
    background-color: $bar-color;
    animation: animate-bar 300ms linear infinite alternate;
    transform-origin: bottom;
    opacity: 0;

    &:first-child {
      margin-left: 0;
    }
    &:last-child {
      margin-right: 0;
    }

    @for $i from 1 to $bars-number + 1 {
      &:nth-child(#{$i}) {
        animation-duration: (350 + random(450)) + ms;
        animation-delay: random(500) + ms;
      }
    }
  }
}

@keyframes animate-bar {
  0% {
    transform: scaleY(0);
    opacity: .8;
  }
  100% {
    transform: scaleY(100%);
    opacity: .4;
  }
}

在我看来,CSS3动画在这种情况下会有所帮助我使用jQuery for XB Compatibility EMS to me CSS3动画在这种情况下会有所帮助我使用jQuery for XB Compatibility EMS to me CSS3动画在这种情况下会有所帮助我使用jQuery for XB Compatibility EMS to me CSS3动画在这种情况下会有所帮助jQuery for XB Compatibility有趣的是,问题在于@robert的评论;基线上下移动。是的,但我认为副作用超出了问题的范围。有趣的是,问题在于@robert的评论;基线上下移动。是的,但我认为副作用已经消失了问题范围的另一面。有趣的是,这是小提琴:问题是根据@robert的评论;基线上下移动。是的,但我认为副作用超出了问题的范围。有趣的是,这是小提琴:问题是根据@robert的评论;基线上下移动。是的,但我认为副作用超出了问题的范围。