问题描述
我设计一个网页。当我们点击DIV命名的邮件的内容,我怎么能显示包含一个标签电子邮件和文本框的弹出窗口?
I am designing a web page. When we click the content of div named mail, how can I show a popup window containing a label email and text box?
推荐答案
东西这个简单的不需要插件。这可能看起来像很多code,但它的真正pretty的简单。
Something this simple doesn't need a plugin. This might look like a lot of code but it's really pretty simple.
首先,CSS - 调整这个你喜欢的:
First the CSS - tweak this however you like:
a.selected {
background-color:#1F75CC;
color:white;
z-index:100;
}
.messagepop {
background-color:#FFFFFF;
border:1px solid #999999;
cursor:default;
display:none;
margin-top: 15px;
position:absolute;
text-align:left;
width:394px;
z-index:50;
padding: 25px 25px 20px;
}
label {
display: block;
margin-bottom: 3px;
padding-left: 15px;
text-indent: -15px;
}
.messagepop p, .messagepop.div {
border-bottom: 1px solid #EFEFEF;
margin: 8px 0;
padding-bottom: 8px;
}
和JavaScript的:
And the JavaScript:
function deselect(e) {
$('.pop').slideFadeToggle(function() {
e.removeClass('selected');
});
}
$(function() {
$('#contact').on('click', function() {
if($(this).hasClass('selected')) {
deselect($(this));
} else {
$(this).addClass('selected');
$('.pop').slideFadeToggle();
}
return false;
});
$('.close').on('click', function() {
deselect($('#contact'));
return false;
});
});
$.fn.slideFadeToggle = function(easing, callback) {
return this.animate({ opacity: 'toggle', height: 'toggle' }, 'fast', easing, callback);
};
和最终的HTML:
<div class="messagepop pop">
<form method="post" id="new_message" action="/messages">
<p><label for="email">Your email or name</label><input type="text" size="30" name="email" id="email" /></p>
<p><label for="body">Message</label><textarea rows="6" name="body" id="body" cols="35"></textarea></p>
<p><input type="submit" value="Send Message" name="commit" id="message_submit"/> or <a class="close" href="/">Cancel</a></p>
</form>
</div>
<a href="/contact" id="contact">Contact Us</a>
根据您可能想通过Ajax调用加载弹出内容的情况。这是最好的避免这种可能的话,因为它可能会看到内容之前给予用户更加显著的延迟。在这里,夫妇,你要改变,如果你采用这种方法进行。
Depending on the situation you may want to load the popup content via an ajax call. It's best to avoid this if possible as it may give the user a more significant delay before seeing the content. Here couple changes that you'll want to make if you take this approach.
HTML变成了:
<div>
<div class="messagepop pop"></div>
<a href="/contact" id="contact">Contact Us</a>
</div>
和JavaScript的总体思路变为:
And the general idea of the JavaScript becomes:
$("#contact").on('click', function() {
if($(this).hasClass("selected")) {
deselect();
} else {
$(this).addClass("selected");
$.get(this.href, function(data) {
$(".pop").html(data).slideFadeToggle(function() {
$("input[type=text]:first").focus();
});
}
}
return false;
});
这篇关于如何生成使用jQuery一个简单的弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!