2

I am trying to create a function that animates the header from right to left, and then left to right.

You can see the start of it here: http://opportunityfinance.net/Test/financeforum2013/

When the page loads it begins to slide the header to the right, revealing an image on the left, and then back, revealing an image on the right. I need to keep doing this over and over and over again. And each time the images on the right and left need to change. I have 5 images that I would like to cycle through for the side images. The Small Business Finance Forum image should remain constant inbetween the two changing side images, and just move right to left and then left to right.

Here is the code I am using thus far. The jQuery delay of 10 seconds doesn't seem to work however, and I can't figure out how to call the function again when the animation is complete.

jQuery:

$(document).ready(function(){
    $(".header_right").html('<img src="images/1.jpg" alt="OFN" />');
    $(".header_left").html('<img src="images/2.jpg" alt="OFN" style="max-height: ' + $("div.header").height() + 'px' + ';" />');
    slide();
});

function slide(slideVar)
{
    slideVar = !slideVar ? 2 : slideVar;
    slideVar = slideVar > 5 ? 1 : slideVar;

    $(function () {
        var mrgLeft = parseInt($(".header_right").css('marginLeft'),10) == 0 ? $(".header_right").outerWidth() : 0;
        $(".header_left").html('<img src="images/' + slideVar + '.jpg" alt="OFN" style="max-height: ' + $("div.header").height() + 'px' + ';" />');

        $("img.hImage").delay("10000").animate({ marginLeft: mrgLeft + 'px' }, { duration: 3000, queue: false });
        $(".header_left").delay("10000").animate({ marginLeft: mrgLeft + 'px'}, { duration: 3000, queue: false });
        $(".header_right").delay("10000").animate({ 'right': '-' + mrgLeft + 'px'}, { duration: 3000, queue: false });
    });
    slideVar++;
}

HTML setup:

<div class="header">
    <img class="hImage" src="images/header.jpg" alt="Small Business Finance Forum" />
    <div class="header_right"></div>
    <div class="header_left"></div>
</div>

CSS:

.hImage
{
    width: 64%;
}

div.header_right
{
    position: absolute;
    display: block;
    right: -10px;
    top: 0px;
    width: 35.5%;
}

div.header_left
{
    position: absolute;
    display: block;
    left: -35.5%;
    top: 0px;
    width: 35.5%;
}

div.header
{
    box-sizing: border-box;
    background-color: #00457E !important;
    width: 100%;
}

Basically, I just want to slide the main "header.jpg" image left and right, each time changing the left and right image up to and including 5.jpg. It should loop from 1.jpg to 5.jpg. But mainly, I can't figure out how to fix the delay issue (since it doesn't work) and to make it loop within the slide() function when the animation is complete.

Can someone please help me get started with this?

Thanks guys :)

lukeocom
  • 3,243
  • 3
  • 18
  • 30
Solomon Closson
  • 6,111
  • 14
  • 73
  • 115
  • I suggest, before someone tries to make some brutal animation loops that you **fix first your elements position on resize**. (Yes, resize your window and you'll see why you should stop at this stage... and review your CSS and animations properties.) – Roko C. Buljan May 03 '13 at 15:54
  • Oh, yeah, I see what you mean, but that's not hard to fix on resize. So it's just a matter of setting the `marginLeft` and/or `right` values. I'm not concerned about that for now. – Solomon Closson May 03 '13 at 15:56
  • What's the argument `slideVar` for? – Foreign Object May 03 '13 at 16:16
  • That's for the Image Number... 1.jpg, 2.jpg, 3.jpg, 4.jpg, and 5.jpg. It needs to rotate through those images, placing that image in, either, the header_left or header_right div before the slide starts. – Solomon Closson May 03 '13 at 16:33
  • Though I think it needs another argument to determine whether to slide left or right. – Solomon Closson May 03 '13 at 16:38
  • Basically, if I can figure out how to get the `delay("10000")` and an infinite loop within the slide() function working, after the animation has completed, than I would consider this question answered! Cause I'm sure I can figure the rest out myself. – Solomon Closson May 03 '13 at 17:38
  • I know this question is jQuery specific, but I want to share this with you http://jsfiddle.net/coma/bXDHE/5/ – coma May 06 '13 at 10:00
  • @solomon if i might give some designtips. make the social buttons smaler, and the same row as the very last line, they take away the power of the network logo. this nice lady could look into the content instead of looking out. your "register" looks like text, the animated text under like the button for, little confusing. but i like that kind of simple pagestyle you made. – Ol Sen May 07 '13 at 05:08
  • delay(3000) is not a string-value, should work then. http://api.jquery.com/delay/ – Ol Sen May 07 '13 at 05:17

1 Answers1

0

I've created a little image slider that you might like to use as a starting point. It uses a container of fixed width with an id #container. Within it are containers for the three different images. You can change the widths of all of these in the css to suit your needs.

The animation uses the complete function to call a swapImg function. This swaps the left or right image depending on which direction the image container has just moved.

The images swapped are taken from the image array. So alternate images are displayed on alternate sides. So every second image is displayed on the right... This means you will need to use an even number of images in the array, or change the script.

here's the fiddle

http://jsfiddle.net/Rgh2B/5/

lukeocom
  • 3,243
  • 3
  • 18
  • 30
  • Ok, yeah, this is a good starting point. However, I'm dealing with 2 Images to start with, not 1. The sliding will need to affect both images, but it's a little more complicated than just a slide to another image. It needs to slide left, right, and than left, right again... infinitely looping the slide, but keep the 1 image shown at all times, and when the right image slides out, the left image slides in at the same time, and when the left image slides out, the right image slides in at the same time, during this infinite loop. – Solomon Closson May 06 '13 at 14:09
  • oic,. That's a little more complicated! – lukeocom May 07 '13 at 00:40
  • Yeah, that's definitely coming along nicely. Actually, there will need to be 3 images in there. The middle image should go from side to side, while the left and right images should slide in and slide out of view at the same time the middle image is going from side to side (so the slide will be uniform for all images, that is to say that they will all be sliding in the same direction at all times), if you follow me. But, here's the catch, each time the middle image is going from side to side, it will need to change the image on that side to a different image for when it slides in again. – Solomon Closson May 07 '13 at 02:40
  • So, basically, when the left and right images slide out of view, the image will need to change, after it slides out, to a different image that will slide back in again, infinitely looping. I have a total of 5 images that I would like to rotate infinitely on the left and right sides. They are all the same height and same width also. The middle image is wider than the side images though, but not sure if that is important for this, if we can just perform the slide based on the left and right image widths (which will be the same at all times). – Solomon Closson May 07 '13 at 02:46
  • The middle image should never change, only the side images, and only after the side images slide out. The middle image will always be shown moving from side to side (left to right to left to right, infinitely). – Solomon Closson May 07 '13 at 02:50
  • yes the sliding is based on the side image widths. Use the fiddle as a starting point. play around with the css to get the desired widths. Ill leave the rest up to you, you should be able to update it to what you need... :) I think the sliding is how you want it? http://jsfiddle.net/Rgh2B/4/ – lukeocom May 07 '13 at 03:19
  • updated the fiddle, removed the redundant code. made the center image wider. http://jsfiddle.net/Rgh2B/5/ – lukeocom May 07 '13 at 04:54
  • 1
    Wow, that's exactly what I need. Thanks, you deserve this! – Solomon Closson May 07 '13 at 23:22