jQuery彈出層插件簡(jiǎn)化版代碼第1/2頁(yè)
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:'<div style="height:20px; width:@width@px; background-color:#777777;"><span id="@moveid@" style="position:relative; left:0px; top:0px; height:20px; width:100px;"><span id="@titleid@">@title@</span></span><span class="layerclose" style="position:relative; top:0px; float:right; right:20px; color:red;">close</span></div><div style="border:solid #ff0000 1px; width:@width@px; height:@height@px;"><div style="width:100%; height:100%; background-color:#ffffff;" id="@contentid@"></div></div>',
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 = '<div id="'+___id___+'_background" style="background:#000000; filter:alpha(opacity='+(opacity*100)+'); opacity: '+opacity+'; width:'+__bw+'px; height:'+__bh+'px; z-index:'+(__index++)+'; position:absolute; left:0px; top:0px;"></div>';
if($.layerSettings.isbg)
{
$("body").append(__bgDiv);
}
$("body").append('<div id="'+___id___+'" style="z-index:'+__index+';position:absolute; left:'+__left+'px; top:'+__top+'px;"></div>');
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] != undefined){
dragCallbacks[currentElement.id](e, currentElement);
}
return false;
}
});
$(document).mouseup(function(e){
if(isMouseDown && dragStatus[currentElement.id] != 'false'){
isMouseDown = false;
if(dropCallbacks[currentElement.id] != undefined){
dropCallbacks[currentElement.id](e, currentElement);
}
return false;
}
});
(function(){
bubblings[___id___] = true;
dragStatus[___id___] = "on";
//setHandler
bubblings[this.id] = true;
dragStatus[_moveid] = "handler";
$("#"+_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 = true;
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 = true;
});
$("#"+_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;
}
}
});
return ___index;
}
});
})(jQuery);
相關(guān)文章
jQuery對(duì)html元素的取值與賦值實(shí)例詳解
這篇文章主要介紹了jQuery對(duì)html元素的取值與賦值,較為詳細(xì)的分析了jQuery針對(duì)常見(jiàn)html元素的獲取與賦值技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-12-12基于jquery實(shí)現(xiàn)select選擇框內(nèi)容左右移動(dòng)添加刪除代碼分享
這篇文章主要介紹了基于jquery實(shí)現(xiàn)select選擇框內(nèi)容左右移動(dòng)添加刪除功能,推薦給大家,有需要的小伙伴可以參考下。2015-08-08jQuery Dialog 打開(kāi)時(shí)自動(dòng)聚焦的解決方法(兩種方法)
這篇文章主要介紹了jQuery Dialog 打開(kāi)時(shí)自動(dòng)聚焦的解決方法,及jquery dialog打開(kāi)時(shí),自動(dòng)聚焦在第一個(gè)控件上的方法,對(duì)jquery dialog相關(guān)知識(shí)感興趣的朋友通過(guò)本文一起學(xué)習(xí)吧2016-11-11jQuery hover事件簡(jiǎn)單實(shí)現(xiàn)同時(shí)綁定2個(gè)方法
這篇文章主要介紹了jQuery hover事件簡(jiǎn)單實(shí)現(xiàn)同時(shí)綁定2個(gè)方法,可實(shí)現(xiàn)同時(shí)綁定懸停與離開(kāi)事件的功能,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2016-06-06jQuery實(shí)現(xiàn)簡(jiǎn)單的下拉菜單導(dǎo)航功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)簡(jiǎn)單的下拉菜單導(dǎo)航功能,涉及jQuery針對(duì)頁(yè)面元素的遍歷與節(jié)點(diǎn)修改相關(guān)操作技巧,需要的朋友可以參考下2017-12-12jquery+easeing實(shí)現(xiàn)仿flash的載入動(dòng)畫(huà)
本文主要給大家講述的是如何使用jquery+easeing實(shí)現(xiàn)仿flash的載入動(dòng)畫(huà)的方法,附上示例代碼,非常細(xì)致全面,這里推薦給大家,希望對(duì)大家熟練使用jQuery有所幫助。2015-03-03jquery實(shí)現(xiàn)的判斷倒計(jì)時(shí)是否結(jié)束代碼
在一些購(gòu)物網(wǎng)站經(jīng)常會(huì)遇到倒計(jì)時(shí)的功能,例如某些商品在一定期限內(nèi)搞活動(dòng),下面小編給大家分享一段代碼關(guān)于jquery實(shí)現(xiàn)的判斷倒計(jì)時(shí)是否結(jié)束代碼,希望對(duì)大家有所幫助2016-02-02