如何使用表单创建弹出窗口?

时间:2015-09-30 09:26:07

标签: javascript jquery html css

以下是我在下面的代码段中的登录表单。我需要的是通过单击菜单中的登录链接在弹出窗口中打开此表单。我通过互联网搜索,但没有找到相关的答案。我知道我应该使用JavaScript,但我不知道如何制作正确的代码。 我应该使用普通的JS还是jQuery?为什么? 谢谢。

body {
    background: #a7a7a7;
}

.login__form {
	width: 320px;
    padding: 20px;
    background: #fff;
    border-radius: 5px;
    border-top: 5px solid #ff4e50;
    margin: 0 auto;
}

.login__form fieldset{
	border: 0;
}

.login__form h3 {
    text-align: center;
    color: #666;
    font-size: 18px;
    text-transform: uppercase;
    margin-top: 0;
    margin-bottom: 20px;
	font-weight: bold;
    padding: 0;
    margin: 0;
    margin-bottom: 12px;
}

.login__form input {
    width: 100%;
    height: 42px;
    box-sizing: border-box;
    border-radius: 5px;
    border: 1px solid #ccc;
    margin-bottom: 20px;
    font-size: 14px;
    font-family: Montserrat;
    padding: 0 20px 0 50px;
    outline: none;
}

.login__form input#username {
    background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px top no-repeat;
    background-size: 16px 80px;
}

.login__form input#username:focus {
    background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px bottom no-repeat;
    background-size: 16px 80px;
}

.login__form input#password {
    background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px top no-repeat;
    background-size: 16px 80px;
}

.login__form input#password:focus {
    background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px bottom no-repeat;
    background-size: 16px 80px;
}

.login__form input:active, .login__form input:focus {
    border: 1px solid #ff4e50;
}

.login__form input#button {
    width: 100%;
    height: 40px;
    background: #ff4e50;
    box-sizing: border-box;
    border-radius: 5px;
    border: 1px solid #e15960;
    color: #fff;
    font-weight: bold;
    text-transform: uppercase;
    font-size: 14px;
    font-family: Montserrat;
    outline: none;
    cursor: pointer;
    margin: 0;
    padding: 0;
}

.login__form input#button:hover {
    opacity: 0.7;
    filter: alpha(opacity=70);
}
<form name="login" class="login__form">
			<h3>Login</h3>
			<fieldset>
				<input type="text" value="" placeholder="Username" id="username">
				<input type="password" value="" placeholder="Password" id="password">
				<input type="submit" value="Submit" id="button">
			</fieldset>
</form>

4 个答案:

答案 0 :(得分:3)

您可以使用jquery UI

&#13;
&#13;
  $(function() {
    $("#dialog").dialog({
      autoOpen: false,
      show: {
        effect: "blind",
        duration: 1000
      },
      hide: {
        effect: "explode",
        duration: 1000
      }
    });

    $("#opener").click(function() {
      $("#dialog").dialog("open");
    });
  });
&#13;
body {
  background: #a7a7a7;
}
.login__form {
  /*width: 320px;*/
  padding: 20px;
  background: #fff;
  border-radius: 5px;
  border-top: 5px solid #ff4e50;
  margin: 0 auto;
}
.login__form fieldset {
  border: 0;
}
.login__form h3 {
  text-align: center;
  color: #666;
  font-size: 18px;
  text-transform: uppercase;
  margin-top: 0;
  margin-bottom: 20px;
  font-weight: bold;
  padding: 0;
  margin: 0;
  margin-bottom: 12px;
}
.login__form input {
  width: 100%;
  height: 42px;
  box-sizing: border-box;
  border-radius: 5px;
  border: 1px solid #ccc;
  margin-bottom: 20px;
  font-size: 14px;
  font-family: Montserrat;
  padding: 0 20px 0 50px;
  outline: none;
}
.login__form input#username {
  background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px top no-repeat;
  background-size: 16px 80px;
}
.login__form input#username:focus {
  background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px bottom no-repeat;
  background-size: 16px 80px;
}
.login__form input#password {
  background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px top no-repeat;
  background-size: 16px 80px;
}
.login__form input#password:focus {
  background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px bottom no-repeat;
  background-size: 16px 80px;
}
.login__form input:active,
.login__form input:focus {
  border: 1px solid #ff4e50;
}
.login__form input#button {
  width: 100%;
  height: 40px;
  background: #ff4e50;
  box-sizing: border-box;
  border-radius: 5px;
  border: 1px solid #e15960;
  color: #fff;
  font-weight: bold;
  text-transform: uppercase;
  font-size: 14px;
  font-family: Montserrat;
  outline: none;
  cursor: pointer;
  margin: 0;
  padding: 0;
}
.login__form input#button:hover {
  opacity: 0.7;
  filter: alpha(opacity=70);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<div id="dialog">
  <form name="login" class="login__form">
    <h3>Login</h3>
    <fieldset>
      <input type="text" value="" placeholder="Username" id="username">
      <input type="password" value="" placeholder="Password" id="password">
      <input type="submit" value="Submit" id="button">
    </fieldset>
  </form>
</div>
<button id="opener">Log In</button>
&#13;
&#13;
&#13;

当然,您可以修改此代码以符合您的需求。

答案 1 :(得分:0)

您现在习惯了简单的jquery代码,就像这样

$(document).ready(function(){
    $('.login__form').hide();
    $('.click').on('click', function(){
      $('.login__form').show();
    });
  $('.closePopUP').on('click', function(){
     $('.login__form').hide();
  })
  
});
body {
    background: #a7a7a7;
}
.closePopUP{position: absolute;
right: 8px;
top: 7px;
cursor: pointer;
border: solid 2px red;
border-radius: 50%;
font-size: 14px;
font-family: arial;
padding: 2px 6px;}
.login__form {
	width: 320px;
    padding: 20px;
    background: #fff;
    border-radius: 5px;
    border-top: 5px solid #ff4e50;
    margin: 0 auto;position:relative;
}

