DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> jQuery 動畫彈出窗體支持多種展現方式
jQuery 動畫彈出窗體支持多種展現方式
編輯:JQuery特效代碼     

打包下載


動畫效果
  從哪個對象上觸發的即從該對象開始逐漸向屏幕中間移動,並逐漸展開,展開後裡面的顯示對象再從上到下慢慢展開。點擊關閉時,先將展開的顯示的對象慢慢縮回,然後再慢慢移到觸發的對象上面。
  說的有點繞,我自己都不明白是什麼意思,說白了就是從哪兒來回哪兒去。
  展現方式
    第一種:string
    這是最簡單最明了的方式,不用多說,就是直接賦值字符串並顯示出來。
    第二種:ajax
    這種是支持ajax的展現,就是異步獲取數據並展示出來。
    第三種: iframe
    顧名思義就是支持嵌套iframe顯示。
    第四種:controls
    這個名字有點不太好理解,就是將頁面的某個對象展現出來。比如:document.getElementById("showName");
  插件代碼實現

代碼如下:
(function($){
$.alerts = {
alert : function(o,options){
var defaults = {
title : '標題',
content : '內容',
GetType : 'string', //controls,ajax,string,iframe
IsDrag : true,
Url : '',
Data : null,
width:400,
height:300,
callback : function(){}
}
var options = $.extend(defaults,options);
if(!$("#window")[0])
{
$.alerts._createObject();
}
var position = $.alerts._getPosition(o);
var winPosition = $.alerts._getWindowPosition(options);
$("#windowContent").hide();
$("#window").css(
{
width:position.w,
height:position.h,
top:position.t,
left:position.l,
zIndex:1001
}
);
$("#windowBottom,#windowBottomContent").css(
{
height:options.height-30
}
);
$("#windowContent").css(
{
height:options.height - 45,
width:options.width - 25
}
);
$("#windowTopContent").html(options.title);
switch(options.GetType){
case "string":
$("#windowContent").html(options.content);
break;
case "ajax":
if(options.Url == ''){
alert("AjaxUrl不能為空");
return;
}else{
$.ajax(
{
type: "POST",
url: options.Url,
data: options.Data,
success: function(msg){
$("#windowContent").html(msg);
}
}
);
}
break;
case "controls":
$("#windowContent").html(options.content.innerHTML);
break;
case "iframe":
$("#windowContent").empty();
$("<iframe>").attr(
{
src : options.Url,
width:options.width,
height:options.height
}
).appendTo("#windowContent");
break;
}
$("#window").animate(
{
left:winPosition.l,
top:winPosition.t,
height:winPosition.h,
width:winPosition.w
},500,function(){
//$("#windowContent").fadeIn('slow');
$("#windowContent").slideDown(600);
if($("#middleElement_bgDiv").get().length == 0){
$("<div>").attr("id","middleElement_bgDiv").css(
{
position:"absolute",
left:"0px",
top:"0px",
width:$(window).width()+"px",
height:Math.max($("body").height(),$(window).height())+"px",
filter:"Alpha(Opacity=40)",
opacity:"0.4",
backgroundColor:"#AAAAAA",
zIndex:"1000",
margin:"0px",
padding:"0px"
}
).appendTo("body");
}else{
$("#middleElement_bgDiv").show();
}
}
);
$("#windowClose").one("click",function(){
$("#windowContent").slideUp(600,function(){
$("#window").animate(
{
left:position.l,
top:position.t,
height:position.h,
width:position.w
},500,function(){
$(this).hide();
if($("#middleElement_bgDiv").get().length > 0){
$("#middleElement_bgDiv").hide();
}
$("#window").css(
{
left:winPosition.l,
top:winPosition.t,
height:winPosition.h,
width:winPosition.w
}
);
}
);
})
});
$("#windowTop").mousedown(function(){
$.alerts.Drag(
document.getElementById("window"),
{
e : event,
Drag : options.IsDrag
}
);
});
},
_createObject : function(){
$("<div id=\"window\">"+
"<div id=\"windowTop\">"+
"<div id=\"windowTopContent\">Window example</div>"+
"<img src=\"images/window_min.jpg\" id=\"windowMin\" />"+
"<img src=\"images/window_max.jpg\" id=\"windowMax\" />"+
"<img src=\"images/window_close.jpg\" id=\"windowClose\" />"+
"</div>"+
"<div id=\"windowBottom\"><div id=\"windowBottomContent\"> </div></div>"+
"<div id=\"windowContent\"></div>"+
"<img src=\"images/window_resize.gif\" id=\"windowResize\" />"+
"</div>").appendTo("body");
},
_getWindowPosition : function(options){
var wPosition = $.alerts._getPosition("#window");
var windowPosition = {};
windowPosition.t = parseInt($(window).height()/6)+parseInt($(window).scrollTop());
windowPosition.l = ($(window).width()+$(window).scrollLeft())/2 - options.width/2;
windowPosition.w = options.width;
windowPosition.h = options.height;
return windowPosition;
},
_getPosition : function(o){
var top = $(o).offset().top;
var left = $(o).offset().left;
var height = $(o).height();
var width = $(o).width();
return {t:top,l:left,h:height,w:width};
},
Drag : function(obj,options){
var e = options.e || window.event;
var Drag = options.Drag;
if(Drag == false)return;
var x=parseInt(obj.style.left);
var y=parseInt(obj.style.top);
var x_=e.clientX-x;
var y_=e.clientY-y;
if(document.addEventListener){
document.addEventListener('mousemove', inFmove, true);
document.addEventListener('mouseup', inFup, true);
} else if(document.attachEvent){
document.attachEvent('onmousemove', inFmove);
document.attachEvent('onmouseup', inFup);
}
inFstop(e);
inFabort(e);
function inFmove(e){
var evt;
if(!e)e=window.event;
obj.style.left=e.clientX-x_+'px';
obj.style.top=e.clientY-y_+'px';
inFstop(e);
}
function inFup(e){
var evt;
if(!e)e=window.event;
if(document.removeEventListener){
document.removeEventListener('mousemove', inFmove, true);
document.removeEventListener('mouseup', inFup, true);
} else if(document.detachEvent){
document.detachEvent('onmousemove', inFmove);
document.detachEvent('onmouseup', inFup);
}
inFstop(e);
}
function inFstop(e){
if(e.stopPropagation) return e.stopPropagation();
else return e.cancelBubble=true;
}
function inFabort(e){
if(e.preventDefault) return e.preventDefault();
else return e.returnValue=false;
}
}
}
JAlert = function(o,json){
$.alerts.alert(o,json);
};
})(jQuery);

