2013-10-12 4 views
0

Я создал снежную хлопь с отзывчивым сценарием. Все кажется, ничего не проблема. Но я хочу улучшить направление хлопьев, как слева направо и справа налево, так и сверху вниз. Я попытался, но я не уверен, где я совершил ошибку.Анимация с использованием jquery

Вот fiddle

Javascript:

 var fallingSpeed = Math.floor(Math.random() * 5 + 1); 
      var movingDirection = Math.floor(Math.random() * 2); 
      var currentTop = parseInt(jQuery(this).css('top')); 
      var currentLeft = parseInt(jQuery(this).css('left')); 
      jQuery(this).css('top', currentTop + fallingSpeed); 
     alert(currentLeft); 
// 
     //alert(movingDirection); 
       if (movingDirection === 0) { 
        jQuery(this).css('left', currentLeft + fallingSpeed); 
       } else { 
        // set the snow move to left 
        jQuery(this).css('left', currentLeft + -(fallingSpeed)); 
       } 

Как я могу переместить чешуйки слева направо и справа налево? Любое предложение было бы замечательным.

Спасибо.

+2

да Javascript snowflakes..reminds меня 90-х;) – bitWorking

+0

[HOT] Следуйте этому URL - http://stackoverflow.com/questions/19333422/create-random-fall-object-in-jquery/19333584? noredirect = 1 – avalkab

ответ

1

Попробуйте step обратного вызова функции JQuery одушевленных:

$(function(){ 
    var drop = $('.drop').detach(); 
    function create(){ 
    var clone = drop 
     .clone() 
     .appendTo('.container') 
     .css('left', Math.random()*$(document).width()-20) 
    .html('*') 
     .animate(
       {'top': $(document).height()-20}, 
      { 
       duration: Math.random(1000)+8000, 
       complete:function(){ 
       $(this).fadeOut(200,function(){$(this).remove();}); 
       }, 
       step:function (currentTop){ 
            var fallingSpeed = Math.floor(Math.random() * 5 + 1); 
      var movingDirection = Math.floor(Math.random() * 2); 
      var currentTop = parseInt(jQuery(this).css('top')); 
      var currentLeft = parseInt(jQuery(this).css('left')); 
      jQuery(this).css('top', currentTop + fallingSpeed); 

       if (movingDirection === 0) { 
        jQuery(this).css('left', currentLeft + fallingSpeed); 
       } else { 
        // set the snow move to left 
        jQuery(this).css('left', currentLeft + -(fallingSpeed)); 
       } 
       } 
      }); 
    } 

DEMO

+0

+1 Спасибо, что отлично работает :) –

Смежные вопросы