0

I'm trying to slide a div to the right and have it loop. Below is my code a link to the jsfiddle:

<head>
  <meta charset="utf-8">
  <title>animate demo</title>
  <style>
  div {
    background-color: #bca;
    width: 100px;
    border: 1px solid green;
  }
  </style>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>


<div id="block">Hello!</div>

<script>
function scroll() {
    $( "#block" ).animate({marginLeft: "0.6in"}, 
    {
        duration: 1500,
        complete: function() {
            scroll
        }
    }
)};
scroll();
</script>

http://jsfiddle.net/ZWSPJ/

3
  • You need to reset your margin-left to 0 at the start of your function if you want a repeating animation. Commented Dec 2, 2013 at 16:54
  • Are you trying to make a marquee type scroll (where it runs off the right edge and runs back on at the left edge), should it just jump back to 0 and start again or should it forever move to the right? Commented Dec 2, 2013 at 16:54
  • The answer by adeneo should be the accepted answer. Commented Dec 2, 2013 at 17:14

4 Answers 4

4

Update:

I appreciate the accept, but adeneo's answer was so much more elegant. As he removed it, I am going to include his approach for the rotation here: Live demo (click).

var $block = $('#block'); //I like to cache my references in advance

var flag = 0;
(function scroll() { //auto call this function with an IIFE
  $block.animate({marginLeft: (flag) ? 0 : '0.6in'}, 1500, scroll);
  flag ^= 1; //reverse flag
}());

Other approaches:

Live demo here (click).

var $block = $('#block'); //you can go ahead and cache this reference
function scroll() {
  $block.css('marginLeft', 0); //reset margin
  $block.animate({marginLeft: "0.6in"}, //animate margin
  {
    duration: 1500,
    complete: function() {
      scroll(); //repeat
    }
  }
)};
scroll(); //call function to begin loop

In case you're looking to reverse the animation on each run, you could do this:

Live demo here (click).

var $block = $('#block'); //you can go ahead and cache this reference

var animSwitch = 0;
function scroll() {
  var margin = 0;
  if (!animSwitch) {
    margin = '0.6in';
    animSwitch = 1;
  }
  else {
    animSwitch = 0;
  }
  $block.animate({marginLeft: margin}, //animate margin
    {
      duration: 1500,
      complete: function() {
        scroll(); //repeat
      }
    }
  )
};
scroll(); //call function to begin loop
Sign up to request clarification or add additional context in comments.

Comments

2

So one correct answer is:

function scroll() {
    $( "#block" ).animate({marginLeft: "+=0.6in"}, 
    {
        duration: 1500,
        complete: scroll
    }
)};
scroll();

Comments

1

do you want something like this ?

fiddle Demo

function scroll() {
    $("#block").animate({
        marginLeft: "+=0.6in"
    }, {
        duration: 1500,
        complete:scroll
    })
};
scroll();

Comments

0

part of your problem is

complete: function() {
    scroll
}

this wont call the scroll function, just the wrapping function. You would need to do:

complete: function() {
    scroll();
}

or neater

complete : scroll

1 Comment

yes your right m59 - just to clarify, this only why the script wasnt looping at all, it doesnt address the whole issue. see other other answers for a more complete response to the question ( that i didnt read properly, long day :) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.