The technique of developing _javascript of JavaScript simple Sketchpad

Source: Internet
Author: User

The example of this article for you to share the production of JavaScript simple Sketchpad method for your reference, the specific content as follows

Html:

<body id= "Bodys" >
<span id= "Eraser" >
Clear All
</span>
<span id= "COLORBTN" >
stroke Color
</span>
<input type= "range" name= "Ram" id= "Ram" min= "1" max= "M" step= "1" value= "ten"/>
<label id= "ramnum" > Font size:10px</label>
<ul id= "Colorpanel" >
<li> Black </li >
<li> red </li>
<li> green </li>
<li> Blue </li>
</ul>
</body>

Css:

*{
margin:0;
padding:0
 }
. box_black{
Background-color:black;
Position:absolute
}
. box_red{
background-color:red;
Position:absolute
}
. box_green{
Background-color:green;
Position:absolute
}
. box_blue{
Background-color:blue;
Position:absolute;
}
#eraser {
width:80px;
height:50px;
Background-color:brown;
Display:inline-block;
Text-align:center;
line-height:50px;
Cursor:pointer;
}
#colorbtn {
width:80px;
height:50px;
Background-color:tomato;
Display:inline-block;
Text-align:center;
line-height:50px;
Cursor:pointer;
}
#colorpanel {
width:80px;
height:200px;
List-style:none;
margin-left:88px;
Display:none;
}
#colorpanel >li{
width:80px;
height:50px;
Text-align:center;
line-height:50px;
Background-color:aquamarine;
Display:inline-block;
Cursor:pointer;
}
#colorpanel >li:hover{
background-color:orange;
}

Javascript:

Window.onload=function () {///Save class name as an array var classname=["Box_black", "box_red", "Box_green", "Box_blue"];
 The default class name is Box_black var clsname=classname[0];
 var Obody=document.getelementbyid ("Bodys");
 var odiv=obody.getelementsbytagname ("div");
 var Eraser=document.getelementbyid ("Eraser");
 var Colorbtn=document.getelementbyid ("Colorbtn");
 var Colorpanel=document.getelementbyid ("Colorpanel");
 var Ram=document.getelementbyid ("Ram");
   var Ramnum=document.getelementbyid ("Ramnum");
   Colorbtn.onmouseover=function () {colorpanel.style.display= "block";
   } colorbtn.onmouseout=function () {colorpanel.style.display= ' none ';
    } colorpanel.onmouseover=function () {this.style.display= "block";
    } colorpanel.onmouseout=function () {this.style.display= ' none ';
    for (Var i=0;i<colorpanel.children.length;i++) {colorpanel.children[i].index=i;
    Colorpanel.children[i].onclick=function () {//mouse click Li Switch class name to change the style clsname=classname[this.index]; Colorpanel.stYle.display= "None";
    }//define default font size of 10px var width= "10px";
    var height= "10px";
    Change font size by sliding range ram.onmousemove=function () {width=height=ram.value+ "px";
    Ramnum.innerhtml= "Font size:" +width; //mouse click on the screen, through the sliding mouse constantly create div attribute node, and give it a style document.onmousedown=function () {document.onmousemove=function (event) {var oe vent=event| |
 window.event; var scrolltop=document.documentelement.scrolltop| |
 Document.body.scrollTop; var scrollleft=document.documentelement.scrollleft| |
 Document.body.scrollLeft;
    var box=document.createelement ("div");
    Box.classname=clsname;
    Box.style.width=width;
    Box.style.height=height;
    Obody.appendchild (box);
    box.style.left=scrollleft+oevent.clientx+ "px";
box.style.top=scrolltop+oevent.clienty+ "px"; }//When the mouse button is released, log off the mouse sliding event document.onmouseup=function () {document.onmousemove=null;}//When the rubber difference button is clicked, Iterate through all the div and remove them all from the parent node eraser.onclick=function () {var odiv=obody.getelementsbytagname ("div"); for (Var i=0;i< odiv.length;i++){Obody.removechild (odiv[i]);} //The following is the Cancel button bubbling event because we clicked the button is not able to draw Div eraser.onmousedown=function (event) {var ievent=event| |
window.event;
Ievent.cancelbubble=true; } colorbtn.onmousedown=function (Event) {var ievent=event| |
window.event;
Ievent.cancelbubble=true; } colorpanel.onmousedown=function (Event) {var ievent=event| |
window.event;
Ievent.cancelbubble=true; } ram.onmousedown=function (Event) {var ievent=event| |
window.event;
Ievent.cancelbubble=true;
 }
}

}

This is based on the JavaScript event based on the basics, relatively simple, in fact, can be further optimized. Look at the effect chart below.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.