//interval變數
var inl = [];
//原始透明度
var opa = 100;
//判斷瀏覽器
var ie = navigator.appName == "Microsoft Internet Explorer" ? "Microsoft Internet Explorer" : "";
//縮小和開啟按紐
function gogo() {
speed = 50;
var mode;
//利用方塊的display來判定是否縮小或放大
if(!$('tmp').style.display) {
mode = "-";
//display是正常的話設定interval縮小
inl['suotu'] = setInterval("suodi('-')", 10);
}else {
mode = "+";
//display是none的話先把display改回去正常,再設定interval放大
$('tmp').style.display = "";
inl['suotu'] = setInterval("suodi('+')", 10);
}
clearInterval(inl['dcdr']);
inl['dcdr'] = setInterval("dcdr('" + mode + "')", 10);
}
//淡出淡入
function dcdr(op) {
//擷取透明度
opa = define(op, opa, 4, 0, 100);
//更新透明度
ie ? $('tmp').style.filter = "alpha(opacity=" + opa + ")" : $('tmp').style.opacity = opa / 100;
//如果透明度小於等於0或大於等於100
if(opa <= 0 || opa >= 100) {
//停止interval
clearInterval(inl['dcdr']);
}
}
function define(id, a, b, a2, a3) {
if(id == "-") {
a -= b;
if(a <= a2) {
a = a2;
}
}else {
a += b;
if(a >= a3) {
a = a3;
}
}
return a;
}
//縮小或放大
function suodi(opt) {
//速度大於1的話繼續除以s,否則等於1
speed = speed > 1 ? speed * 0.862 : 1;
//擷取高度
var hw = define(opt, $('tmp').clientHeight, speed, speed, 300);
//設定高度和寬度
$('tmp').style.height = hw + "px";
$('tmp').style.width = hw + "px";
//假如方塊的高度小於速度
if($('tmp').clientHeight <= speed) {
//隱藏方塊
$('tmp').style.display = "none";
//按鈕名字改為"開啟"
$('anniu').value = "開啟";
//關閉interval
clearInterval(inl['suotu']);
}else if($('tmp').clientWidth >= 300) {
//按鈕名字改為"縮小"
$('anniu').value = "縮小";
//關閉interval
clearInterval(inl['suotu']);
}
}
//簡化getElementById為$
var $ = function($) {
return document.getElementById($);
}
//interval變數
var inl = [];
//原始透明度
var opa = 100;
//判斷瀏覽器
var ie = navigator.appName == "Microsoft Internet Explorer" ? "Microsoft Internet Explorer" : "";
//縮小和開啟按紐
function gogo() {
speed = 50;
var mode;
//利用方塊的display來判定是否縮小或放大
if(!$('tmp').style.display) {
mode = "-";
//display是正常的話設定interval縮小
inl['suotu'] = setInterval("suodi('-')", 10);
}else {
mode = "+";
//display是none的話先把display改回去正常,再設定interval放大
$('tmp').style.display = "";
inl['suotu'] = setInterval("suodi('+')", 10);
}
clearInterval(inl['dcdr']);
inl['dcdr'] = setInterval("dcdr('" + mode + "')", 10);
}
//淡出淡入
function dcdr(op) {
//擷取透明度
opa = define(op, opa, 4, 0, 100);
//更新透明度
ie ? $('tmp').style.filter = "alpha(opacity=" + opa + ")" : $('tmp').style.opacity = opa / 100;
//如果透明度小於等於0或大於等於100
if(opa <= 0 || opa >= 100) {
//停止interval
clearInterval(inl['dcdr']);
}
}
function define(id, a, b, a2, a3) {
if(id == "-") {
a -= b;
if(a <= a2) {
a = a2;
}
}else {
a += b;
if(a >= a3) {
a = a3;
}
}
return a;
}
//縮小或放大
function suodi(opt) {
//速度大於1的話繼續除以s,否則等於1
speed = speed > 1 ? speed * 0.862 : 1;
//擷取高度
var hw = define(opt, $('tmp').clientHeight, speed, speed, 300);
//設定高度和寬度
$('tmp').style.height = hw + "px";
$('tmp').style.width = hw + "px";
//假如方塊的高度小於速度
if($('tmp').clientHeight <= speed) {
//隱藏方塊
$('tmp').style.display = "none";
//按鈕名字改為"開啟"
$('anniu').value = "開啟";
//關閉interval
clearInterval(inl['suotu']);
}else if($('tmp').clientWidth >= 300) {
//按鈕名字改為"縮小"
$('anniu').value = "縮小";
//關閉interval
clearInterval(inl['suotu']);
}
}
//簡化getElementById為$
var $ = function($) {
return document.getElementById($);
}
<body style="margin:0; padding:100px 150px" style="margin:0; padding:100px 150px">
<table style="border:1px solid #ccc" style="border:1px solid #ccc">
<tr>
<td><div style="position:absolute; font-family:'微軟雅黑'; font-size:13px" style="position:absolute; font-family:'微軟雅黑'; font-size:13px"> 示範</div><div id="tmp" style="height:300px; width:300px; background:black"></div></td>
</tr>
<tr>
<td align="center" style="padding-top:20px" style="padding-top:20px">
<input id="anniu" type="button" value="縮小" style="font-family:'微軟雅黑'; font-size:12px; font-weight:bold; border:1px solid #ccc" style="font-family:'微軟雅黑'; font-size:12px; font-weight:bold; border:1px solid #ccc" onfocus="this.blur()" onclick="gogo()"/>
</td>
</tr>
</table>
</body> xmlns="http://www.w3.org/1999/xhtml">