javascript jQuery外掛程式練習

來源:互聯網
上載者:User

簡化後的外掛程式:
SimplePlugin.htm: 複製代碼 代碼如下:<!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" >
<head>
<title>簡化後的外掛程式</title>
<script type="text/ecmascript" src="../js/jquery-1.2.6.js"></script>
<script type="text/ecmascript" src="../js/jquery.SimplePlugin.js"></script>
<script type="text/ecmascript">
$(function() {
$("input").click(function(){
$("body").dialog();
})
});
function f(){
$("body").find("#MaskID").hide(1000);
$("body").find("#DivDialog").hide(1000);
}
</script>
</head>
<body>
<input type="button" value="hi plugin" />
</body>
</html>

jquery.SimplePlugin.js:

複製代碼 代碼如下:$.fn.dialog=function(){
this.MaskDiv=function()//自訂一個函數
{
//建立遮罩背景,這裡沒有設定透明度,為了簡單。zIndex決定了遮罩。
$("body").append("<div ID=MaskID></div>");
$("body").find("#MaskID").width("888px").height("666px")
.css({position:"absolute",top:"0px",left:"0px",background:"#ccc",zIndex:"10000"});
}
this.MaskDiv();//調用自訂函數。
$("body").append("<div ID=DivDialog style='display:none'><ul><li>提示</li></ul><input type='button' value='close' onclick='f();' /></div>");
var obj=$("body").find("#DivDialog");
obj.width("200px").height("200px");
obj.css({position:"absolute",top:"100px",left:"100px",background:"#FFCC66",zIndex:"10001"}).show("slow");

return this;
}

完整的外掛程式:
myplugin.html:

複製代碼 代碼如下:<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>練習jQuery外掛程式</title>
<script type="text/ecmascript" src="../js/jquery-1.2.6.js"></script>
<script type="text/ecmascript" src="../js/jquery.firstplugin.js"> </script>
<script type="text/ecmascript" src="../js/jquery.dialog.js"></script>
<style type='text/css'>
*{padding:0; margin:0} /*此行樣式一定要加,不然可能會引起BUG出現。*/

#MyDiv{
position:absolute;
width:200px;
height:200px;
font-size:12px;
background:#666;
border:1px solid #000;
z-index:10001;
display:none;
text-align:center;
}
</style>
<script type="text/ecmascript">
$(document).ready(function() {
$("input").click(function(){
$("body").dialog();
})
})
</script>
</head>

<body>
<div>
<input type="button" value="hi plugin" />
</div>
</body>
</html>

jquery.dialog.js: 複製代碼 代碼如下:// JScript 檔案
$.fn.dialog=function(){
this.MaskDiv=function()//自訂一個函數
{
var wnd = $(window), doc = $(document);
if(wnd.height() > doc.height()){ //當高度少於一屏
wHeight = wnd.height();
}else{//當高度大於一屏
wHeight = doc.height();
}
//建立遮罩背景
$("body").append("<div ID=MaskID></div>");
$("body").find("#MaskID").width(wnd.width()).height(wHeight)
.css({position:"absolute",top:"0px",left:"0px",background:"#ccc",filter:"Alpha(opacity=90);",opacity:"0.3",zIndex:"10000"});
}
this.sPosition=function(obj)//自訂一個帶參數的函數
{
var MyDiv_w = parseInt(obj.width());
var MyDiv_h = parseInt(obj.height());

var width =parseInt($(document).width());
var height = parseInt($(window).height());
var left = parseInt($(document).scrollLeft());
var top = parseInt($(document).scrollTop());

var Div_topposition = top + (height / 2) - (MyDiv_h / 2); //計算上邊距
var Div_leftposition = left + (width / 2) - (MyDiv_w / 2); //計算左邊距
return Array(Div_topposition,Div_leftposition);
}
this.MaskDiv();
$("body").append("<div ID=DivDialog style='display:none'><ul><li>提示</li></ul></div>");
var obj=$("body").find("#DivDialog");
obj.width("200px").height("200px");
PosT=this.sPosition(obj);
obj.css({position:"absolute",top:PosT[0]+"px",left:PosT[1]+"px",background:"#FFCC66",zIndex:"10001"}).show("slow");
return this;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.