burger menu icon
WillMaster

WillMaster > LibraryWeb Content Preparation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Fun Jumper

Just for fun, let's put a div on the web page that jumps. The div can contain any valid HTML content. For this article, the div will contain an image.

When the mouse pointer moves over the image, the image jumps to a new location. For devices without a mouse, tapping the image makes it jump.

The demonstration image is an outline of an envelope within a colored circle. The image has the word "Possibilities" above the envelope.

If you're using a laptop or desktop computer to read this article, try putting the mouse pointer on the demonstration image. The image will jump to a new location.

On the other hand, if you are using a phone or tablet to read the article, tap the image to make it jump.

When the image jumps, it relocates itself to a random location within the browser window (the viewport). The window can be scrolled, and the image remains at its viewport location.

Fun ideas include an image of a squirrel, a small video playing, the words "May problems elude you", a laughing cartoon face, or an eagle in flight. You're probably thinking of something right now that would be fun on your web page.

Here is the source code to implement the demonstration. The source code has HTML and JavaScript. After the demonstration is working, the HTML code may be modified to suit your preference. No customization is required for the JavaScript.

<div style="position:fixed; left:125px; top:200px; width:118px; height:118px;"
onmouseover="JumpToNewLocation(this)" 
onclick="JumpToNewLocation(this)">
<img src="https://www.willmaster.com/images/icons/possibilities-newsletter-2.png" style="max-width:100%;" alt="envelope in a cirle">
</div>

<script type="text/javascript">
function JumpToNewLocation(d)
{
   d.style.top=Math.floor(Math.random()*(window.innerHeight-parseInt(d.style.height)))+"px";
   d.style.left=Math.floor(Math.random()*(window.innerWidth-parseInt(d.style.width)))+"px";
}
</script>

Pop the above code into a test web page, and you should be good to go.

A not about code positioning: Where you place the above code can affect how the fun jumper works. The fun jumper will tend to hide behind images and some other elements that appear in the source code after the fun jumper code. You can see the effect on this page. The fun jumper code is at the end of the article. The fun jumper hides under the "support this website" box below the article and the image in the navigation colomn on the right (or below if you are on a phone).

When you are ready to define your own content instead of the demonstration image, return here for how-to information.

The div contains five inline CSS declarations that are required. The values may be changed for all but one of the declarations. Additional CSS declarations may be added to the div. Here are the five required ones.

  • position:fixed;

    The position:fixed; declaration must be present with the same value as the demonstration.

  • left:125px; top:200px; (both)

    Both left:125px; and top:200px; are required. They position the fun jumper div when the page first loads. Change the values as appropriate for your implementation. Adjust the numbers to your preference.

  • width:118px; height:118px; (both)

    Both width:118px; and height:118px; are required for the JavaScript to calculate a new location. The width and height tell the JavaScript the closest to calculate to the right edge and bottom of the viewport. Adjust the numbers according to your requirements. The width and height values may differ from each other.

Replace the img tag with your own div content.

The fun jumper is easy to implement.

Paste the code into one of your web pages. When you are ready to define your own content, replace the image tag.

"May you bring smiles to your site visitors." — Me

envelope in a cirle

(This content first appeared in Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2026 Will Bontrager Software LLC