如何为动态生成的div添加闪烁效果

时间:2013-09-17 11:42:25

标签: jquery css

我需要默认添加自发闪烁效果(不点击div)到下面链接中给出的动态生成的div。

演示:http://jsfiddle.net/ramapriya/xeYnv/1/

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;
}

var columns = 40,
  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());
});
#container {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}
.col {
  display: inline-block;
  outline: 1px solid purple;
  overflow: hidden;
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="container"></div>

3 个答案:

答案 0 :(得分:10)

您可以通过.col随机选择eq() div并通过fadeInfadeOut函数添加眨眼效果来实现。

添加此功能:

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

然后在blink中调用setInterval(blink,100);功能以启动效果。

Here is an example in jsFiddle

以下是示例摘录:

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() * (40 * 40)))
    .fadeOut('fast')
    .fadeIn('fast');
}

var columns = 40,
    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());
});

setInterval(blink,100);
#container {
    position: absolute;
    top:0;right:0;bottom:0;left:0;
}
.col { 
    display: inline-block;
    outline: 1px solid purple;
    overflow: hidden; 
    height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<div id="container"></div>
  

答案 1 :(得分:0)

我不知道它是否影响任何其他功能,但它有效

<div id='blink_me'>Text You want to blink goes here...!</div>


    <script type='text/javascript'>
     $(document).ready(function(){
     hideMe();
     });


    function showMe(){
       $('#blink_me').fadeIn(700);
       hideMe();
    }


    function hideMe(){
      $('#blink_me').fadeOut(700);
      showMe();
    }
   </script>

答案 2 :(得分:0)

文字你想要闪烁到这里......!
<script type='text/javascript'>
 $(document).ready(function(){
 blinkMe();
 });


function blinkMe(){
   $('#blink_me').fadeOut(700).fadeIn(700);
   setTimeout(blinkMe,100);// or blinkMe();
}