調用代碼
代碼如下:
$(function(){
$("a").each(function(){
$(this).bind("click",function(){
JAlert(this,{
title : '提示窗體',
content : $("#msg")[0].outerHTML,
GetType : 'string', //controls,ajax,string,iframe
IsDrag : true,
Url : "windows.html",
Data : null,
width:300,
height:200
});
});
});
});

使用說明:
    title: 窗體標題
    content:取決於GetType屬性,如果GetType='string',那麼content就是要顯示的內容,如果GetType='controls',那麼content則為要顯示的DOM對象。其它兩個類型可不用填寫。
    GetType:展現的四種類型:string,iframe,ajax,controls
    IsDrag:是否支持拖拽
    Url: 同樣取決於GetType屬性,如果GetType='ajax',那麼Url就是請求的URL地址,如果GetType='iframe',那麼URL就是iframe的鏈接地址。其它兩個類型不用填寫
    Data:當GetType='ajax'時,Data屬性為請求的數據。
    width:顯示層的寬度
    height:顯示層的高度
  HTML代碼
代碼如下:
<body>
<a href="javascript:void(0);" id="windowOpen1">Open window</a>
<a href="javascript:void(0);" id="windowOpen2">Open window</a> <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/> <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/> <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<a href="javascript:void(0);" id="windowOpen3">Open window</a>
<div id="msg" style="font-size:16px;padding-top:16px;line-height:25px;"> 歡迎訪問《<a href="http://jb51.net" target="_blank"></a>》的網站,希望與大家一起探討技術問題,共同實現各自的夢想!<br/><br/>網址:http://jb51.net</div>
</body>

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved