SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    525
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Determine scroll position while using overflow

    i have a list that has a set height of 100 pixels. the overflow property is set to 'auto', that way a scroll bar appears if the list gets too crowded.

    Code HTML4Strict:
    <ul style="height: 100px; overflow: auto;">
         <li>Testing</li>
         <li>Testing</li>
         <li>Testing</li>
         <li>Testing</li>
         <li>Testing</li>
         <li>Testing</li>
         <li>Testing</li>
    </ul>

    i'm wanting a way to reset the scroll position to the top of the list. anyone know how i can do this? thanks!

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The scrollTop property is what you're after
    http://www.quirksmode.org/dom/w3c_cssom.html
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Code css:
    .scrollbox {
        height: 100px;
    	overflow: auto;
    }

    Code html4strict:
    <ul id="myScrollbox" class="scrollbox">
         <li>Testing</li>
         <li>Testing</li>
         <li>Testing</li>
         <li>Testing</li>
         <li>Testing</li>
         <li>Testing</li>
         <li>Testing</li>
    </ul>
    <p><input id="scrollTop" type="button" value="Scroll to top"></p>

    Code javascript:
    function scrollToTop(el) {
    	el.scrollTop = 0;
    }
    document.getElementById('scrollTop').onclick = function () {
    	var el = document.getElementById('myScrollbox');
        scrollToTop(el);
    };

    Or in full:
    Code html4strict:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>Scroll test</title>
    <style type="text/css">
    .scrollbox {
        height: 100px;
    	overflow: auto;
    }
    </style>
    </head>
    <body>
    <ul id="myScrollbox" class="scrollbox">
         <li>Testing</li>
         <li>Testing</li>
         <li>Testing</li>
         <li>Testing</li>
         <li>Testing</li>
         <li>Testing</li>
         <li>Testing</li>
    </ul>
    <p><input id="scrollTop" type="button" value="Scroll to top"></p>
    <script type="text/javascript">
    function scrollToTop(el) {
    	el.scrollTop = 0;
    }
    document.getElementById('scrollTop').onclick = function () {
    	var el = document.getElementById('myScrollbox');
        scrollToTop(el);
    };
    </script>
    </body>
    </html>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •