Non-inline style functions
<!doctype html>
<meta charset= "UTF-8" >
<title>Document</title>
<style>
* {
Text-align:center;
}
</style>
<body>
<input type= "button" value= "Style" id= "btn"/>
<div id= "Div1" > Hello </div>
<!--//Get functions for non-inline CSS styles--
<script>
function GetStyle (obj, attr) {//Get non-inline style, obj is an object, attr is a value
if (obj.currentstyle) {//for IE get non-inline style
return obj.currentstyle[attr];
} else {
return getComputedStyle (obj, false) [attr];//for non-IE
}
}
//write/Get CSS style for object
function css (obj, attr, value) {//object, style, value, two parameters to get the style, 3 to set the style
if (arguments.length = = 2) {
return GetStyle (obj, attr)
} else {
if (arguments.length = = 3) {
obj.style[attr] = value;
}
}
}
window.onload = function () {
var odiv = document.getElementById (' Div1 ');
var obtn = document.getElementById (' btn ');
Obtn.onclick = function () {
alert (GetStyle (odiv, ' height '));
css (odiv, ' background ', ' green ');
alert (CSS (odiv, "width"));
}
}
</script>
</body>
//Multi-object animation (animation with transparency)
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title> Multi-object complex movement with transparency </title>
<style>
*{
margin:0;
padding:0;
font-size:12px;
}
Table {
Border-collapse:collapse;
border-spacing:0;
}
fieldset, img {
border:0
}
address, caption, cite, code, DFN, EM, strong, Th, var {
Font-style:normal;
Font-weight:normal
}
OL, ul {
List-style:none
}
caption, Th, TD {
Text-align:center
}
H1, H2, H3, H4, H5, h6 {
font-size:100%;
Font-weight:normal
}
Q:before, Q:after {
content: '
}
ABBR, acronym {
border:0
}
. odiv {
position:relative;
}
. Odiv ul li {
width:200px;
height:100px;
Background:yellow;
margin-bottom:20px;
border:2px solid #000;
}
#li1 {
opacity:0.3;
Filter:alpha (opacity:30);
}
</style>
<body>
<div id= "Odiv" class= "Odiv" >
<ul>
<li id= "Li1" ></li>
<li id= "Li2" ></li>
<li id= "Li3" ></li>
</ul>
</div>
<script>
window.onload= function () {
var Li1=document.getelementbyid (' Li1 ');
var Li2=document.getelementbyid (' Li2 ');
var Li3=document.getelementbyid (' Li3 ');
li1.onmouseover= function () {
Startmov (this,100, ' opacity ');
};
li1.onmouseout= function () {
Startmov (this,30, ' opacity ')
};
li2.onmouseover=function () {
Startmov (this,200, ' height ')
};
li2.onmouseout= function () {
Startmov (this,100, ' height ')
};
li3.onmouseover= function () {
Startmov (this,400, ' width ')
};
li3.onmouseout= function () {
Startmov (this,200, ' width ')
};
Adds a timer to the current three objects, respectively.
Li1.timer=null;
Li2.timer=null;
Li3.timer=null;
};
//move in to move out the animated function triggered by
function Startmov (obj,itarget,attr) {
clearinterval (Obj.timer);//clear the current timer before executing
//Add a timer to the current object
Obj.timer=setinterval (function () {
var icur=0;
if (attr== ' opacity ') {
Icur=math.round (parsefloat (GetStyle (obj,attr)) *100);//computers are often inaccurate when calculating decimals
}else{
Icur=parseint (GetStyle (obj,attr));
}
var speed=0;
speed= (itarget-icur)/8;
speed=speed>0? Math.ceil (Speed): Math.floor (speed);
if (icur==itarget) {
clearinterval (obj.timer);
}else{
if (attr== ' opacity ') {
obj.style.filter= ' alpha (opacity: ' + (icur+speed) + ') ';
obj.style.opacity= (icur+speed)/100;
}else {
obj.style[attr]=icur+speed+ ' px ';
}
}
},30);
}
//Get non-inline style
function GetStyle (obj,attr) {
if (obj.currentstyle) {
return obj.currentstyle[attr];
}else {
return getComputedStyle (Obj,false) [attr];
}
}
</script>
</body>
Get non-inline style functions and multi-object animations (native JS notation)