Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

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!

Image Slides Forward and Backward

For this article, I wanted something fun. And I got it. Yet, it can have genuine worthwhile uses.

An image in a div slides forward (toward the viewer) or backward (away from the viewer) either automatically or with a tap. It may be coded to respond to other user actions.

Here is an example.

image for transition

(Image from LightFocus.com)

The image automatically slides forward 2 seconds after the code is loaded.

To subsequently slide the image backward or forward, tap anywhere on the image or within the containing div.

While it can be published for amusement, it can be used to attract attention to something of consequence. Examples:

  • An image representing something important to focus on — climate condition, a school or family get-together, news event, … .

  • An image of a product with "Sale ends Sunday" imprinted near the top-left corner. The image slides forward until the imprint is prominent. And slides backward to let the product be seen.

  • An image of a map may display a wide area and then slide forward to display the narrow area of a destination.

How it Works

The CSS specifies (i) a width declaration for the image and (ii) a transform declaration. The value of the transform property specifies that a change in width shall take a specified number of seconds to accomplish (9 seconds for the above example).

JavaScript is used to specify a new image width.

When the JavaScript specifies a new image width, the CSS carries out the width change with a duration of 9 seconds. (A different duration may be specified for your implementation.)

As the width becomes larger, the image appears to move within the div toward the viewer. As the width becomes smaller, it appears to move away.

The Source Code

Below is the source code to recreate the article's example on your own page.

There are three parts. Put them all on the same page and you are good to go.

  1. Here is the div containing the image and the image tag itself. The div may be styled as you wish. The img tag may load any of your publicly available images.

    <div 
       onclick="InitiateTransition()" 
       style="
          border:9px solid #6A6560;
          border-radius:9px; 
          height:200px; 
          width:200px; 
          margin:0 auto; 
          overflow:hidden;">
    <img id="the-image-4-testing" src="https://lightfocus.com/album-imaginative/knitimages/Granny-knits-01.jpg" alt="image for transition">
    </div>
    

    InitiateTransition is the function that specifies a new value for the image width. The function name is also specified twice in the JavaScript (further below).

    the-image-4-testing is the img tag id value. The id value is used in both the CSS and the JavaScript, too.

    If either of the above change, corresponding changes need to be made in the other parts of this system, the CSS and/or the JavaScript.

  2. The CSS is for the image tag.

    <style>
    #the-image-4-testing {
       width:100px;
       transition:width 9s;
    }
    </style>
    

    the-image-4-testing is the img tag id value. The id value is used in both the HTML div tag and the JavaScript, too.

    100 is the number of pixels wide the image renders as when the page first loads. The number is also specified in the JavaScript.

    9s specifies a duration of 9 seconds when the image width is changed. The number 9 may be changed according to your preference.

  3. The HTML code for the div and image:

    <script>
    setTimeout(InitiateTransition,2000);
    function InitiateTransition()
    {
       var minWidth = 100; // pixels
       var img = document.getElementById("the-image-4-testing");
       img.style.width = (parseInt(img.style.width)>minWidth ? minWidth : img.naturalWidth) + "px"; }
    </script>
    

    InitiateTransition is the function that specifies a new value for the image width. The function name is also specified in HTML for the div and image. The line with the first InitiateTransition tells the function to run after 2 seconds have elapsed. The second InitiateTransition assigns the name to the function.

    100 is the minimum width of the image, the same number of pixels wide the image renders as when the page first loads. The number is also specified in the HTML for the div and image.

    the-image-4-testing is the img tag id value. The id value is used in both the CSS and the HTML div tag, too.

To place this article's example on your test page, place all three of the above parts on the page.

The forward and backward sliding image can be a fun addition to your website. It may serve a more serious role of getting attention for something important, too.

(This article first appeared with an issue of the 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-2024 Will Bontrager Software LLC