window.onload = function() { var progress = document.querySelector('.progress'), totalLength = (progress.offsetWidth * 2) + (progress.offsetHeight * 2); var btn = document.querySelector('#enter'), progressVal = document.querySelector('#progress'); btn.addEventListener('click', function() { input = (progressVal.value > 100) ? 100 : progressVal.value; borderLen = (input / 100) * totalLength; console.log(borderLen); if (borderLen <= progress.offsetWidth) { backgroundPos = 'background-position: ' + (-500 + borderLen) + 'px 0px, 495px -300px, 500px 295px, 0px 300px'; progress.setAttribute('style', backgroundPos); } else if (borderLen <= (progress.offsetWidth + progress.offsetHeight)) { backgroundPos = 'background-position: 0px 0px, 495px ' + (-300 + (borderLen - progress.offsetWidth)) + 'px, 500px 295px, 0px 300px'; progress.setAttribute('style', backgroundPos); } else if (borderLen <= (progress.offsetWidth * 2 + progress.offsetHeight)) { backgroundPos = 'background-position: 0px 0px, 495px 0px, ' + (500 - (borderLen - progress.offsetWidth - progress.offsetHeight)) + 'px 295px, 0px 300px'; progress.setAttribute('style', backgroundPos); } else { backgroundPos = 'background-position: 0px 0px, 495px 0px, 0px 295px, 0px ' + (300 - (borderLen - (progress.offsetWidth * 2) - progress.offsetHeight)) + 'px'; progress.setAttribute('style', backgroundPos); } }); };
.progress { height: 300px; width: 500px; margin-top: 20px; background: linear-gradient(to right, black 99.99%, transparent), linear-gradient(to bottom, black 99.99%, transparent), linear-gradient(to right, black 99.99%, transparent), linear-gradient(to bottom, black 99.99%, transparent); background-size: 100% 5px, 5px 100%, 100% 5px, 5px 100%; background-repeat: no-repeat; background-position: -500px 0px, 495px -300px, 500px 295px, 0px 300px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script> <input id='progress' type='text' /> <button id='enter'>Set Progress</button> <div class="progress"></div>