.login__form fieldset{
	border: 0;
}

.login__form h3 {
    text-align: center;
    color: #666;
    font-size: 18px;
    text-transform: uppercase;
    margin-top: 0;
    margin-bottom: 20px;
	font-weight: bold;
    padding: 0;
    margin: 0;
    margin-bottom: 12px;
}

.login__form input {
    width: 100%;
    height: 42px;
    box-sizing: border-box;
    border-radius: 5px;
    border: 1px solid #ccc;
    margin-bottom: 20px;
    font-size: 14px;
    font-family: Montserrat;
    padding: 0 20px 0 50px;
    outline: none;
}

.login__form input#username {
    background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px top no-repeat;
    background-size: 16px 80px;
}

.login__form input#username:focus {
    background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px bottom no-repeat;
    background-size: 16px 80px;
}

.login__form input#password {
    background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px top no-repeat;
    background-size: 16px 80px;
}

.login__form input#password:focus {
    background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px bottom no-repeat;
    background-size: 16px 80px;
}

.login__form input:active, .login__form input:focus {
    border: 1px solid #ff4e50;
}

.login__form input#button {
    width: 100%;
    height: 40px;
    background: #ff4e50;
    box-sizing: border-box;
    border-radius: 5px;
    border: 1px solid #e15960;
    color: #fff;
    font-weight: bold;
    text-transform: uppercase;
    font-size: 14px;
    font-family: Montserrat;
    outline: none;
    cursor: pointer;
    margin: 0;
    padding: 0;
}

.login__form input#button:hover {
    opacity: 0.7;
    filter: alpha(opacity=70);
}
.click{float:left;cursor:pointer;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<button class="click">login popup</button>
<form name="login" class="login__form">
<span class="closePopUP">X</span>
			<h3>Login</h3>
			<fieldset>
				<input type="text" value="" placeholder="Username" id="username">
				<input type="password" value="" placeholder="Password" id="password">
				<input type="submit" value="Submit" id="button">
			</fieldset>
</form>

答案 2 :(得分:0)

Pure js解决方案。您还应该使用表单包装元素的绝对位置。

(function() {
  var loginLink = document.getElementById('login-button');
  loginLink.addEventListener('click', function() {
    var loginForm = document.getElementById('login-form');
    loginForm.style.display = 'block';
  });
})();
body {
  background: #a7a7a7;
}
.login__form {
  width: 320px;
  padding: 20px;
  background: #fff;
  border-radius: 5px;
  position: absolute;
  border-top: 5px solid #ff4e50;
  margin: 0 auto;
  left: 50%;
  margin-left: -160px;
  display: none;
}
.login__form fieldset {
  border: 0;
}
.login__form h3 {
  text-align: center;
  color: #666;
  font-size: 18px;
  text-transform: uppercase;
  margin-top: 0;
  margin-bottom: 20px;
  font-weight: bold;
  padding: 0;
  margin: 0;
  margin-bottom: 12px;
}
.login__form input {
  width: 100%;
  height: 42px;
  box-sizing: border-box;
  border-radius: 5px;
  border: 1px solid #ccc;
  margin-bottom: 20px;
  font-size: 14px;
  font-family: Montserrat;
  padding: 0 20px 0 50px;
  outline: none;
}
.login__form input#username {
  background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px top no-repeat;
  background-size: 16px 80px;
}
.login__form input#username:focus {
  background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px bottom no-repeat;
  background-size: 16px 80px;
}
.login__form input#password {
  background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px top no-repeat;
  background-size: 16px 80px;
}
.login__form input#password:focus {
  background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px bottom no-repeat;
  background-size: 16px 80px;
}
.login__form input:active,
.login__form input:focus {
  border: 1px solid #ff4e50;
}
.login__form input#button {
  width: 100%;
  height: 40px;
  background: #ff4e50;
  box-sizing: border-box;
  border-radius: 5px;
  border: 1px solid #e15960;
  color: #fff;
  font-weight: bold;
  text-transform: uppercase;
  font-size: 14px;
  font-family: Montserrat;
  outline: none;
  cursor: pointer;
  margin: 0;
  padding: 0;
}
.login__form input#button:hover {
  opacity: 0.7;
  filter: alpha(opacity=70);
}
<a href="#" id="login-button">Login</a>
<form name="login" class="login__form" id="login-form">
  <h3>Login</h3>
  <fieldset>
    <input type="text" value="" placeholder="Username" id="username">
    <input type="password" value="" placeholder="Password" id="password">
    <input type="submit" value="Submit" id="button">
  </fieldset>
</form>

答案 3 :(得分:0)

您可以使用jQuery UI Dialog

查看examples

单击按钮(#create-user)

即可触发对话框
$( "#create-user" ).button().on( "click", function() {
      dialog.dialog( "open" );
    });

您可以将html表单放入页面上的#dialog-form div(查看jQuery-UI中的其他示例) 对话框本身将使用以下代码段进行初始化:

dialog = $( "#dialog-form" ).dialog({
  autoOpen: false,
  height: 300,
  width: 350,
  modal: true,
  buttons: {
    "Create an account": addUser,
    Cancel: function() {
      dialog.dialog( "close" );
    }
  },
  close: function() {
    form[ 0 ].reset();
    allFields.removeClass( "ui-state-error" );
  }
});

注意:要使用脚本,您必须嵌入jQuery-UI-Library。有一些js / css只有弹出窗口,但我更喜欢jQuery解决方案。