This article examples describe how to achieve the effect of imitating QQ show dress-up in js. Share it for your reference. The specific implementation method is as follows:
Copy the code as follows:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="www.w3.org/1999/xhtml">
<head>
<title>Imitation QQ show dress-up effect</title>
<meta http-equiv="content-Type" content="text/html;charset=gb2312">
<!--Add the following code between <head> and </head>-->
<style>
#cs img{cursor:hand}
</style>
<base href="http://www.zzsky.cn/effect/images/qqshow/">
</head>
<body>
<!--Add the following code between <body> and </body>-->
<!-- Place the following code where you want to display the image preview-->
<div id="bodyshow" style="border:1px solid #000000;padding:0;POSITION: relative; Left: 0px; TOP: 0px; HEIGHT: 226px; WIDTH: 140px;"></div>
<!-- This is a submission form, assigning a string representing the user image to the hidden domain userrequip Submit->
<form name="equipform" method="post" action="">
<input name="userequip" type="hidden" value="">
<input name="saveequip" type="submit" value="Save image" >
<input name="toreequip" type="button" value="original image" onclick="shoiwit('df>df>df>0');return false;" >
</form>
<script language="JavaScript">
<!--
var myequip="df>df>df>0"; //The initial display configuration code can be read and output by the server.
function shoewit(equip){ //This function is configured as a parameter to display virtual avatar
showlayers=equip.split('>'); //Use ">" as the separator to assign the image names of each layer to an array showlayers[]
str="";
for(i=0;i<showlayers.length;i++){
if(showlayers[i]!='0'&&showlayers[i]!=''){ //If the image name is 0 or empty, the layer will not be displayed.
str+="<img src='"+(i+1)+"/"+showlayers[i]+".gif' style='padding:0;position:absolute;top:0;left:0;width:140;height:226;z-index:"+(i+1)+";'>";
}
}
//Finally cover a completely transparent image on the top layer, so that the user can only save this image in the right link above>Save As:
str+="<img src='blank.gif' style='padding:0;position:absolute;top:0;left:0;width:140;height:226;z-index:100;'>";
if(equipform.userequip) equipform.userequip.value=myequip=equip; // Assign the configuration code to the hidden domain
bodyshow.innerHTML=str; //Show pictures of each layer.
}
//After the page loading, the initial virtual image is displayed first:
document.body.onload=new Function("shoiwit(myequip)");
function dressit(layer,img){ //This function is used to change the configuration, the parameters are the number of layers, the image name
//event.returnValue=false;
showlayers=myequip.split('>');
newequip="";
for(i=0;i<showlayers.length;i++){
if(i+1==layer){
if(img==showlayers[i]) newequip+="df"; //If the layer is already this picture, it will be restored to the original picture
else newequip+=img; //Otherwise change to this picture
}
else newequip+=showlayers[i]; //The other layers of pictures remain unchanged
if(i+1!=showlayers.length) newequip+=">";
}
Shoiwit(newequip); //Show the latest configuration
}
-->
</script>
<script event="onclick" for="cs">
var obj=event.srcElement;
if(obj.tagName!="IMG") return;
var vars=obj.src.match(///(/d)//(/d)x/.gif$/);
dressit(vars[1],vars[2]);
</script>
<table cellpacing="0" cellpadding="0" id="cs">
<tr>
<td><img src="4/1x.gif"></td>
<td><img src="4/2x.gif"></td>
<td><img src="4/3x.gif"></td>
<td><img src="4/4x.gif"></td>
</tr>
<tr>
<td><img src="3/1x.gif"></td>
<td><img src="3/2x.gif"></td>
<td><img src="3/3x.gif"></td>
<td><img src="3/4x.gif"></td>
</tr>
<tr>
<td><img src="2/1x.gif"></td>
<td><img src="2/2x.gif"></td>
<td><img src="2/3x.gif"></td>
<td><img src="2/4x.gif"></td>
</tr>
<tr>
<td><img src="1/1x.gif"></td>
<td><img src="1/2x.gif"></td>
<td><img src="1/3x.gif"></td>
<td><img src="1/4x.gif"></td>
</tr>
</table>
</body>
</html>
The operation effect is shown in the figure below:
I hope this article will be helpful to everyone's JavaScript programming.