Home > Articles > Open Source > Ajax & JavaScript

This chapter is from the book

This chapter is from the book

10.5 Dynamic Styles

An element's style can be changed dynamically. Often such a change is made in response to user events, which we discuss in Chapter 11. Such style changes can create many effects, including mouse hover effects, interactive menus, and animations. Figure 10.4 is a simple example that changes the background-color style property in response to user input.

 1   <?xml version = "1.0" encoding = "utf-8"?>
 2   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 3      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 5   <!-- Fig. 10.4: dynamicstyle.html -->
 6   <!-- Dynamic styles. -->
 7   <html xmlns = "http://www.w3.org/1999/xhtml">
 8      <head>

Fig. 10.4. Dynamic styles. (Part 1 of 2.)

 9         <title>Dynamic Styles</title>
10         <script type = "text/javascript">
11            <!--
12            function start() 
13            {
14               var inputColor = prompt( "Enter a color name for the " +
15                  "background of this page", "" );                     
16               document.body.style.backgroundColor = inputColor;       
17            } // end function start
18            // -->
19         </script>
20      </head>
21      <body id = "body" onload = "start()">
22         <p>Welcome to our website!</p>
23      </body>
24   </html>





Fig. 10.4. Dynamic styles. (Part 2 of 2.)

Function start (lines 12–17) prompts the user to enter a color name, then sets the background color to that value. [Note: An error occurs if the value entered is not a valid color.] We refer to the background color as document.body.style.backgroundColor—the body property of the document object refers to the body element. We then use the style property (a property of most XHTML elements) to set the background-color CSS property. This is referred to as backgroundColor in JavaScript—the hyphen is removed to avoid confusion with the subtraction (-) operator. This naming convention is consistent for most CSS properties. For example, borderWidth correlates to the border-width CSS property, and fontFamily correlates to the font-family CSS property. In general, CSS properties are accessed in the format node.style.styleproperty.

Figure 10.5 introduces the setInterval and clearInterval methods of the window object, combining them with dynamic styles to create animated effects. This example is a basic image viewer that allows you to select a Deitel book cover and view it in a larger size. When one of the thumbnail images on the right is clicked, the larger version grows from the top-left corner of the main image area.

 1   <?xml version = "1.0" encoding = "utf-8"?>
 2   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 3      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 5   <!-- Fig. 10.5: coverviewer.html -->
 6   <!-- Dynamic styles used for animation. -->
 7   <html xmlns = "http://www.w3.org/1999/xhtml">
 8      <head>
 9         <title>Deitel Book Cover Viewer</title>
10         <style type = "text/css">
11            .thumbs   { width: 192px; 
12                        height: 370px; 
13                        padding: 5px; 
14                        float: left } 
15            .mainimg  { width: 289px; 
16                        padding: 5px; 
17                        float: left } 
18            .imgCover { height: 373px } 
19            img       { border: 1px solid black } 
20         </style>
21         <script type = "text/javascript">
22            <!-- 
23            var interval = null; // keeps track of the interval
24            var speed = 6; // determines the speed of the animation
25            var count = 0; // size of the image during the animation
27            // called repeatedly to animate the book cover
28            function run() 
29            {
30               count += speed;
32               // stop the animation when the image is large enough
33               if ( count >= 375 )
34               {

Fig. 10.5. Dynamic styles used for animation. (Part 1 of 4.)

35                  window.clearInterval( interval );
36                  interval = null;                 
37               }  // end if
39               var bigImage = document.getElementById( "imgCover" );
40               bigImage.style.width = .7656 * count + "px";
41               bigImage.style.height = count + "px";
42            } // end function run
44             // inserts the proper image into the main image area and
45            // begins the animation
46            function display( imgfile )
47            {
48               if ( interval )
49                  return;
51               var bigImage = document.getElementById( "imgCover" );
52               var newNode = document.createElement( "img" );
53               newNode.id = "imgCover";
54               newNode.src = "fullsize/" + imgfile;
55               newNode.alt = "Large image";
56               newNode.className = "imgCover";
57               newNode.style.width = "0px";
58               newNode.style.height = "0px";
59               bigImage.parentNode.replaceChild( newNode, bigImage );
60               count = 0; // start the image at size 0
61               interval = window.setInterval( "run()", 10 ); // animate
62            } // end function display
63            // -->
64         </script>
65      </head>
66      <body>
67         <div id = "mainimg" class = "mainimg">
68            <img id = "imgCover" src = "fullsize/iw3htp4.jpg"
69               alt = "Full cover image" class = "imgCover" />
70         </div>
71         <div id = "thumbs" class = "thumbs" >
72            <img src = "thumbs/iw3htp4.jpg" alt = "iw3htp4"
73               onclick = "display( 'iw3htp4.jpg' )" />
74            <img src = "thumbs/chtp5.jpg" alt = "chtp5"
75               onclick = "display( 'chtp5.jpg' )" />
76            <img src = "thumbs/cpphtp6.jpg" alt = "cpphtp6"
77               onclick = "display( 'cpphtp6.jpg' )" />
78            <img src = "thumbs/jhtp7.jpg" alt = "jhtp7"
79               onclick = "display( 'jhtp7.jpg' )" />
80            <img src = "thumbs/vbhtp3.jpg" alt = "vbhtp3"
81               onclick = "display( 'vbhtp3.jpg' )" />
82            <img src = "thumbs/vcsharphtp2.jpg" alt = "vcsharphtp2"
83               onclick = "display( 'vcsharphtp2.jpg' )" />
84         </div>
85      </body>
86   </html>

