0

Looking at this question I was able to move my 2 divs around the screen randomly. Look at the CSS of the two divs containing the pictures:

div.a {
 z-index: -101;
 width: 95px;
 height:95px;
 position:fixed;
 top: 100px;
 left: 0px;  
 text-align: center;
}

div.b {
 z-index: -102;
 width: 100px;
 height:100px;
 position:fixed;
 top: 300px;
 right: 0px;  
 text-align: center;
}

div.a img, div.b img { 
 animation-name: giro;
 animation-duration: 10000ms;
 animation-iteration-count: infinite;
 animation-timing-function: linear;
}

As you can see they are 2 divs with the classes a and b and inside they only contain a picture (using the tag img). Using the last piece of CSS the picutre rotates forever inside the div.

The div moves as well (together with the pictures), but he moves with the jQuery code below:

$(document).ready(function() {
    animateDiv($('.a'));
    animateDiv($('.b'));
  });

  function makeNewPosition($container) {

    // Get viewport dimensions (remove the dimension of the div)
    var h = $(window).height() - 100;
    var w = $(window).width() - 100;

    var nh = Math.floor(Math.random() * h);
    var nw = Math.floor(Math.random() * w);

    return [nh, nw];
  }
  
  function animateDiv($target) {
    var newq = makeNewPosition($target.parent());
    var oldq = $target.offset();
    var speed = calcSpeed([oldq.top, oldq.left], newq);

    $target.animate({
        top: newq[0],
        left: newq[1]
    }, speed, function() {
        animateDiv($target);
    });

  };
  
  function calcSpeed(prev, next) {
   var x = Math.abs(prev[1] - next[1]);
   var y = Math.abs(prev[0] - next[0]);
   var greatest = x > y ? x : y;

   //speed of ----- --- moving around
   var speedModifier = 0.03;

   var speed = Math.ceil(greatest/speedModifier);
   return speed;
  } 

The code above is the same as the one you can find in the question I linked. The only differences are the speedModifier (from 1 to 0.03) and the two $(window).height() and $(window).width() because I need the divs to move around the entire screen.


PROBLEM

The fiddle in the question I linked works fine and the divs are moving forever at random positions. In my case (using my code) the divs are moving randomly in the entire screen for a bit but they they stuck in a point of the screen and they don't move anymore.

Is it because I set a too small speedModifier?

FIDDLE OF MY CODE

Gino
  • 19
  • 3
Raffaele Rossi
  • 2,997
  • 4
  • 31
  • 62
  • 1
    Can you create a jsfiddle with your code as a working example? – Dekel Jan 02 '17 at 00:00
  • Yes done, I was going to – Raffaele Rossi Jan 02 '17 at 00:03
  • 5
    "they they stuck in a point of the screen and they don't move anymore" I don't see this happening with the fiddle you liked... how much time do you wait until this happen? – a--m Jan 02 '17 at 00:06
  • 2
    How much time does it take for them to not move anymore? – Dekel Jan 02 '17 at 00:06
  • after 1/2 minutes. If I set the speed Modifier to 0.1 it does not happen. A speed modifier of 0.01 or 0.0090 makes it happen – Raffaele Rossi Jan 02 '17 at 00:08
  • Pictures still rotate, but they won't move anymore. I guess it's due to the ceiling and the small value of the speedModifier – Raffaele Rossi Jan 02 '17 at 00:09
  • 1
    Ah, they have lost acceleration... :) Huh, i really had to wait for this to happen. Btw, even when speed modifier is set to 0.1, after some time - they stack (too much recursion)... btw, animateDiv($target); - is in callback of jQ animate() - and i feel that's the cause - if this could be changed and called on setInterval(), that could be solution... Yep, with higher values - stacking apperas much earlier - so, setInterval, calculate new pos, animate (no callback)... – sinisake Jan 02 '17 at 00:23
  • call a setInterval on what? You wanna use a setInterval instead of an animate()? – Raffaele Rossi Jan 02 '17 at 00:25
  • Whole function, actually. – sinisake Jan 02 '17 at 00:26
  • Could you show me how would you do that? – Raffaele Rossi Jan 02 '17 at 00:28
  • @RaffaeleRossi, i could try... – sinisake Jan 02 '17 at 00:28

0 Answers0