你可能不希望添加整個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