>웹 프론트엔드 >JS 튜토리얼 >jQuery 팝업 레이어 플러그인 단순화된 버전 코드 download_jquery

jQuery 팝업 레이어 플러그인 단순화된 버전 코드 download_jquery

WBOY
WBOY원래의
2016-05-16 18:59:551052검색
코드 복사 코드는 다음과 같습니다.

String.prototype.replaceAll = function(s1,s2){ 
return this.replace(new RegExp(s1,"gm"),s2); 
};
(function($){ 
/* 
* $-layer 0.1 - New Wave Javascript 

* Copyright (c) 2008 King Wong 
* $Date: 2008-10-09 $
*/
var ___id___ = "";
var ___settings___ = {};
var isMouseDown = false;

var currentElement = null;

var dropCallbacks = {};
var dragCallbacks = {};

var bubblings = {};

var lastMouseX;
var lastMouseY;
var lastElemTop;
var lastElemLeft;

var dragStatus = {};

var holdingHandler = false;

$.getMousePosition = function(e){
var posx = 0;
var posy = 0;

if (!e) var e = window.event;

if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
return { 'x': posx, 'y': posy };
};
$.updatePosition = function(e) {
var pos = $.getMousePosition(e);

var spanX = (pos.x - lastMouseX);
var spanY = (pos.y - lastMouseY);
var _top = (lastElemTop + spanY) > 0 ? (lastElemTop + spanY) : 0;
var _left = (lastElemLeft + spanX) > 0 ? (lastElemLeft + spanX) : 0;
$("#"+___id___).css("top", _top);
$("#"+___id___).css("left", _left);
};

$.fn.ondrag = function(callback){
return this.each(function(){
dragCallbacks[this.id] = callback;
});
};
$.fn.ondrop = function(callback){
return this.each(function(){
dropCallbacks[this.id] = callback;
});
};

$.fn.dragOff = function(){
return this.each(function(){
dragStatus[this.id] = 'off';
});
};

$.fn.dragOn = function(){
return this.each(function(){
dragStatus[this.id] = 'on';
});
};
$.extend({
layerSettings:{
id:"layerdiv",
width:220,
height:220,
templete:'
@title@close
',
content:'',
title:'',
isbg:true,
opacity:0.3
},
layerSetup: function( settings ) {
$.extend( $.layerSettings, settings );
___settings___[settings.id] = settings;
___id___ = settings.id;
},
layershow:function(){
var __bw = $("body").width();
var __bh = $("body").height() > $(window).height() ? $("body").height() : $(window).height();
var _width = $.layerSettings.width;
var _height = $.layerSettings.height;

if(document.getElementById(___id___)) return;
var _moveid = ___id___ + "_move";
var _titleid = ___id___ + "_title";
var _contentid = ___id___ + "_content";
var _cssurl = $.layerSettings.cssurl;
var opacity = $.layerSettings.opacity;
__index = $.layermaxindex();
var __left = (__bw - _width) > 0 ? (__bw - _width)/2 : 0;
var __top = 100;
var __bgDiv = '
';
if($.layerSettings.isbg)
{
$("body").append(__bgDiv);
}
$("body").append('
');
var _templete = $.layerSettings.templete;
var __templete = _templete.replaceAll("@width@",_width).replaceAll("@height@",_height).replaceAll("@titleid@",_titleid).replaceAll("@contentid@",_contentid ).replaceAll("@title@",jQuery.layerSettings.title).replaceAll("@moveid@",_moveid);
$("#" __id___).append(__templete);
$("#" _contentid).append($.layerSettings.content);
$("#" _titleid).append($.layerSettings.title);
var idd = ___id___;
$(".layerclose").bind("click",function()
{
$.layerclose(idd);
});
$("#" __id___).bind("click",function()
{
var id = this.id;
$.layerSetup(___settings___[id]);
$(this).css("z-index",$.layermaxindex())
});
$(document).bind("click",function(e)
{
var pos = $.getMousePosition(e);

});
$(document).mousemove(function(e){
if(isMouseDown && dragStatus[currentElement.id] != 'false'){
$.updatePosition(e);
if( dragCallbacks[currentElement.id] != 정의되지 않음){
dragCallbacks[currentElement.id](e, currentElement)
}
return false;
$(document).mouseup(function(e){
if(isMouseDown && dragStatus[currentElement.id] != 'false'){
isMouseDown = false;
if(dropCallbacks[currentElement .id] != 정의되지 않음){
dropCallbacks[currentElement.id](e, currentElement)
}
return false;
(function(){
버블링[___id___] = true;

dragStatus[___id___] = "on";

//setHandler
버블링[this.id ] = true;

dragStatus[_moveid] = "핸들러"

$("#" _moveid).css("cursor", "move")

$("#" _moveid).mousedown(function(e){
var id = this.id.replace("_move","");
___id___ = id;
$("#" id).css("z-index",$.layermaxindex());
$.layerSetup(___settings___[id])
if((dragStatus[___id___] == "off") || ( dragStatus[___id___] == "handler" && !holdingHandler))
return bubblings["#" ___id___]

isMouseDown =
currentElement = $("#" ___id___); 🎜>
var pos = $.getMousePosition(e);
lastMouseX = pos.x;
lastMouseY = pos.y;

lastElemTop = document.getElementById(___id___).offsetTop;
lastElemLeft = document.getElementById(___id___).offsetLeft;

$.updatePosition(e);
holdingHandler =
}); " _moveid).mouseup(function(e){
holdingHandler = false;
});
//end setHandler
})();
},
layerclose:function(__id)
{
$("#" __id "_Background").remove();
$("#" __id).remove();
},
layermaxindex:function()
{
var ___index = 0;
$.each($("*"),function(i,n){
var __tem = $(n).css("z-index");
if(__tem>0)
{
if(__tem > ___index)
{
___index = __tem 1;
}
}
});
___인덱스를 반환합니다.
}
});
})(jQuery);



사용 방법:

(1)显示层:


复aze代码

代码如下:
function show() { $.layerSetup({ id:"abc",/ /弹출层적ID 제목:"test",//标题
내용:'test',//内容
isbg:false,//是否显示背景遮光层
불투명도:0.3 ,//背景遮光层的透명도,值越大透amdom越低,0为完全透明,1为不透明
templete:'
< ;span id="@titleid@">
'//模板
});
$.layershow();
}


(2)关闭层:




复aze代码


代码如下:
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.