Fig. 10.5. Dynamic styles used for animation. (Part 2 of 4.)


a) The cover viewer page loads with the cover of this book.


b) When the user clicks the thumbnail of C How to Program, the full-size image begins growing from the top-left corner of the window.

Fig. 10.5. Dynamic styles used for animation. (Part 3 of 4.)


c) The cover continues to grow.


d) The animation finishes when the cover reaches its full size.

Fig. 10.5. Dynamic styles used for animation. (Part 4 of 4.)

The body (lines 66–85) contains two div elements, both floated left using styles defined in lines 14 and 17 in order to present them side by side. The left div contains the full-size image iw3htp4.jpg, which appears when the page loads. The right div contains six thumbnail images which respond to the click event by calling the display method and passing it the filename of the corresponding full-size image.

The display function (lines 46–62) dynamically updates the image in the left div to the one corresponding to the user's click. Lines 48–49 prevent the rest of the function from executing if interval is defined (i.e., an animation is in progress.) Line 51 gets the left div by its id, imgCover. Line 52 creates a new img element. Lines 53–55 set its id to imgCover, set its src to the correct image file in the fullsize directory, and set its required alt attribute. Lines 56–59 do some additional initialization before beginning the animation in line 61. To create the growing animation effect, lines 57–58 set the image width and height to 0. Line 59 replaces the current bigImage node with newNode (created in line 52), and line 60 sets count, the variable that controls the animation, to 0.

Line 61 introduces the window object's setInterval method, which starts the animation. This method takes two parameters—a statement to execute repeatedly, and an integer specifying how often to execute it, in milliseconds. We use setInterval to call function run every 10 milliseconds. The setInterval method returns a unique identifier to keep track of that particular interval—we assign this identifier to the variable interval. We use this identifier to stop the animation when the image has finished growing.

The run function, defined in lines 28–42, increases the height of the image by the value of speed and updates its width accordingly to keep the aspect ratio consistent. Because the run function is called every 10 milliseconds, this increase happens repeatedly to create an animated growing effect. Line 30 adds the value of speed (declared and initialized to 6 in line 24) to count, which keeps track of the animation's progress and dictates the current size of the image. If the image has grown to its full height (375), line 35 uses the window's clearInterval method to stop the repetitive calls of the run method. We pass to clearInterval the interval identifier (stored in interval) that setInterval created in line 61. Although it seems unnecessary in this script, this identifier allows the script to keep track of multiple intervals running at the same time and to choose which interval to stop when calling clearInterval.

Line 39 gets the image and lines 40–41 set its width and height CSS properties. Note that line 40 multiplies count by a scaling factor of .7656 in order to keep the ratio of the image's dimensions consistent with the actual dimensions of the image. Run the code example and click on a thumbnail image to see the full animation effect.

This section demonstrated the concept of dynamically changing CSS styles using JavaScript and the DOM. We also discussed the basics of how to create scripted animations using setInterval and clearInterval.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020