Javascript 3 个表单的动画切换
Javascript Animate Toggle for 3 forms
目前我有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;
}
在更多元素之间切换的最佳方式是:
活动元素有一个活动-class(活动)所有其他元素都隐藏 css。
当有人点击 "show form 1" 时,然后删除所有 active-classes 并将其仅添加到 form-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 />
根据我从你的问题中得到的信息,我认为你的意图是动画化多种形式之间的切换。这将悬停需要在您的 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
您可以使用这个简单的代码:
$(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>
目前我有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;
}
在更多元素之间切换的最佳方式是:
活动元素有一个活动-class(活动)所有其他元素都隐藏 css。
当有人点击 "show form 1" 时,然后删除所有 active-classes 并将其仅添加到 form-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 />
根据我从你的问题中得到的信息,我认为你的意图是动画化多种形式之间的切换。这将悬停需要在您的 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
您可以使用这个简单的代码:
$(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>