views:

45

answers:

3

I am trying to create a scrollable DIV using the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html>
    <head>
        <title>Test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <script type="text/javascript" src="lib/jquery/jquery-1.3.2.js"></script>

    <style type="text/css">
        div.container {
          overflow:hidden;
          width:200px;
          height:200px;
        }
        div.content {
          position:relative;
          top:0;
        }
    </style>

    <script type="text/javascript">
        $(function(){
          $(".container a.up").bind("click", function(){
            var topVal = $(this).parents(".container").find(".content").css("top");
            $(this).parents(".container").find(".content").css("top", topVal-10);
          });

          $(".container a.dn").bind("click", function(){
            var topVal = $(this).parents(".container").find(".content").css("top");
            $(this).parents(".container").find(".content").css("top", topVal+10);
          });
        });
    </script>

</head>

<body>
<div class="container">
  <p>
    <a href="#" class="up">Up</a> / 
    <a href="#" class="dn">Down</a>
  </p>
  <div class="content">
    <p>Hello World 1</p>
    <p>Hello World 2</p>
    <p>Hello World 3</p>
    <p>Hello World 4</p>
    <p>Hello World 5</p>
    <p>Hello World 6</p>
    <p>Hello World 7</p>
    <p>Hello World 8</p>
    <p>Hello World 9</p>
    <p>Hello World 10</p>
    <p>Hello World 10</p>
    <p>Hello World 11</p>
    <p>Hello World 12</p>
    <p>Hello World 13</p>
    <p>Hello World 14</p>
    <p>Hello World 15</p>
    <p>Hello World 16</p>
    <p>Hello World 17</p>
    <p>Hello World 18</p>
    <p>Hello World 19</p>
    <p>Hello World 20</p>
    <p>Hello World 21</p>
    <p>Hello World 22</p>
    <p>Hello World 23</p>
    <p>Hello World 24</p>
    <p>Hello World 25</p>
    <p>Hello World 26</p>
    <p>Hello World 27</p>
  </div>
</div>
</body>
</html>

I don't know where I am messing up, but it simply refuses to work. Any suggestions?

EDIT: I have tried both overflow: auto and overflow: hidden

A: 

why have you used overflow:hidden try using overflow:auto

div.container { overflow:auto; width:200px; height:200px; }

sushil bharwani
@sushil: Thanks. I tried both and it didn't seem to work.
Legend
+2  A: 

You have to use overflow:auto otherwise the scroll will not show up.

EDIT: I just threw your code into a page and looked at it. This changes my answer a bit. From what I see, do you want to have it scrollable by the Up/Down links?

You should use $(this).preventDefault() as well to stop the # from showing up in the URL when you click Up or Down.

This doesn't necessarily solve your curiosity but someone has already developed a plugin that might be useful if you wish to use it.

http://flowplayer.org/tools/scrollable/index.html

Eric Reynolds
@Eric: Yup... I don't want the scrollbars but rather want to control through the links... If I can get that animate effect to work, that would be awesome.
Legend
I had vaguely remembered a plugin when I was trying to find something last week (link is in my answer). It is obviously possible to do what you want to do, I however cannot think of it. But if you don't HAVE to reinvent the wheel, I would use the plugin
Eric Reynolds
+2  A: 

topVal is a string, like 100px. You would need to parseInt(topVal, 10) to read it before adding a number to it (and that's assuming it was already set to a pixel value).

Better to use scrollTop to change the vertical scrolling position instead of trying to mess with the CSS. Also remember to return false from your click handlers to stop the # link being followed. Or, better, don't mark the buttons up as links, 'cos they're not links, they don't go anywhere. Use a button or eg. span, styled as appropriate.

Better still to just use a perfectly normal overflow: auto div and let the browser provide the scrollbar. This will typically be much more usable than having custom scroll-up/scroll-down buttons, which personally I always find infuriatingly unpleasant to use.

bobince
Agreed - homemade scrolling mechanisms are a terrible idea.
Pointy
@bobince: Thanks it worked great...
Legend