你可能不希望添加整个jQuery用户界面库只是为了这个目的。如果我在这里,你会怎么做。
所以,当你“专注”是输入 - 让称它为“opensModal” - 你想要的模式打开。这很简单,并且自我解释 - 尽管“长”代码。实际上,其中大部分只是为了使模态/模态外观更漂亮。在这里,我们去:
HTML:
<!-- the input -->
<input class="opensModal" type="text" />
<!-- the modal and its overlay -->
<div class="modalOverlay is-inactive">
<div class="modal">
<input type="checkbox" />
<input type="checkbox" />
<button>Ok</button>
<button>Cancel</button>
</div>
</div>
CSS:
.modalOverlay {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
-webkit-transition: 0.6s;
}
.modalOverlay.is-inactive {
visibility: hidden;
background: rgba(0, 0, 0, 0);
}
.modalOverlay.is-active {
visibility: visible;
background: rgba(0, 0, 0, 0.4);
}
.modal {
margin: 100px auto;
background: #fff;
width: 100px;
padding: 20px;
-webkit-transition: 0.4s 0.6s;
}
.modalOverlay.is-inactive .modal {
visibility: hidden;
opacity: 0;
-webkit-transform: scale(0.1);
}
.modalOverlay.is-active .modal {
visibility: visible;
opacity: 1;
-webkit-transform: scale(1);
}
JQUERY(JavaScript)的
(function() {
var $modal = $('.modalOverlay'),
openModal = function() {
$modal
.removeClass('is-inactive')
.addClass('is-active');
},
closeModal = function() { //use it wherever you want
$modal
.removeClass('is-active')
.addClass('is-inactive');
},
onDocReady = function() {
$('.opensModal').on('focus', openModal);
};
$(onDocReady);
})();
这里有一个小提琴:http://jsfiddle.net/2edPZ/3/
你使用jQuery UI ? – 2013-03-27 14:35:56
是的,我在这个项目中使用JQuery。 – 2013-03-27 14:42:45
[jQuery UI](http://jqueryui.com)是一个位于jQuery之上的库。不知道这是你的意思还是不在那里。它有一个模态对话框。发布你所拥有的任何代码,即使它不起作用,如果你希望人们提供帮助。 – 2013-03-27 14:50:03