일단 들어가기에 앞서 아래 문서들을 읽어 주시면 감사하겠습니다.
추천 사이트 : http://docs.jquery.com/Plugins/Authoring
번역된 사이트 : http://lazygyu.tistory.com/42
아래문서는 간단한 예부터 시작하여 작동원리등을 구현하여 보겠습니다.
<script type="text/javascript">
<!--//
jQuery.fn.debug = function() {
return this.each(function(){
alert('debug:'+this);
});
};
$(function(){
$("#sample_id").click(function(){
var msg = $(this).debug();//호출 부분
});
});
//-->
</script>
</body>
<div id="sample_id">샘플아이디</div>
</html>
$.fn.debug = function(options) { var opt = { flag1: "a", flag2: "b", flag3: "c" }; $.extend(opt, options); return this.each(function(){ alert("flag1:"+opt.flag1+",flag2:"+opt.flag2+",flag3:"+opt.flag3); }); }; $("#sample_id1").click(function(){ var msg = $(this).debug({ flag1: "d"}); });
<script type="text/javascript"> <!--// $.fn.locationcenter = function() { return this.each(function(){ var element = $(this); var win = $(window); var x = win.width(); var y = win.height(); element.css("position", "absolute"); element.css("left", win.scrollLeft() + x/2 - element.width()/2); element.css("top", win.scrollTop() + y/2 - element.height()/2); }); }; $(function(){ $("#sample_id").click(function(){ $("#popup").locationcenter(); }); }); //--> </script> //본문 <div id="sample_id">중앙에 레이어 띄우기</div> <div id="popup" class="popup">나는 팝업입니다.</div>
jquery.simplepop-1.0.0.js (function($) { $.fn.locationcenter = function(options) { var opt = { backlayer:true }; $.extend(opt, options); this.each(function(){ var element = $(this); var win = $(window); var x = win.width(); var y = win.height(); if(opt.backlayer){ $("body").append('<div id="layerBack" style="display:none"></div>'); $("#layerBack").css({ opacity: (30 / 100), filter: 'alpha(opacity=' + 30 + ')', position: 'absolute', zIndex: 1000, top: '0px', left: '0px', width: '100%', height: $(document).height(), background: "#000" }).show(); $("#layerBack").live("click", function(){ $("#layerBack").hide(); element.hide(); }); } element.css({"position":"absolute","z-index":"2000"}); element.css("left", win.scrollLeft() + x/2 - element.width()/2); element.css("top", win.scrollTop() + y/2 - element.height()/2); }); return this; }; })(jQuery);
본문내용
$(function(){
$("#popup").hide();
$("#sample_id").click(function(){
$("#popup").locationcenter();
$("#popup").toggle()
//.draggable(); //사용을 원할 경우 jquery-ui 사용
});
$("#sample_id1").click(function(){
$("#popup").locationcenter({ backlayer: false});
$("#popup").toggle()
//.draggable(); //사용을 원할 경우 jquery-ui 사용
});
$("#closepop").click(function(){
$("#layerBack").hide();
$("#popup").hide();
});
});
body 이하 내용
<div id="sample_id">중앙에 레이어 띄우기[투명배경]</div>
<div id="sample_id1">중앙에 레이어 띄우기</div>
<div id="popup" class="popup">나는 팝업입니다.<span id="closepop">[닫기]</span></div>
소스 길이는 길었졌지만 내용은 별반차이가 없습니다. if(opt.backlayer){속의 내용을 제외하고는 2.다양한 인자값 전달란과 같습니다.
단지 backlayer 옵션이 있으면 배경 투명레이어 설정을 어떻게 할 것인가의 차이 말고는 없습니다.
상기중에서 jquery에서 기본적으로 제공하는 몇개함수를 설명하고 넘어가겠습니다.
-----------------------------------------------------------------------------------
Events 함수 ?live()?: 클릭하신후 원본 보세요
HTML상의 있지도 않은 element에 대해 이벤트를 지정할 수 있다.
즉 jquery는 현재 문서상에 존재하는 것에 대해 이벤트를 지정할 수 있습니다. 그런데 현재 문서가 아니라 실시간으로 불러온 문서속의 특정 elements 를 제어하기 위해서는 어떻게 할까요?
바로 live()를 사용합니다.
일반적인 사용
$('.clickme').bind('click', function() {/*************/
});
$('.clickme').click(function(){/*************/
});
live사용
$('.clickme').live('click', function() {/*************/
});
같이 사용될 수 있는 이벤트는 ?click,?dblclick,?keydown,?keypress,?keyup,?mousedown,mousemove,?mouseout,?mouseover, and?mouseup 등이 있습니다.
-----------------------------------------------------------------------------------
Events 함수 ?toggle()?: 클릭하신후 원본 보세요
토글은 클릭시 마다 기능을 번갈아 가며 보여 줍니다.
$(div).toggle(
function () { /******* 실행1 ****/},
function () { /******* 실행2 ****/ }
);
특정 함수 하나만 있을 경우는 실행후 그 전 단계로 돌립니다.
참조로 hover를 보세요
(function($) {
$.fn.loadingbar = function(options) {
var opt = {
url:"http://mall.shop-wiz.com/images/common/loading.swf",
width:100,
height:28
};
$.extend(opt, options);
this.each(function(){
var element = $(this);
var win = $(window);
var x = win.width();
var y = win.height();
var flashobj = "";
var flashobj = flashobj + "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='"+opt.width+"' height='"+opt.height+"'>";
var flashobj = flashobj + "<param name='allowScriptAccess' value='always' /> ";
var flashobj = flashobj + "<param name='movie' value='"+opt.url+"' /> ";
var flashobj = flashobj + "<param name='quality' value='high' /> ";
var flashobj = flashobj + "<param name='wmode' value='transparent'> ";
var flashobj = flashobj + "<embed src='"+opt.url+"' quality='high' width='"+opt.width+"' height='"+opt.height+"' wmode='transparent' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />";
var flashobj = flashobj + "</object>";
$('#Loadingbarimg').remove();
element.append('<div id="Loadingbarimg" style="display:block"></div>');
$("#Loadingbarimg").html(flashobj);
$("#Loadingbarimg").css({"position":"absolute","z-index":1000, "opacity": 0.9});
$("#Loadingbarimg").css("left", win.scrollLeft() + x/2 - $("#Loadingbarimg").width()/2);
$("#Loadingbarimg").css("top", win.scrollTop() + y/2 - $("#Loadingbarimg").height()/2);
});
//return this;
};
})(jQuery);
<script type="text/javascript">
<!--//
var time_id;
$(function(){
$("#sample_id").click(function(){
$("body").loadingbar();//로딩바를 가져온다.
$.post("doloop.php",{}, function(data){
$("#Loadingbarimg").hide();//로딩바를 숨긴다
});
});
});
//-->
</script>
<div id="sample_id">서버에 전송</div>
(function($) {
$.fn.chart = function(options) {
var opt = {
height:10,
bgcolor:"blue"
};
$.extend(opt, options);
this.each(function(){
var el = $(this);
var ratio = el.attr("ratio");
width = Math.round((ratio * el.width()) / 100);
el.append('<span class="bar" style="float:left;background-color:'+opt.bgcolor+';width:0px;height:'+opt.height+'px"></span>');
el.append('<span>'+ratio+'%</span>');
var bar = $('.bar', this);
bar.animate({
'width' : width
}, 1000);
});
//return this;
};
})(jQuery);
<script type="text/javascript">
<!--//
$(function(){
$(".myGraph").chart();
});
//-->
</script>
<style type="text/css">
<!--
body{ font-size:12px;}
li{ list-style:none; }
.graphbound li { width:671px; height:20px; padding:3px 0px 0px 0px; overflow:hidden; *zoom:1; }
.graphbound li .myGraph { width:500px; height:15px; float:left; background:#CCCCCC}
-->
</style>
<body>
<ul class="graphbound">
<li>
<span class="myGraph" ratio="50"></span>
</li>
<li>
<span class="myGraph" ratio="70"></span>
</li>
<li>
<span class="myGraph" ratio="90"></span>
</li>
<li>
<span class="myGraph" ratio="80"></span>
</li>
</ul>
</body>
(function($) {
var wizgallery_show_index = 0;//현재보여지는 인덱스
var wizgallery_select_index = 0;//현재 선택된 인덱스
var wizgallery_gall_cnt = 0;//모든 이미지 갯수
var wizgallery_el;
var wizgallery_defaults;
$.fn.wizgallery = function(options) {
wizgallery_defaults = {
autoSlideInterval: 5000
};
wizgallery_el = this;
wizgallery_gall_cnt = wizgallery_el.size();//겔러리 갯수
var options = $.extend(wizgallery_defaults, options);
$(this).hover(function() {
//alert('over');
return clearInterval (autoPlay);
}, function() {
return autoPlay = setInterval( $.ch_gallery, options.autoSlideInterval);
});
return autoPlay = setInterval( $.ch_gallery, options.autoSlideInterval);
};
$.ch_gallery = function() {
wizgallery_el.addClass('wizgallery');
wizgallery_select_index = wizgallery_show_index % wizgallery_el.size();
wizgallery_el.each(function(index){
if(index == wizgallery_select_index) $(this).show();
else $(this).hide();
});
wizgallery_show_index++;
};
})(jQuery);
<script type="text/javascript">
<!--//
$(function(){
$(".rollbanner").wizgallery();
});
//-->
</script>
<body>
<ul class="post">
<li class="rollbanner" style="display:block;"><img src="http://www.shop-wiz.com/img/logo.gif" /> 내용1 </li>
<li class="rollbanner" style="display:none;"> <img src="http://wstatic.naver.com/w9/lg_naver_v3.gif" /> 내용2 </a> </li>
</ul>
</body>
</html>
많이 힘드시죠..
이제 조그만 더 하면 기초적인 플러그인은 마무리 됩니다.
힘내시기 바랍니다.
(function($) {
$.fn.formvalidate = function() {
var result = true;
var msg = "";
var input_attr = "";
$(':input', this).each(function (index) {
input_attr = $(this).attr('type');
input_tag = $(this).get(0).tagName;
if($(this).hasClass('required')){
switch(input_attr){
case "checkbox":
try {
result = $.validate.checkbox($(this));
return result;
} catch(exception) {}
break;
case "radio":
try {
//alert(input_attr);
result = $.validate.radio($(this));
return result;
} catch(exception) {}
break;
case "text":
//$.validate.text($(this));
try {
result = $.validate.text($(this));
return result;
} catch(exception) {alert('occur error')}
break;
}
if($(this).val() == "") {
msg = $(this).attr("msg");
return false;
}
}
});//$(':input', this).each(function (index) {
if(msg != "") alert(msg);
return result;
};
$.validate = {
checkbox : function($f) {
//체크박스일경우 실행
},
radio : function($f) {
//라디오일경우 실행
},
text : function($f) {
//텍스트박스일경우 실행
},
getclass : function(strClass, strClassName) {
//클라스를 가져오는 메소드
},
email : function(email) {
//이메일유효성 체크 메소드
},
jumin : function(jumin1, jumin2) {
//주민등록 유효성 체크 메소드
},
num : function(str) {
// 숫자체크
},
alpha : function(str) {
// 영문체크
},
kor : function(str) {
//한글체크
},
alphanum : function(str) {
//영숫자 체크
},
special : function(str) {
//특수문자 체크
}
};
})(jQuery);
<script type="text/javascript">
<!--//
$(function(){
$("#btn_insert").click(function(){
if($('#b_reg_form').formvalidate()){
alert('실행모드 실행');
}
});
});
//-->
</script>
<form name="b_reg_form" id="b_reg_form" action="" method="post">
<span class="button bull"><a id="btn_insert">등록</a></span>
</form>
상세 내용은 모두 제거 하였습니다.
전체적인 모습을 보는데는 상기처럼 처리하는 것이 더 휴율적인것 같아서..
11. 팝업