Skip to content

JavaScript Array Slice Animation

CODEPEN

JavaScript Array Slice Animation

Github: https://github.com/furkangulsen98/HTML-CSS-JAVASCRIPT/tree/master/JavaScript%20Array%20Slice%20Animation


HTML CODE:

<body onload="message()">

    <div class="container">

        <h2>ARRAY SLICE</h2>

        <div class="boxes">
            <div class="box">0</div>
            <div class="box">1</div>
            <div class="box">2</div>
            <div class="box">3</div>
            <div class="box">4</div>
            <div class="box">5</div>
            <div class="box">6</div>
            <div class="box">7</div>
            <div class="box">8</div>
        </div>

        <div class="boxInput">
            <span class="boxInp1">
                <span class="num1" id="numtxt">start</span>
                <span style=
                      "color:#a88e32; font-size: 30px; padding: 0;margin:0;" 
                      >{</span>
                <input class="numBox" id="inputBegin" value="0" maxlength="1"/>
                <span style=
                      "color:#a88e32; font-size: 30px" 
                      >}</span>
            </span><br />
            <span class="boxInp2">
                <span class="num2" id="numtxt">,end</span>
                <span style=
                      "color:#a88e32; font-size: 30px; padding: 0;margin:0;" 
                      >{</span>
                <input class="numBox" id="inputEnd" value="0" maxlength="1"/>
                <span style=
                      "color:#a88e32; font-size: 30px" 
                      >}</span>
            </span><br />
        </div>

    </div>
</body>

CSS CODE:

body, html {
    padding: 0;
    margin: 0;
    background: #262626;
    font-family: monospace;
    color: #fff;
    height: 100%;
    overflow: hidden;
    display: flex;
}
.container{
    display: block;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
}
.boxes{
    width: 700px;
    height: 80px;
    display: flex;
}
.boxes > .box{
    background: #975353;
    border: 1px solid rgba(0, 0, 0, 0.2);
    line-height: 80px;
    border-radius: 8px;
    flex: 1;
    margin: 0 10px;
    text-align: center;
    font-size: 20px;
    opacity:0;
    -webkit-transform: translateY(-100px);
          transform: translateY(-100px);
}
.boxes .box:nth-of-type(1) {
    -webkit-animation: in 0.5s forwards;
    animation: in 0.5s forwards;
    -webkit-animation-delay: 0.2s;
    animation-delay: 0.2s;
}
.boxes .box:nth-of-type(2) {
    -webkit-animation: in 0.5s forwards;
    animation: in 0.5s forwards;
    -webkit-animation-delay: 0.4s;
    animation-delay: 0.4s;
}
.boxes .box:nth-of-type(3) {
    -webkit-animation: in 0.5s forwards;
    animation: in 0.5s forwards;
    -webkit-animation-delay: 0.6s;
    animation-delay: 0.6s;
}
.boxes .box:nth-of-type(4) {
    -webkit-animation: in 0.5s forwards;
    animation: in 0.5s forwards;
    -webkit-animation-delay: 0.8s;
    animation-delay: 0.8s;
}
.boxes .box:nth-of-type(5) {
    -webkit-animation: in 0.5s forwards;
    animation: in 0.5s forwards;
    -webkit-animation-delay: 1s;
    animation-delay: 1s;
}
.boxes .box:nth-of-type(6) {
    -webkit-animation: in 0.5s forwards;
    animation: in 0.5s forwards;
    -webkit-animation-delay: 1.2s;
    animation-delay: 1.2s;
}
.boxes .box:nth-of-type(7) {
    -webkit-animation: in 0.5s forwards;
    animation: in 0.5s forwards;
    -webkit-animation-delay: 1.4s;
    animation-delay: 1.4s;
}
.boxes .box:nth-of-type(8) {
    -webkit-animation: in 0.5s forwards;
    animation: in 0.5s forwards;
    -webkit-animation-delay: 1.6s;
    animation-delay: 1.6s;
}
.boxes .box:nth-of-type(9) {
    -webkit-animation: in 0.5s forwards;
    animation: in 0.5s forwards;
    -webkit-animation-delay: 1.8s;
    animation-delay: 1.8s;
}
.boxes .box:nth-of-type(10) {
    -webkit-animation: in 0.5s forwards;
    animation: in 0.5s forwards;
    -webkit-animation-delay: 2s;
    animation-delay: 2s;
}
@keyframes in {
  to {
    -webkit-transform: translateY(0px);
            transform: translateY(0px);
    opacity: 1;
  }
}
h2{
    width: auto;
    display: block;
    margin: 0 auto;
    text-align: center;
    font-size: 60px;
    margin-bottom: 5px;
    border: none;
    color: #e3dede;
    border-radius: 20px;
    border-bottom: 2px solid #975353;
}
.boxInput{
    display: flex;
    justify-content: center;
}
.boxInp1{
    position: relative;
    float: left;
}
#numtxt {
    font-size: 42px;
    color: #6da53d;
}
.numBox{
    width: 30px;
    height: 45px;
    margin: 7px;
    background: none;
    border: none;
    font-size: 40px;
    text-align: center;
    line-height: 45px;
    outline: none;
    border-bottom: 1px solid rgba(157, 148, 61, 0.65);
    padding: 0;
    margin: 0;
    margin-top: 50px;
    font-family: monospace;
    font-weight: bold;
    color: #e3dede
}
.num2{
    margin-left: 10px;
}
.boxes > .box.selected {
    background: #33b758;
    transition: .5s;
}

JS CODE:

  function message() {
            alert("truecodes.org")
        }

        for(var event of ['input', 'keyup', 'wheel']) {
            inputBegin.addEventListener(event, keyProcess)
            inputEnd.addEventListener(event, keyProcess)
        }

        var say = 0;
        function keyProcess(e) {
            if(e.deltaY && Date.now() - say > 200){
                say = Date.now();
                let change = Math.sign(e.deltaY);
                this.value = Math.min(9, Math.max(0, Number(this.value) + change));
            }

            if (e.keyCode == 38) {
                var value = Math.min(Number(this.value) + 1, 9);
                this.value = value;
            }
            if (e.keyCode == 40) {
                var value = Math.max(Number(this.value) - 1, 0);
                this.value = value;
            }

            var begin = Number(inputBegin.value)
            var end = Number(inputEnd.value)

            var arr = []
            var arrayElements = document.querySelectorAll('.boxes .box')
            for(var ele of arrayElements) {
                ele.classList.remove('selected')
                arr.push(ele)
            }

            var slicedElements = arr.slice(begin, end)
            for(var ele of slicedElements) {
                ele.classList.add('selected')
            }

        }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: