0

i am having problem in stopping other divs from animating. i am making a square blinking divs but everytime i add any other divs that i don't need to animate , they animate as well.

This is my code :

javascript

var fadeintime = 500;
var fadeouttime = 1000;
$(document).ready(function(){
setInterval(anim, 50);
});
function anim(){
var rand = Math.floor((Math.random()*$("body").children("div").length));
var el = $("body").children("div")[rand];
$(el).animate({opacity: "0.7"}, fadeintime);
$(el).animate({opacity: "0.1"}, fadeouttime);
} 

html

<div class="squares1"></div><div class="squares2"></div><div class="squares3"></div><div class="squares5"></div><div class="squares3"></div><div class="squares4"></div>... to as many squares i want

css

body {
background-color:#222;
    line-height:0;
    overflow:hidden;
    padding:0;
    margin:0;
}
.squares {
    background: none repeat scroll 0 0 #B6FFFF;
    transition: background 1200ms ease-out 0s;
        opacity: 0;
    height: 3em;
width: 50px;
    float:left;
    padding:0;
    margin:0;
}
.squares1 {
    background: none repeat scroll 0 0 #CEFF24;
    transition: background 1200ms ease-out 0s;
        opacity: 0;
    height: 3em;
width: 50px;
    float: left;
    padding:0;
    margin:0;
}
.squares2 {
    background: none repeat scroll 0 0 #BF86FF;
    transition: background 1200ms ease-out 0s;
        opacity: 0;
    height: 3em;
width: 50px;
    float:left;
    padding:0;
    margin:0;
}
.squares3 {
    background: none repeat scroll 0 0 #FF8B24;
    transition: background 1200ms ease-out 0s;
        opacity: 0;
    height: 3em;
width: 50px;
    float:left;
    padding:0;
    margin:0;
}
.squares4 {
    background: none repeat scroll 0 0  #EFFFB6;
    transition: background 1200ms ease-out 0s;
        opacity: 0;
    height: 3em;
width: 50px;
    float:left;
    padding:0;
    margin:0;
}
.squares5 {
    background: none repeat scroll 0 0  #FFBAF2;
    transition: background 1200ms ease-out 0s;
        opacity: 0;
    height: 3em;
width: 50px;
    float: left;
    padding:0;
    margin:0;
}

I think there is problem with javascript, can anyone point how to stop other divs from animating ?

rahulkapoor99
  • 117
  • 10

2 Answers2

0

I think problem lies in this line of code

var el = $("body").children("div")[rand];

Since this will consider all div element you will not have any control on a particular div.T ry providing id to each div.

<div class="squares1" id="div1">
0

I guess you want something like

Demo

js

function get_random_color() {
    var letters = '0123456789ABCDEF'.split('');
    var color = '#';
    for (var i = 0; i < 6; i++) {
        color += letters[Math.round(Math.random() * 15)];
    }
    return color;
}

function blink(){
    $('.col').eq(Math.round(Math.random() * (30 * 30)))
    .fadeOut('fast')
    .fadeIn('fast');

    setTimeout(blink,100);
}

var columns = 10,
    container = $("#container"),
    width = (100 / columns);

$("head").append("<style>.col { width: " + width + "%;} .row {  height: " + width + "%  }</style>");

for (var ii = 0; ii < columns; ii++) {
    var $row = $("<div />", {
        class: "row"
    });
    container.append($row);

    for (var i = 0; i < columns; i++) {
        var $col = $("<div />", {
            class: "col",
            style: "background: " + get_random_color() + ";",
            id : ii + "-" + i
        });
        $row.append($col);
    }
}

$("div.col").click(function () {
    alert(this.id + " " + $(this).html());
});

blink();

HTML

<div id="container"></div>

css

#container {
    position: absolute;
    top:0;right:0;bottom:0;left:0;
}
.col { 
    display: inline-block;
    overflow: hidden; 
    height: 100%;
}

Source : How to add blinking effect for the dynamically generated div

Community
  • 1
  • 1
4dgaurav
  • 11,360
  • 4
  • 32
  • 59