与显示不一致
Slidetoggle inconsistencies with display
我制作了一个简单的系统,可以在切换面板中显示信息。我的问题是滑动切换"有时"将面板的显示错误地切换为块,有时切换为正确的css集内联块。
我看到的大多数答案都说,只需在滑动切换后设置显示类型,但这会导致动画中期出现不稳定的外观变化。
$(document).ready(function() {
// hide all div containers
$('#collapsible-panels div.yanswer').hide();
// append click event to the a element
$('#collapsible-panels a').click(function(e) {
// find class of clicked object, first class only because it's the one we initially set
var cN = $(this).attr('class').split(' ')[0];
// if there is a tab with our corresponding class active, we close it and toggle active off of it.
if ($(this).nextAll('#collapsible-panels div.' + cN + '.active').length != 0) {
$(this).nextAll('#collapsible-panels div.' + cN).first().slideToggle();
$(this).nextAll('#collapsible-panels div.' + cN).first().toggleClass('active');
$(this).toggleClass('tabbed');
e.preventDefault();
}
//if there is no tab open, find object with the corresponding class and open it and toggle as active.
else if ($(this).nextAll('#collapsible-panels div.active').length == 0) {
$(this).nextAll('#collapsible-panels div.' + cN).first().slideToggle();
$(this).nextAll('#collapsible-panels div.' + cN).first().toggleClass('active');
$(this).toggleClass('tabbed');
e.preventDefault();
}
//else, basically if a tab is open that is not our chosen tab, find the tab we want to open, close all others, set to inactive then open/activate the selected tab.
else {
$(this).nextAll('#collapsible-panels div.' + cN).first().prevAll('#collapsible-panels div.active').slideToggle();
$(this).nextAll('#collapsible-panels div.' + cN).first().prevAll('#collapsible-panels div.active').toggleClass('active');
$(this).nextAll('#collapsible-panels div.' + cN).first().nextAll('#collapsible-panels div.active').slideToggle();
$(this).nextAll('#collapsible-panels div.' + cN).first().nextAll('#collapsible-panels div.active').toggleClass('active');
$(this).nextAll('#collapsible-panels div.' + cN).first().delay(500).slideToggle();
$(this).nextAll('#collapsible-panels div.' + cN).first().toggleClass('active');
$(this).toggleClass('tabbed');
$(this).nextAll('#collapsible-panels a.tabbed').toggleClass('tabbed');
$(this).prevAll('#collapsible-panels a.tabbed').toggleClass('tabbed');
e.preventDefault();
}
});
});
.tabs {
margin: 4%;
text-align: center;
}
.tabs a {
white-space: nowrap;
vertical-align: middle;
display: inline-block;
text-decoration: none;
padding: 15px 25px;
margin: 0px;
background: #CE1F24;
color: #FFF;
border-radius: 15px;
border: solid 1px #C00000;
text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.4);
box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.6);
}
.tabs a:hover {
background: #B80000;
border: solid 1px #880000;
text-decoration: none;
border-bottom-width: 0px;
}
.tabs a:focus {
outline: none;
}
.tabs a.tabbed {
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 1px 1px rgba(0, 0, 0, 0.2);
background: #587CAF;
border: solid 1px #587CAF;
border-bottom-width: 0px;
}
.tabs a:active {
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 1px 1px rgba(0, 0, 0, 0.2);
background: #587CAF;
border: solid 1px #587CAF;
border-bottom-width: 0px;
}
.tabs div.yanswer {
vertical-align: middle;
display: inline-block;
padding: 20px 60px;
margin-top: 5px;
background: #FFF;
color: #000;
border-radius: 40px;
border: solid 4px #587CAF;
z-index: 10;
}
.tabs div.yanswer p {
font-size: 3em;
width: auto;
}
.tabs div.yanswer ul {
font-size: 1.3em;
}
.tabs div.yanswer p.smallfont {
font-size: 1em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="collapsible-panels" class="tabs">
<a class="ylead" href="#">Why Should I Lead?</a>
<a class="yhelp" href="#">Why Should I Help?</a>
<br>
<div class="ylead yanswer">
<p>If you lead a project you can...</p>
<br>
<ul>
<li>Show management what you can do.</li>
<li>Make decisions.</li>
<li>Add to your resume.</li>
<li>*Earn money or time off.*</li>
</ul>
<br>
<p class="smallfont">*eligible for Tasks with 6+ week turnarounds</p>
</div>
<div class="yhelp yanswer">
<p>If you help with a project you can...</p>
<br>
<ul>
<li>Show management what you can do.</li>
<li>Learn something new.</li>
<li>Help make decisions.</li>
<li>Add to your resume.</li>
</ul>
</div>
</div>
在chrome中打开开发人员控制台的情况下,显示始终保持为内联块,否则通常显示为块。
我找到了自己问题的答案。有了破解修复程序,即在切换后将显示设置为内联块,我注意到它只需要应用一次,之后slideLogge()将正确地在display:none和display:inline块之间切换。
我认为问题是,在css将面板设置为内联块之前,我的.hide()将面板显示设置为none,因此slideToggle()不知道要使用什么默认显示,而是恢复为块。我通过添加$('#collapsulatablepanelsdiv.yanswer').css('display','inlineblock')解决了这个问题;在我的.hide()之前。这确保slideToggle()知道它们应该是内联块。
相关文章:
- 谷歌地图标记不会显示
- 当属性不一致时,如何根据属性对JS对象列表进行排序
- 标记不会显示在谷歌地图上
- hasOwnProperty-即使有属性,也不会显示属性
- 当我使用高度百分比时,谷歌地图javascript api不会显示
- 为什么缩放按钮不会显示在照片擦除中
- 突出显示列表的父项,但不突出显示所有子项
- Firefox赢得了'不会显示我添加的jquery元素,但Chrome会显示
- Android和JavaScript解析API之间不一致
- Javascript的行为与PHP包含文件不一致
- 使用Phonegap Build后,谷歌地图将不会显示在手机上
- 与显示不一致
- localeCompare 显示使用前导变音字符对单词进行排序时不一致的行为
- 与突出显示来自不同网站的文本不一致
- Firefox 显示的行为与视频“进度”事件不一致
- AJAX导致不一致地显示正确的内容
- Google CustomSearchControl显示不一致的分页链接
- 显示/隐藏在 Jquery 图像映射上不一致的工作
- 显示不一致行为的复选框的Onclick事件处理程序
- Unicode在JS和HTML中转义时显示不一致