[英]Default .toggle() easing animation in jQuery not working
I am trying to hide sidebars by using toggle() jquery function. 我试图通过使用toggle()jQuery函数隐藏侧边栏。
Code: 码:
$('#hide_saved_templates').click(function(){
$('#saved_templates').toggle('slow');
var jobWrapper = $('#job_form_wrapper');
if(jobWrapper.hasClass('col-md-7'))
{
jobWrapper.removeClass('col-md-7');
$('#hide_saved_templates').removeClass('glyphicon-backward');
$('#hide_saved_templates').addClass('glyphicon-forward');
jobWrapper.addClass('col-md-9');
}
else if(jobWrapper.hasClass('col-md-10'))
{
jobWrapper.removeClass('col-md-10');
$('#hide_saved_templates').removeClass('glyphicon-backward');
$('#hide_saved_templates').addClass('glyphicon-forward');
jobWrapper.addClass('col-md-12');
}
else if(jobWrapper.hasClass('col-md-12')){
jobWrapper.removeClass('col-md-12');
$('#hide_saved_templates').removeClass('glyphicon-forward');
$('#hide_saved_templates').addClass('glyphicon-backward');
jobWrapper.addClass('col-md-10');
}
else {
jobWrapper.removeClass('col-md-9');
$('#hide_saved_templates').removeClass('glyphicon-forward');
$('#hide_saved_templates').addClass('glyphicon-backward');
jobWrapper.addClass('col-md-7');
}
});
$('#hide_job_history').click(function(){
$('#jobs_history').toggle('slow');
var jobWrapper = $('#job_form_wrapper');
if(jobWrapper.hasClass('col-md-7'))
{
jobWrapper.removeClass('col-md-7');
$('#hide_job_history').removeClass('glyphicon-forward');
$('#hide_job_history').addClass('glyphicon-backward');
jobWrapper.addClass('col-md-10');
}
else if(jobWrapper.hasClass('col-md-10'))
{
jobWrapper.removeClass('col-md-10');
$('#hide_job_history').removeClass('glyphicon-backward');
$('#hide_job_history').addClass('glyphicon-forward');
jobWrapper.addClass('col-md-7');
}
else if(jobWrapper.hasClass('col-md-9')){
jobWrapper.removeClass('col-md-9');
$('#hide_job_history').removeClass('glyphicon-forward');
$('#hide_job_history').addClass('glyphicon-backward');
jobWrapper.addClass('col-md-12');
}
else if(jobWrapper.hasClass('col-md-12')){
jobWrapper.removeClass('col-md-12');
$('#hide_job_history').removeClass('glyphicon-backward');
$('#hide_job_history').addClass('glyphicon-forward');
jobWrapper.addClass('col-md-9');
}
});
The toggle is working fine and toggling the sidebars as expected. 切换功能正常,并按预期切换侧栏。 However, the default easing animation of toggle('slow') is working perfectly for one sidebar
$('#saved_templates').toggle('slow')
and not working for the other $('#jobs_history').toggle('slow');
但是,toggle('slow')的默认缓动动画对于一个侧边栏
$('#saved_templates').toggle('slow')
toggle('slow')来说是完美的,而对另外一个$('#jobs_history').toggle('slow');
. 。 I have used the same code for both sidebars but I didn't able to understand the reason for this inconsistency.
我为两个侧边栏使用了相同的代码,但是我无法理解这种不一致的原因。 I want to apply smooth simple transition to the toggle effect.
我想将平滑的简单过渡应用于切换效果。
HTML Structure: HTML结构:
<div class="row">
<div class="col-md-2 no-padding show-templates-outer-wrapper" id="saved_templates" style="display: none;"></div>
<div class="no-padding col-md-12" id="job_form_wrapper">
<span id="hide_saved_templates" class="glyphicon glyphicon-forward" style="cursor: pointer;"></span>
<span id="hide_job_history" style="position: absolute; right: 5px; cursor: pointer;" class="glyphicon glyphicon-forward"></span>
</div>
<div class="col-md-3 no-padding show-jobs-history-outer-wrapper" id="jobs_history" style="display: none;"></div>
</div>
The problem is toggle
and changing class occurs same time. 问题是
toggle
和更改类同时发生。 Since jobs_history
div
is in the last position it's toggle
event is not affecting others. 由于
jobs_history
div
位于最后位置,因此toggle
事件不会影响其他事件。 Following code snippet may help you. 以下代码段可能会对您有所帮助。
var saved_templates = $('#saved_templates'); var jobs_history = $('#jobs_history'); $('#hide_saved_templates').click(function() { if (saved_templates.is(':visible')) { saved_templates.hide('slow', function() { jobs_history.show(); }); } else { jobs_history.hide(); saved_templates.show('slow'); } $(this).toggleClass('glyphicon-backward glyphicon-forward'); }); $('#hide_job_history').click(function() { if (jobs_history.is(':visible')) { jobs_history.hide(); saved_templates.show('slow'); } else { saved_templates.hide('slow', function() { jobs_history.show(); }); } $(this).toggleClass('glyphicon-backward glyphicon-forward'); });
#saved_templates, #jobs_history, #job_form_wrapper { border: 1px solid #000; height: 20px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <div class="row"> <div class="col-md-2 no-padding show-templates-outer-wrapper" id="saved_templates" style="display: none;"></div> <div class="no-padding col-md-10" id="job_form_wrapper"> <span id="hide_saved_templates" class="glyphicon glyphicon-forward" style="cursor: pointer;"></span> <span id="hide_job_history" style="position: absolute; right: 5px; cursor: pointer;" class="glyphicon glyphicon-forward"></span> </div> <div class="col-md-2 no-padding show-jobs-history-outer-wrapper" id="jobs_history" style="display: none;"></div> </div>
See the result in full page. 全页查看结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.