Question

i have the following problem:

i want the animation to start on click instaed of on ready ... it works when it is set on .ready but no when on .click

$('div.main').ready(function() {
  setTimeout("$('div#tl').css({'top' : '-175px', 'left' : '-175px'})", 100);
  setTimeout("$('div#t').css({'top' : '-175px'})", 300);
  setTimeout("$('div#tr').css({'top' : '-175px', 'left' : '325px'})", 500);
  setTimeout("$('div#r').css({'left' : '325px'})", 700);
  setTimeout("$('div#br').css({'top' : '325px', 'left' : '325px'})", 900);
  setTimeout("$('div#b').css({'top' : '325px'})", 1100);
  setTimeout("$('div#bl').css({'top' : '325px', 'left' : '-175px'})", 1300);
  setTimeout("$('div#l').css({'left' : '-175px'})", 1500);
});

here is the HTML:

<div class="main">
    <div class="sub" id="tl"></div>
    <div class="sub" id="t"></div>
    <div class="sub" id="tr"></div>
    <div class="sub" id="r"></div>
    <div class="sub" id="br"></div>
    <div class="sub" id="b"></div>
    <div class="sub" id="bl"></div>
    <div class="sub" id="l"></div>
</div>

and here the CSS:

div.main {
  position: relative;
  z-index: 2;
  width: 300px;
  height: 300px;
  margin: 0px auto;
  top: 50%;
  margin-top: -150px;
  background-color: #eee;
}

div.sub {
  position: absolute;
  z-index: 1;
  top: 75px;
  left: 75px;
  width: 150px;
  height: 150px;
  background-color: rgba(0,0,0,0.5);
  -webkit-transition: all 0.3s ease-in-out;
}
Was it helpful?

Solution

You should include them within the document ready function.

$(document).ready(function() {
 $('div.main').click(function() {
  setTimeout("$('div#tl').css({'top' : '-175px', 'left' : '-175px'})", 100);
  setTimeout("$('div#t').css({'top' : '-175px'})", 300);
  setTimeout("$('div#tr').css({'top' : '-175px', 'left' : '325px'})", 500);
  setTimeout("$('div#r').css({'left' : '325px'})", 700);
  setTimeout("$('div#br').css({'top' : '325px', 'left' : '325px'})", 900);
  setTimeout("$('div#b').css({'top' : '325px'})", 1100);
  setTimeout("$('div#bl').css({'top' : '325px', 'left' : '-175px'})", 1300);
  setTimeout("$('div#l').css({'left' : '-175px'})", 1500);
});
    });

Working Demo

OTHER TIPS

You just have to put your code in a dom ready function

$(function() {
 $('div.main').click(function() {
   setTimeout("$('div#tl').css({'top' : '-175px', 'left' : '-175px'})", 100);
   setTimeout("$('div#t').css({'top' : '-175px'})", 300);
   setTimeout("$('div#tr').css({'top' : '-175px', 'left' : '325px'})", 500);
   setTimeout("$('div#r').css({'left' : '325px'})", 700);
   setTimeout("$('div#br').css({'top' : '325px', 'left' : '325px'})", 900);
   setTimeout("$('div#b').css({'top' : '325px'})", 1100);
   setTimeout("$('div#bl').css({'top' : '325px', 'left' : '-175px'})", 1300);
   setTimeout("$('div#l').css({'left' : '-175px'})", 1500);
 });
});

The problem is that your div does not exist when the js is executed. The dom ready function is called when all the elements are ready.

See http://api.jquery.com/ready/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top