Javascript動態伸縮+淡出淡入

來源:互聯網
上載者:User

//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">


示範
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.