自作でモーダル画面つくってみた
( javascript )世の中にはごまんとそういうプラグイン的なものあるけど、自分用にごにょごにょしたいのと勉強がてら作成してみました。
用意するものとしては以下の通りです。
- jquery.js(1.9.1)
- TwitterBootStrap(1.3)
- form.css(フォーム関係一式あれば大丈夫)
- bootstrap.jp
html
テスト
Add
XStatus
Category
css
.modal_conteiner {
display: none;
position: fixed;
z-index: 1025;
left: 50%;
top: 50%;
width: 550px;
height: 350px;
margin-left: -300px;
margin-top: -150px;
padding: 0px;
background-color: white;
border: 1px solid #aaa;
box-shadow: 0px 0px 10px #333;
border-radius: 6px;
over-flow: auto;
}
.modal_conteiner .header{
border-radius: 6px 6px 0 0;
border-bottom: 1px solid #DDD;
background-color: whitesmoke;
}
.modal_conteiner .header a.right{
border-radius: 0 6px 0 0;
position: absolute;
z-index: 1;
top: 0px;
right: 0;
bottom: 0;
display: block;
width: 60px;
height: 60px;
font-size: 20px;
border-left: 1px solid rgba(34, 25, 25, 0.15);
box-shadow: inset 0 1px 2px white;
/* text-indent: -9999px; */
}
.modal_conteiner .header a:hover.right{
background-color: #ddd;
}
.modal_conteiner .header a.right div{
text-align: center;
top: 10px;
position: relative;
font-size: 30px;
}
.modal_conteiner .header h2{
height: 50px;
margin: 0;
padding: 10px 0 0 10px;
}
.modal_conteiner .body{
margin: 10px;
top: -10px;
position: relative;
padding-top: 10px
}
.modal_conteiner .body .btn-group > .btn {
height: 50px;
}
.modal_conteiner .footer{
}
#modal_overlay {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1000;
background-color: #ffffff;
opacity: 0.85;
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=85);
filter: alpha(opacity=80);
}
#modal_overlay.visible{
display: none;
}
javascript
//----------------------------------------------
// モーダル画面の表示
//----------------------------------------------
(function($) {
$.SimpleModal = function(element, options) {
var defaults = {
target: '',
continue_overlay: false,
close_callback: function() {}
}
var plugin = this;
plugin.options = $.fn.extend(defaults, options);
//---------------------------
// init
//---------------------------
plugin.init = function() {
_this = element;
return plugin;
}
//---------------------------
// close
//---------------------------
plugin.close = function() {
// init
options = plugin.options;
var callback = function(){};
if(options.continue_overlay == false) {
callback = function(){
$('#modal_overlay').fadeOut(1000, options.close_callback);
}
}
else {
callback = options.close_callback;
}
$(_this).animate({top: "+=300px", opacity: 0}, 500,
function(){
setTimeout(function(){
callback();
}, 1000);
}
)
.animate({top: "-1000px"}, 0);
return this;
}
//---------------------------
// cancel
//---------------------------
plugin.cancel = function() {
// init
options = plugin.options;
var callback = function(){
$('#modal_overlay').fadeOut(1000, options.close_callback);
}
$(_this).animate({top: "-=300px", opacity: 0}, 500, callback).animate({top: "-1000px"}, 0);;
console.log('delay');
return this;
}
//---------------------------
// open
//---------------------------
plugin.open = function() {
// init
options = plugin.options;
$(this).css('opacity', 0);
if(options.continue_overlay == false) {
$('#modal_overlay').fadeIn(300, function(){
$(_this).css({'display': 'block', top: '-100px'});
$(_this).animate({top: "50%", opacity: 1}, 500);
});
}
else {
$(_this).css({'display': 'block', top: '-100px'});
$(_this).animate({top: "50%", opacity: 1}, 500);
}
}
plugin.init();
}
$.fn.SimpleModal = function(options) {
var plugin = new $.SimpleModal(this, options);
plugin.init();
return plugin;
}
})(jQuery);
まとめ
こんな感じで完成。
イメージはこんな感じになります。

デザインはTwitterBootstrapを利用してそれっぽい感じにしてみた。
動きはPintarest風に動かして上からでたり、したに隠れたり動くようにした。
callbackはまだCloseしたときにしか処理させてないけど、徐々に追加していこう。
あと連続してモーダルを表示するときは以下のようにすると実現できた。
function next() {
// 表示させるHTML領域
$('#create_fuman').SimpleModal({
continue_overlay: true,
close_callback: function(e){
// closeしたときに新しいモーダルを表示させる
$('#complete_fuman').SimpleModal({continue_overlay: true}).open();
}
}).close();
}
プラグインあるとそれを何も意識しないで利用しちゃうけど、自分でなんだかんだいいながら作るのものまた一興でした。
