gpt4 book ai didi

javascript - 使用 vanilla js 为条形音箱制作动画

转载 作者:行者123 更新时间:2023-12-04 07:52:28 25 4
gpt4 key购买 nike

我想要条形音箱效果,即

  • 我希望相应的 divs(bars) 像下面的代码片段一样无限地增加/减少高度。我想在 Vanilla JS 中实现相同的效果。
  • 我希望它们随机改变高度,即 5 个条将以 5 种随机方式改变高度。

  • Element.animate() 方法没有完成这项工作,我也不允许使用任何外部库或 API。我不确定使用 CSS 动画方法。
    下面编写的代码是用 jQuery 编写的。我无法在 Vanilla JS 中破译相同的解决方案。

    function fluctuate(bar) {
    var amplitude = Math.random() * 42;
    console.log(amplitude);
    var height = amplitude * 4;
    //Animate the equalizer bar repeatedly

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

    $(".bar").each(function(i) {
    fluctuate($(this));
    console.log($(this));
    });
    .inline-block-wrapper {
    display: inline-block;
    margin-right: -4px;
    }

    .bar-wrapper {
    background-color: black;
    height: 160px;
    width: 135px;
    display: block;
    position: relative;
    }

    .bar {
    /* background-color: green; */
    width: 100%;
    height: 160px;
    position: absolute;
    left: 0px;
    bottom: 0px;
    }

    .bar-container {
    margin: auto;
    width: 50%;
    height: 10rem;
    border: 15px solid black;
    }

    timers {
    display: flex;
    }

    .start {
    display: inline;
    background-color: green;
    margin-right: 50px;
    padding: 10px 10px;
    color: white;
    border: none;
    width: 100px;
    height: 35px;
    justify-content: center;
    }

    .stop {
    display: inline;
    background-color: red;
    margin-left: 50px;
    padding: 10px 10px;
    color: white;
    border: none;
    width: 100px;
    height: 35px;
    justify-content: center;
    }
    <!DOCTYPE html>
    <html>

    <head>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>

    </head>

    <body>
    <div class="bar-container">
    <div class="inline-block-wrapper">
    <div class="bar-wrapper">
    <div class="bar" style="background-color:#754ab7"></div>
    </div>
    </div>
    <div class="inline-block-wrapper">
    <div class="bar-wrapper">
    <div class="bar" style="background-color:#c640a5"></div>
    </div>
    </div>
    <div class="inline-block-wrapper">
    <div class="bar-wrapper">
    <div class="bar" style="background-color:#f05386"></div>
    </div>
    </div>
    <div class="inline-block-wrapper">
    <div class="bar-wrapper">
    <div class="bar" style="background-color:#f58169"></div>
    </div>
    </div>
    <div class="inline-block-wrapper">
    <div class="bar-wrapper">
    <div class="bar" style="background-color:#f9c059"></div>
    </div>


    </div>
    </div>

    <div class="timers">
    <button class="start" onClick=f luctuate()> Start</button>
    <button class="stop" onClick=s topSequence()> Stop</button>

    最佳答案

    使用了大量的 CSS,但我找到了一个接近我想要的东西的解决方案。

      var clicks = 0

    function fluctuate(){
    clicks+=1
    if(clicks>1){

    stopFluctuation()
    }else{

    document.querySelector('.div-stop').classList.toggle('animate-bars')
    var bar = document.getElementsByClassName('div-bar')

    }



    }

    function stopFluctuation() {
    let count = 1;

    let bars = document.getElementsByClassName('div-bar');
    for(bar of bars) {
    bar.classList.toggle('paused-bars');
    }

    }
        .container {
    border: 20px solid black;
    background-color: black;
    }
    .button-container {
    display: flex;
    }
    .start {
    display: inline;
    background-color: green;
    margin-right: 50px;
    padding: 10px 10px;
    color: white;
    border: none;
    width: 100px;
    height: 35px;
    justify-content: center;
    }
    .stop {
    display: inline;
    background-color: red;
    margin-left: 50px;
    padding: 10px 10px;
    color: white;
    border: none;
    width: 100px;
    height: 35px;
    justify-content: center;
    }
    .div-stop {
    display: flex;
    align-items: center;
    width: 550px;
    height: 150px;
    overflow: hidden;
    }
    .div-stop div {
    flex: 10 auto;
    height: 100%;
    margin: 0;
    }
    .animate-bars div {
    animation: animate-bar 500ms linear infinite alternate;
    transform-origin: bottom;


    }
    .animate-bars div:first-child {
    margin-left: 0;
    }
    .animate-bars div:last-child {
    margin-right: 0;
    }
    .animate-bars div:nth-child(1) {

    animation-duration: 2200ms;
    animation-delay: 9ms;
    position: relative;
    top:22%;

    }
    .animate-bars div:nth-child(2) {
    animation-duration: 1500ms;
    animation-delay: 0ms;
    position: relative;
    top: 33%;

    }
    .animate-bars div:nth-child(3) {
    animation-duration: 1789ms;
    animation-delay: 5ms;
    position: relative;

    }
    .animate-bars div:nth-child(4) {
    animation-duration: 2786ms;
    animation-delay: 7ms;
    position: relative;
    top: 10%;
    }
    .animate-bars div:nth-child(5) {
    animation-duration: 1659ms;
    animation-delay: 8ms;
    position: relative;
    top: 27%;
    }

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

    }
    }
    body {
    height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    }
    body::after {
    content: "";
    position: absolute;
    top: 50%;
    right: 0;
    left: 0;
    width: 100%;
    height: 1px;
    }
    .paused-bars {
    -webkit-animation-play-state: paused;
    animation-play-state: paused !important;
    }
     <div class = "container">
    <div class="div-stop">
    <div style="background-color:#754ab7" class="div-bar"></div>
    <div style="background-color:#c640a5" class="div-bar"></div>
    <div style="background-color:#f05386" class="div-bar"></div>
    <div style="background-color:#f58169" class="div-bar"></div>
    <div style="background-color:#f9c059" class="div-bar"></div>


    </div>
    </div>
    <div class = "button-container">
    <button id="start" class= "start" onClick="fluctuate()">Start</button>
    <button id="stop" class = "stop" onClick="stopFluctuation()">Stop</button>
    </div>

    关于javascript - 使用 vanilla js 为条形音箱制作动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66884291/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com