views:

74

answers:

6

With my code I can get a div to scroll through 4 images, but if you look at the code, i have to keep adding '.next' to get it to scroll to another image. I don't want to determine the amount of images to scroll through in jquery, i want jquery to keep looking through the div for as many images as i have, scroll through all of them, then scroll back to the top. just take a look at my code. I don't want to use another plugin because i've almost accomplished what I want to do with just a few lines of code compared to multi KB plugins.

ps: I don't know why the '-165px' is necessary, but it makes it all align perfectly when scrolling from picture to picture.

Jquery:

Jquery:

$(document).ready(function(){
    $('.rotate_container').everyTime(10, function (){
        $('.rotate_container').animate({scrollTop: $('img').offset().top - 165}, 2000).delay(1000)
        .animate({scrollTop: $("img").next().offset().top - 165}, 2000).delay(1000)                   
        .animate({scrollTop: $("img").next().next().offset().top - 165}, 2000).delay(1000)
        .animate({scrollTop: $("img").next().next().next().offset().top - 165}, 2000).delay(1000)
    });
});

Any ideas?

A: 

code would help, but i would suggest using jquery to select the images... then iterate through them.

Aaron Saunders
Should be a comment.
cji
Updated. And I agree, how? My code already works, but I don't want to have to determine how many images to scroll through in the Jquery, I want it to automatically keep selecting the next one. My code only selects how many you determine it to beforehand. Notice each line that I repeat with an extra ".next()" makes it scroll 1 more image.
android.nick
my apologies.. that what I meant to do
Aaron Saunders
A: 

http://flesler.blogspot.com/2007/10/jqueryscrollto.html this plugin has promise.

karlw
+2  A: 

I didn't test it, but this seems like it will work

$(document).ready(function(){
    var container = $('.rotate_container');
    var images = container.find('img');
    container.everyTime(10, function (){
        images.each(function() {
            container.animate({scrollTop: $(this).offset().top - 165}, 2000).delay(1000);
        });
    });
});
Stephen
that's the answer i was looking for, you're awesome. Hey if I may ask, why the two variables at the beginning? I thought they were pretty much just to reuse code, but you only use "container" and "images" once each. Is there another reason? does it speed up the code or something? thanks man, I really appreciate your help.
android.nick
Oh hey, also, would you know how to pause it when the mouse moves over the image when it's not in motion? so if it's sliding to the next image and the person hovers over it, then when it arrives at the image it's sliding to, it pauses until the person removes the mouse, then it continues to slide to the next image.thanks so much.
android.nick
Yeah, it's always good practice to cache your jQuery objects for re-use. I've had to go back and re-factor code so many times that now I start by caching. Also, you were using `$('.rotate_container')` twice, so it was worth the effort to cache. I'll think about your pausing question and get back to you.
Stephen
A: 

My solution requires you to put one additional div inside your container and one css rule. It goes like this:

<script type="text/javascript">
$(document).ready(function(){
    var top = 0;
    var up = true;
    var wrap = $(".rotate_container > div ") // shortcut :)

    function slide() {
        wrap.animate( { "top" : top+"px" }, 400 );

        if( Math.abs( top ) > wrap.height() ){ // if all inner_wrap is outside container
            up = false; 
        }
        else if( top >= 0 ) {
            up = true
        }
        if( up ) top -= 165;
        else top += 165;

        setTimeout( slide, 500 ) // call function slide after 500 ms
    }
    setTimeout( slide, 500 ) // call function slide after 500 ms
});
</script>

<style type="text/css">
.rotate_container {
    max-height:292px;
    max-width:760px;
    height:292px;
    width:760px;
    overflow:hidden;
    padding:0;
    margin:0;}

.rotate_container > div {
    position: relative;
    top: 0; left: 0;
}

.rotate_container img {
    height:292px;
    width:760px;}
</style>


<div class="rotate_container grid_12">
    <div id="inner_wrap">
        <img src="{{ MEDIA_URL }}/employlogo.png" class="top" />
        <img src="{{ MEDIA_URL }}/employlogo.png" class="middle" />
        <img src="{{ MEDIA_URL }}/employlogo.png" class="midbottom" />
        <img src="{{ MEDIA_URL }}/employlogo.png" class="bottom" />
        <img src="{{ MEDIA_URL }}/employlogo.png" class="verybottom" />
    </div>
</div>

Well, it's not pretty, but it works. I misunderstood your snippet at first, sorry.

cji
hey it works, and it's small, nice.one more question if i may, how would i change this line so that instead of starting to scroll back up to the top incrementally, it shoots you all the way back to the top in one smooth animation?.................. "else top += 165;"
android.nick
write `else top = 0;` instead of `else top += 165;`. Or `else { top = 0; wrap.animate( { "top" : top+"px" }, 2000 ); setTimeout( slide, 2000 ); return; }` if you want this animation to take more time.
cji
A: 

Might check out jCarousel or it's little brother, jCarouselLite, which do image scrolling with ease effects.

For a solution to the above, you might assign the first img to variable and then increment it on the callback. In the following code the alert output is 'Second':

<div>First</div>
<div>Second</div>
<script type="text/javascript">
$(document).ready(function(){
    var test = $('div');
        test = test.next();
        alert(test.html());
});
</script>

So, to modify your code:

$(document).ready(function(){
    var img = $('.rotate_container img');
    $('.rotate_container').everyTime(10, function (){
        $('.rotate_container').animate({scrollTop: img.offset().top - 165}, 2000,null,function() { img = img.next(); }).delay(1000)
        .animate({scrollTop: img.offset().top - 165}, 2000,null,function() { img = img.next(); }).delay(1000)
        .animate({scrollTop: img.offset().top - 165}, 2000,null,function() { img = img.next(); }).delay(1000)
          .animate({scrollTop: img.offset().top - 165}, 2000,null,function() { img = img.next(); }).delay(1000)
    });
});

etc.

Can probably modify that using a function to make it even cleaner as well.

clumsyfingers
A: 

Recursion. Write a function to do what you want (I would help with specific code but I don't know what you are going for). Have the function take in the first image. Do what you want in the function, and before you close the function call the function you are in and send it the .next() variable.

Capt Otis
All i want is an image scroller that shows 1 image for a few seconds, then slides up and the next image shows, here I found exactly what I'm intending to build. http://papermashup.com/demos/jquery-slideshow/ -but with the code that I have above, it works just fine, the only problem i have is that I have to tell it in the jquery code exactly how many images to scroll down through, notice the last 3 lines are the same, but with each one adding ".next()" I don't know how to make it so that the script keeps looking down for images. Right now it only goes through how many i determine before hand.
android.nick