In this article, you learn how to get and set a webpage element's z-index. The example code provided used these principles to shuffle a small deck of playing cards by clicking or double-clicking on their images. You might use this information to create your own webpage card game, or for some other interesting application.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site
Your Ad Here!
Your ad here for one full year, only $20. Send me your ad copy, up to six lines, I'll post it ASAP. After you inspect your ad online, you can pay through PayPal.
Click here for more information.

How to Shuffle the Deck With Java Script

An element on a webpage can be overlapping or on top of another element. If you specify several elements positions such that they overlap or are on top of each other, by default they are stacked from bottom to top in the order that their code appears in the webpage document. The relative position of an element in the stack is indicated by a number called its z-index. The lower the z-index, the closer the element will be to the bottom of the stack. An element's z-index can be a negative number.

In this article, I demonstrate how to get and set a webpage element's z-index. In this example I display a small deck of playing cards. When you click on a card, it moves to the top of the stack. When you double-click on a card, it moves to the bottom of the stack. You can probably think of many interesting applications for the principles demonstrated in this article. Below is a working example of the code that I will provide.

The code to achieve this effect is amazingly simple. Each playing card image is contained within a div element. Each div is given an id attribute and a style attribute. The id attribute is used to refer to each specific card to get and set its z-index. The style attribute sets the location and initial z-index of each playing card image. The code for this is shown below.

<div id="card1" style="position:absolute; left:0px; top:0px; z-index:0;" onclick="movetoFront(this)" ondblclick="movetoBack(this)"><img src="c_a.png"></div>
<div id="card2" style="position:absolute; left:30px; top:30px; z-index:1;" onclick="movetoFront(this)" ondblclick="movetoBack(this)"><img src="c_2.png"></div>
<div id="card3" style="position:absolute; left:60px; top:60px; z-index:2;" onclick="movetoFront(this)" ondblclick="movetoBack(this)"><img src="c_3.png"></div>
<div id="card4" style="position:absolute; left:90px; top:90px; z-index:3;" onclick="movetoFront(this)" ondblclick="movetoBack(this)"><img src="c_4.png"></div>
<div id="card5" style="position:absolute; left:120px; top:120px; z-index:4;" onclick="movetoFront(this)" ondblclick="movetoBack(this)"><img src="c_5.png"></div>

In the code above, note that the position property is set to absolute. The left and top properties set the upper-left corner of each playing card image to its desired position on the webpage. The z-index property sets the stacking order of the cards. If you would like to try this example on your own webpage, right-click on each card and in the popup menu that appears, select "save Picture As...". Then in the "Save Picture" dialog box that appears navigate to a folder where you want to save the image file.

In the code, each div defines two events, onclick and ondblclick. When you click on a playing card, the onclick event calls a Java Script funtion named movetoFront, passing a reference to the div element to the function. When you double-click on a playing card, the ondblclick event calls a Java Script function named movetoBack, passing a reference to the div element to the function. Shown below is the code for the movetoFront function.

Web Design Sections

RSS Feed RSS Feed

Easy Java Script Code
Easy Java Script Timer Code
Easy Java Script Windows
Easy Rollovers
Easy Picture Transition Effects
Easy Moving Popup Code
Easy Slide Show Code
Easy Slide Show Code With Linked Slides
Slide Show with Different Size Images
Easy Picture Zoom Code
Easy Picture Panning Code
Easy Java Script Animation
Easy JavaScript Picture Selector Code
Easy Animated Banners with Java Script
Easy Java Script Form Validation
Easy Drag n Drop Code
Easy Graph Generating Code
Easy Code to Sort a Table by Column
Web Designer's Reference
Easy Fading Text Banner Code
Easy Expanding Banner Code
Calendars for Your Website
Date Picker For Your Web Site
Calculators For Your Web Site
Loan Payment Calculator
Length Units Converter
Body Mass Index
Fahrenheit to Celsius Converter
Decimal to Hexidecimal Converter
Easy Code for Screen Tape Adding Machine
Code for Java Script Circle⁄Sphere Calculator
Round a Float to 4 Digits to the Right of the Decimal Point
Make Your Own Graphical Digital Clock
Let Your Web site Visitors Set Font Size
How to Disable the Browser Back Button
Add More Bang to Your Content With Keyword Popup Menus
Put a Music Off Switch on Your Webpage
Java Script Random Password Generator
Password Protection Using Java Script
Replace Drop-down with Drag-and-drop
Submit Forms Without CGI
Code for a Less annoying Popup Window
Using the Java Script Array Object
Using the Java Script Date Object
Java Script Message Boxes
How to Shuffle the Deck With Java Script
Web Site Menus : Which Section Am I In?
How Far Did the User Scroll?
Where Did the User Click?
Four Ways to Use Java Script Event Handlers
Create Your Own Database Using Only Notepad : CDV

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269