目前,我有2种表格。登录表格和注册表格。 我使用Javascript Toggle在它们之间进行切换。但是现在我想添加第三个表格。我怎样才能做到这一点?因此,我希望能够从“登录”表单切换到“注册”表单或“忘记密码”表单,然后从那两个切换回“登录”表单。
在这里,您可以在视图中看到我现在是怎么做的:
(在我的局部视图中,唯一的事情是底部带有a
标签以激活.js代码的表单)
<div class="login-page">
<div class="form">
<!--Login Form-->
@{await Html.RenderPartialAsync("_LoginView"); }
<!--Register Form-->
@{await Html.RenderPartialAsync("_RegisterView"); }
</div>
</div>
还有我的.js文件:
//Toggle between login and registration form
$('.switch a').click(function () {
$('form').animate({ height: "toggle", opacity: "toggle" }, "slow");
});
.css代码:
.register-form {
display: none;
}
答案 0 :(得分:2)
根据我可以从您的问题中获得的信息,我认为您的意图是使多种形式之间的切换具有动画效果。这将需要在JavaScript中编写更多的逻辑。
基于您已经使用的切换机制,我提出了以下解决方案:
$("a[data-toggle]").on("click", function(e) {
var selector = $(this).data("toggle");
if ($(selector).css('display') != 'block') {
$('form').hide();
}
$(selector).animate({height: "toggle", opacity: "toggle"}, "slow");
});
.switch a {
width: 100px;
height: 20px;
background: grey;
text-align: center;
margin: 10px;
padding: 10px;
cursor: pointer;
}
form {
width: 100px;
height: 100px;
display: none;
}
.form1 {
background: red;
}
.form2 {
background: blue;
}
.form3 {
background: green;
}
<script src="https://code.jquery.com/jquery-3.3.1.min.js"
crossorigin="anonymous"></script>
<div class="login-page">
<div class="switch">
<a data-toggle=".form1">Form1</a>
<a data-toggle=".form2">Form2</a>
<a data-toggle=".form3">Form3</a>
</div>
<div class="form">
<form class="form1"></form>
<form class="form2"></form>
<form class="form3"></form>
</div>
</div>
这是一种高效且易于实现的方法。 您可以检查jsFiddle here
答案 1 :(得分:1)
在更多元素之间切换的最佳方法是:
active元素具有acive类(is-active),所有其他元素都被CSS隐藏。
当某人单击“显示表单1”,然后删除所有活动类并将其仅添加到表单1中。
使用css过渡对其进行动画处理。
function hideAllForms() {
$('.form').removeClass('is-active');
}
$('.js-show-form').on('click', (event) => {
const $el = $(event.currentTarget);
hideAllForms();
$('#form-' + $el.data('form')).addClass('is-active');
});
.form { display:none; background-color: red; }
.form.is-active { display: block; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<form id="form-1" class="form is-active">form1</form>
<form id="form-2" class="form" >form2</form>
<form id="form-3" class="form" >form3</form>
</div>
<a class="js-show-form" data-form="1">show form 1</a><br />
<a class="js-show-form" data-form="2">show form 2</a><br />
<a class="js-show-form" data-form="3">show form 3</a><br />
答案 2 :(得分:1)
您可以使用以下简单代码:
$(function(){
$('form').hide();
$('form').first().show();
//Toggle between login and registration form
$('a.switch').on('click', function () {
var visibleForm = $('form:visible');
$(visibleForm).hide();
if($(visibleForm).next().length > 0)
{
$(visibleForm).next().animate({height: "toggle", opacity: "toggle"}, "slow");
}
else
{
$(visibleForm).siblings('form:first').animate({height: "toggle", opacity: "toggle"}, "slow");
}
});
});
这是HTML:
<div class="login-page">
<div class="form">
<!--Login Form-->
@{await Html.RenderPartialAsync("_LoginView"); }
<!--Register Form-->
@{await Html.RenderPartialAsync("_RegisterView"); }
<!--Another Form-->
...
<!--Another Form-->
...
<!--Another Form-->
...
<!--Another Form-->
...
<!--Another Form-->
...
<!--Another Form-->
...
</div>
</div>
<a href='#' class='switch'>Switch</a>