Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7867839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:48:47+00:00 2026-06-03T00:48:47+00:00

I’ve been doing some Googling to find an answer to this, but I’ve had

  • 0

I’ve been doing some Googling to find an answer to this, but I’ve had no luck. It could be because I’m a bit of an amateur and I don’t know the proper terms to search for, but maybe someone here can steer me in the right direction or help me out.

Anyway, I’m looking for a way to get a div to randomly, smoothly move around a page. There will be a background color, then this image which I want to seemingly randomly, infinitely move around the page. Much like the background of a DVD player’s home screen where “DVD” is just floating around.

Starting point of the div doesn’t matter, nor does the ending point. It just needs to randomly move around the page for the duration a user is on that page.

I’ve got decent HTML and CSS skills, very basic JS skills, and some experience implementing jQuery. Ideally, I’d like something which I can implement myself.

Thanks in advance!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:48:50+00:00Added an answer on June 3, 2026 at 12:48 am

    The basic premise is to generate positional values, and use jquery’s animate() function to move the div. The calculation of the next position is simple, you just need a bit of math. Here’s a very basic jsfiddle i just knocked up. It could do with possibly a delay timer, a dynamically calculating speed based on how far its got too move e.c.t. But it gives you a start point i hope.

    http://jsfiddle.net/Xw29r/

    Updated example code with speed modifier:

    http://jsfiddle.net/Xw29r/15/


    For some reason this is still getting some attention, so here’s an updated answer that uses CSS transitions which should be much smoother.

    http://jsfiddle.net/bf9nv1q6/

    function RandomObjectMover(obj, container) {
    	this.$object = obj;
      this.$container = container;
      this.container_is_window = container === window;
      this.pixels_per_second = 250;
      this.current_position = { x: 0, y: 0 };
      this.is_running = false;
    }
    
    // Set the speed of movement in Pixels per Second.
    RandomObjectMover.prototype.setSpeed = function(pxPerSec) {
    	this.pixels_per_second = pxPerSec;
    }
    
    RandomObjectMover.prototype._getContainerDimensions = function() {
       if (this.$container === window) {
           return { 'height' : this.$container.innerHeight, 'width' : this.$container.innerWidth };
       } else {
       	   return { 'height' : this.$container.clientHeight, 'width' : this.$container.clientWidth };
       }
    }
    
    RandomObjectMover.prototype._generateNewPosition = function() {
    
    	// Get container dimensions minus div size
      var containerSize = this._getContainerDimensions();
    	var availableHeight = containerSize.height - this.$object.clientHeight;
      var availableWidth = containerSize.width - this.$object.clientHeight;
        
      // Pick a random place in the space
      var y = Math.floor(Math.random() * availableHeight);
      var x = Math.floor(Math.random() * availableWidth);
        
      return { x: x, y: y };    
    }
    
    RandomObjectMover.prototype._calcDelta = function(a, b) {
    	var dx   = a.x - b.x;         
      var dy   = a.y - b.y;         
      var dist = Math.sqrt( dx*dx + dy*dy ); 
      return dist;
    }
    
    RandomObjectMover.prototype._moveOnce = function() {
    		// Pick a new spot on the page
        var next = this._generateNewPosition();
        
        // How far do we have to move?
        var delta = this._calcDelta(this.current_position, next);
        
    		// Speed of this transition, rounded to 2DP
    		var speed = Math.round((delta / this.pixels_per_second) * 100) / 100;
        
        //console.log(this.current_position, next, delta, speed);
              
        this.$object.style.transition='transform '+speed+'s linear';
        this.$object.style.transform='translate3d('+next.x+'px, '+next.y+'px, 0)';
        
        // Save this new position ready for the next call.
        this.current_position = next;
      
    };
    
    RandomObjectMover.prototype.start = function() {
    
    	if (this.is_running) {
      	return;
      }
    
    	// Make sure our object has the right css set
      this.$object.willChange = 'transform';
      this.$object.pointerEvents = 'auto';
    	
      this.boundEvent = this._moveOnce.bind(this)
      
      // Bind callback to keep things moving
      this.$object.addEventListener('transitionend', this.boundEvent);
      
      // Start it moving
      this._moveOnce();
      
      this.is_running = true;
    }
    
    RandomObjectMover.prototype.stop = function() {
    
    	if (!this.is_running) {
      	return;
      }
      
      this.$object.removeEventListener('transitionend', this.boundEvent);
      
    	this.is_running = false;
    }
    
    
    // Init it
    var x = new RandomObjectMover(document.getElementById('a'), window);
    
    
    // Toolbar stuff
    document.getElementById('start').addEventListener('click', function(){
    	x.start();
    });
    document.getElementById('stop').addEventListener('click', function(){
    	x.stop();
    });
    document.getElementById('speed').addEventListener('keyup', function(){
      if (parseInt(this.value) > 3000 ) {
     		alert('Don\'t be stupid, stupid');
        this.value = 250;
      }
    	x.setSpeed(parseInt(this.value));
    });
    
    
    // Start it off
    
    x.start();
    div#toolbar {
      position:fixed;
      background:#20262F;
      width:100%;
      text-align:center;
      padding: 10px
    }
    div#a {
    width: 50px;
    height:50px;
    background-color:red;
    position:absolute;
    }
    <div id="toolbar">
    <button id="start">Start</button>
    <button id="stop">Stop</button>
    <input type="number" value="250" id="speed" />
    </div>
    <div id='a'></div>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have some data like this: 1 2 3 4 5 9 2 6
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.