Artikel ini menganalisis kesan lapisan topeng jQuery melalui contoh. Kongsikan dengan semua orang untuk rujukan anda, butirannya adalah seperti berikut:
Mari kita lihat kod sampel dahulu:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh" lang="zh" dir="ltr"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <mce:script type="text/javascript" src="jquery-1.4.4.min.js" mce_src="jquery-1.4.4.min.js"></mce:script> <title>Example | xHTML1.0</title> <mce:style> <!-- *{ font-family:Arial, Helvetica, sans-serif; font-size:12px; } #full_box{ background-color:gray; display:none; z-index:3; position:absolute; left:0px; top:0px; filter:Alpha(Opacity=30); /* IE */ -moz-opacity:0.4; /* Moz + FF */ opacity: 0.4; } #dialog{ position:absolute; width:200px; height:200px; background:#F00; display:none; z-index:5; } --> </mce:style> <style mce_bogus="1"> *{ font-family:Arial, Helvetica, sans-serif; font-size:12px; } #full_box{ background-color:gray; display:none; z-index:3; position:absolute; left:0px; top:0px; filter:Alpha(Opacity=30); /* IE */ -moz-opacity:0.4; /* Moz + FF */ opacity: 0.4; } #dialog{ position:absolute; width:200px; height:200px; background:#F00; display:none; z-index:5; } </style> <mce:script type="text/javascript"> <!-- function showBox() { var bH = $(window).height(); var bW = $(window).width(); $("#full_box").css({width:bW,height:bH,display:"block"}); var objWH = getObjWh('dialog'); var tbT = objWH.split("|")[0] + "px"; var tbL = objWH.split("|")[1] + "px"; $("#dialog").css({top:tbT,left:tbL,display:"block"}); $("#dialog_content").html("<div style="text-align:center" mce_style="text-align:center">正在加载,请稍后...</div>"); $(window).scroll(function (){ resetBox();}); $(window).resize(function (){ resetBox();}); } function resetBox() { var full_box = $("#full_box").css("display"); if (full_box == 'block') { var bH = $(window).height(); var bW = $(window).width(); var objWH = getObjWh('dialog'); var tbT = objWH.split("|")[0] + "px"; var tbL = objWH.split("|")[1] + "px"; $("#dialog").css({top:tbT,left:tbL,display:"block"}); } } function getObjWh(obj) { var st = $(window).scrollTop(); var sl = $(window).scrollLeft(); var ch = $(window).height(); var cw = $(window).width(); var objH = $("#"+obj).height(); var objW = $("#"+obj).width(); var objT = Number(st) + (Number(ch) - Number(objH))/2; var objL = Number(sl) + (Number(cw) - Number(objW))/2; return objT +"|" +objL; } function closeBox() { $("#dialog").css("display","none"); $("#full_box").css("display","none"); } // --> </mce:script> <button id="click" onclick="showBox()">click</button> <div id="full_box"></div> <div id="dialog"> <div id="dialog_content"></div> <div style="text-align:center;" mce_style="text-align:center;"> <a href="#" mce_href="#" onclick="closeBox();">关闭</a> </div> </div> </body> </html>
Sebenarnya, prinsip lapisan topeng adalah sangat mudah.
Div merangkumi kandungan di bawah.
Salah satu gaya css yang lebih kritikal ialah
indeks x: nilai integer
Semakin besar nilai, semakin tinggi, semakin kecil nilainya, semakin rendah ia boleh menjadi nombor negatif.
Kod js di atas mempunyai beberapa ralat. Perkara berikut telah diperbetulkan.
//显示层 function showBox(id) { var bH = document.body.offsetHeight;//$(window).height(); var bW = document.body.offsetWidth;//$(window).width(); if (bH < $(window).height()) { bH = $(window).height(); } $("#full_box").css({width:bW,height:bH,display:"block"}); var objWH = getObjWh('dialog'); var tbT = objWH.split("|")[0] + "px"; var tbL = objWH.split("|")[1] + "px"; if(id=='template'){ $("#div_template").show(); }else if(id == 'history'){ $("#div_history").show(); }else{ $("#tree_"+id).show(); } $(window).scroll(function (){ resetBox(id);}); $(window).resize(function (){ resetBox(id);}); } //重置层 function resetBox(id) { var full_box = $("#full_box").css("display"); if (full_box == 'block') { var bH = document.body.offsetHeight;//$(window).height(); var bW = document.body.offsetWidth;//$(window).width(); if (bH < $(window).height()) { bH = $(window).height(); } var objWH = getObjWh('dialog'); var tbT = objWH.split("|")[0] + "px"; var tbL = objWH.split("|")[1] + "px"; $(".dialog").css({top:tbT,left:tbL}); $("#full_box").css({width:bW,height:bH}); } } //获得层参数 function getObjWh(obj) { var st = $(window).scrollTop(); var sl = $(window).scrollLeft(); var ch = $(window).height(); var cw = $(window).width(); var objH = $("#"+obj).height(); var objW = $("#"+obj).width(); var objT = Number(st) + (Number(ch) - Number(objH))/2; var objL = Number(sl) + (Number(cw) - Number(objW))/2; return objT +"|" +objL; } //关闭层 function closeBox(id) { if(id == 'template'){ $("#div_template").hide(); }else if(id == 'history'){ $("#div_history").hide(); }else{ $("#tree_"+id).hide(); } $("#full_box").hide(); }
Pembaca yang berminat dengan lebih banyak kandungan yang berkaitan dengan kesan khas jQuery boleh menyemak topik khas tapak ini: "Ringkasan kesan khas klasik biasa dalam jQuery"
Saya harap artikel ini akan membantu semua orang dalam pengaturcaraan jQuery.