您可能不想仅仅为了这个目的而添加整个 jquery ui 库。如果我在这里,你会怎么做。
因此,当您“关注”该输入时 - 让我们称之为“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);
}
查询(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/