views:

1329

answers:

2

I am using jQuery's scrollTo plugin to scroll up and down my page, using UP arrow and DOWN arrow.

i have a bunch of div with class "screen", as so: <div class="screen-wrapper">...</div>

What I am trying to do is, when i press UP or DOWN, the window scrolls to the next, or previous div with class of "screen".

I have the keypresses taken care of. According to the plugin docs, to scroll a window, you use $.scrollTo(...);

Here's the code I have:

$(document).keypress(function(e){
    switch (e.keyCode) {
        case 40:    // down
            n = $('.screen-wrapper').next()
            $.scrollTo( n, 800 );
          break;
        case 38:    // up

          break;
        case 37:    // left

          break;
        case 39:    // right

          break;

    }      
});

And if it helps, here's the HTML div. I have a few of these on the page, and essentially, am trying to scroll to next one by pressing down arrow:

<div class='screen-wrapper'>
<div class='screen'>
    <div class="sections">
        <ul>
            <li><img src="images/portfolio/sushii-1.png " /></li>
            <li><img src="images/portfolio/sushii-2.png" /></li>
            <li><img src="images/portfolio/sushii-3.png" /></li>
        </ul>
    </div>

    <div class="next"></div>
    <div class="prev"></div>
</div> 

And also if it needed, I can provide a link where this is being used if it'll help someone get a better idea.

edit And, i forgot to mention what the real question here is. The question/problem is that it won't scroll down past the first element, as seth mentioned.

+2  A: 

There's no real question here but I'm going to guess the problem is that your page doesn't scroll past the first one. It's because you call this on every keypress:

            n = $('.screen-wrapper').next()

It's probably returning the same one every single time you call it. Try moving the instantiation outside of the keypress.

          var s = $('.screen');
          var curr=-1, node;

          $(document).keypress(function(e){
             switch( e.keyCode ) {
               case 40:
                  node = s[++curr];
                  if (node) {
                    $.scrollTo( node,800);
                  }
                  else {
                    curr = s.length-1;
                  }
                  break;
                case 38:
                   node = s[--curr];
                   if (node) {
                     $.scrollTo( node ,800);
                   }
                   else {
                      curr = 0;
                    }
                   break;
                }
          });
seth
Alright. thanks. Will try now.
lyrae
back. it does work perfectly when i press down arrow. but it's a little buggy when press UP. Your help was good enough, ill try to take it from here. but if by any means you or someone happens to still wanna help, the output can be seen here: 9leaves.com/showcase.php
lyrae
@lyrae if he solved your problem, you should accept his answer, or at least give an upvote...
TM
@TM It didn't solve though. I mean, it partially did. It scrolls down, but i gotta press UP twice to scroll up. I do appreciate his help though. And yes, i usually tend to +1 every answer in my threads just for them taking the time to help. (but i do this at the end)
lyrae
whoops...brain fart. updated the answer to fix bug. Where's my QA dept dammit!?
seth
Perfect :)thanks seth
lyrae
I guess you were my QA. :) Sorry about that lyrae.
seth
didn't need to edit the 3rd time. what you just had worked.
lyrae
You should put the old one back for simplicity's sake. Or, have them both up there in case the simple one breaks later, ill have the more complex one to fall back on
lyrae
It's all in the revision history. Click on the link next to edited:
seth
learn something new everyday don't we? thanks
lyrae
+1  A: 

I just implemented something similar and I used id attributes for each element.

Why not do something like

window.location.href = '#section-' + curr;

instead of using scrollTo?

wrumsby
Can't really give ID attributes to each DIV. I won't always know how many there will be, and the javascript that allows the scrolling of images will stop working, unless i have a block of code for each ID. =(
lyrae
You could use a selector like '.sections li' to loop over all the list items and assign an id using JavaScript. Alternatively you could generate the ids on the server and let JavaScript know how many there are. Have a look at the script on my website (via my profile) for an example.
wrumsby
That will cause an entry to be written into the browser history, which may not be desirable.
Simon Lieschke