Software, your way.
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!

Slow Pulse Image

Sometimes you need to attract attention — without rudely interrupting the site visitor, getting in their face.

A small movement can do the trick, something that attracts attention when attention isn't focused elsewhere.

(stop)

An image with a pulse can provide the small movement. Like the example on the right. Every second, the image either grows 2 pixels bigger or shrinks by 2 pixels.

The reason for the 2 pixels (instead of 1 pixel) size adjustment is to keep the image centered on itself. The image shifts its position 1 pixel left and up when it gets larger by 2 pixels. And the reverse when the size is adjusted smaller.

Here's the source code of the example. Information follows.

<div style="position:relative; height:54px; width:54px;">
<img 
   id="an-image" 
   src="//www.willmaster.com/images/wmlogo_icon.gif" 
   style="position:absolute; left:2px; top:2px; width:50px; height:50px;" 
   alt="Willmaster.com logo">
</div>

<script>
var imageID = "an-image";
var pulseInterval = 1; // Number of seconds, decimal number acceptable.
var originaltop = "2px";
var originalleft = "2px";
var originalwidth = "50px";
var originalheight = "50px";
var smallertop = "3px";
var smallerleft = "3px";
var smallerwidth = "48px";
var smallerheight = "48px";
function Pulse()
{
   var img = document.getElementById(imageID);
   if( parseInt(img.style.width) == parseInt(originalwidth) )
   {
      img.style.top = smallertop;
      img.style.left = smallerleft;
      img.style.width = smallerwidth;
      img.style.height = smallerheight;
   }
   else
   {
      img.style.top = originaltop;
      img.style.left = originalleft;
      img.style.width = originalwidth;
      img.style.height = originalheight;
   }
}
var interval = setInterval(Pulse,parseInt(pulseInterval*1000));
function stopAnimation() { clearInterval(interval); }
</script>

<p>
<a href="javascript:stopAnimation()">(stop)</a>
</p>

The code with an aqua background color at the beginning of the source code and with a beige background color at the end of the source code is HTML and the rest is JavaScript.

In the aqua background color area is a div with an image. The div's style is position:relative; and its dimensions are a couple pixels larger than the image to help position the example. The example dimensions don't need to be adhered to in your implementation. Unless needed for positioning or other reason, dimensions aren't required.

The image in that div has an id value (colored blue). The same an-image id value is specified in the JavaScript so the JavaScript knows which image to manipulate.

On the next line of the JavaScript (colored green) is where you specify the number of seconds for the pulse interval, which may be a decimal number. The example has an interval of 1 second.

The next four lines (colored red) is where the original image position and dimensions are specified, the position and size when the page is first loaded. It would be the same as specified for the image in the HTML code above the JavaScript.

The four lines following those (colored brown) is where the image position and dimensions are specified for the smaller version of the image. Use this table to determine the position and dimensions for the smaller version values.

smallertop1 pixel larger than the number for originaltop
smallerleft1 pixel larger than the number for originalleft
smallerwidth2 pixels smaller than the number for originalwidth
smallerheight2 pixels smaller than the number for originalheight

Doing it that way will keep the image centered on itself while it pulses.

The HTML at the bottom of the source code with the beige background color is optional. It provides a link to stop the image pulse for those who find it annoying.

That's the source code for the pulsing image. It should gently get attention when attention isn't otherwise occupied.

(This article first appeared in Possibilities ezine.)

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-2024 Will Bontrager Software LLC