Agyan
adda
Run
Reset
Copy
jQuery Tutorial › jQuery css() and Dimensions › Size information and scroll progress
Back to tutorial
Code
Edit, then press Run or Ctrl+Enter
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> <div id="bar" style="position:fixed;top:0;left:0;height:5px;width:0;background:#e8590c;z-index:10"></div> <div id="box" style="width:200px;padding:15px;border:5px solid #1f386b;margin:20px">Box</div> <button id="info">Show sizes</button> <button id="grow">Make wider</button> <pre id="out"></pre> <div style="height:900px">Scroll down to see the progress bar at the top.</div> <script> $(function () { $('#info').click(function () { const $b = $('#box'); $('#out').text([ 'width(): ' + $b.width(), 'innerWidth(): ' + $b.innerWidth(), 'outerWidth(): ' + $b.outerWidth(), 'outerWidth(true): ' + $b.outerWidth(true), 'offset top: ' + Math.round($b.offset().top) ].join('\n')); }); $('#grow').click(() => $('#box').css({ width: '+=40', background: '#e7f1ff' })); $(window).on('scroll', function () { const max = $(document).height() - $(window).height(); $('#bar').css('width', ($(this).scrollTop() / max * 100) + '%'); }); }); </script>
Result