英文:
css animation only works once
问题
我有这段代码。我希望切换时每次打开和关闭都有一个 easeOut 动画,但只有在第一次打开时才有效。
$('.toggle-content:not(.opened), .content-tab:not(.opened)').hide();
$('.tab-index a').click(function () {
$(this).parent().next().slideToggle(300, 'easeOutExpo', function () {
if ($(this).is(':visible'))
$(this).css('display', 'table-row');
else
$(this).css('display', 'hidden')
});
$(this).parent().toggleClass('tab-opened tab-closed');
$(this).children('i').toggleClass('icon-plus-sign icon-minus-sign');
$(this).attr('text', ($(this).attr('text') == 'Close') ? 'Open' : 'Close');
return false;
});
这是我想要使用它的示例:
<div class="toggle">
<p class="tab-index tab-closed">
<a href="#" title="Close"><i class="icon-plus-sign"></i> 文本</a></p>
<div class="content-tab closed">
<p>文本</p>
</div>
</div>
我希望每次打开或关闭切换时都有动画效果,但我对 CSS 不太了解,不知道具体是哪里出了问题。提前感谢你的帮助!
英文:
I have this code. I want the toggle to have an easeOut animation every time it opens and closes, but it only works when opening it the first time.
$('.toggle-content:not(.opened), .content-tab:not(.opened)').hide();
$('.tab-index a').click(function () {
$(this).parent().next().slideToggle(300, 'easeOutExpo', function () {
if ($(this).is(':visible'))
$(this).css('display', 'table-row');
else
$(this).css('display', 'hidden')
});
$(this).parent().toggleClass('tab-opened tab-closed');
$(this).children('i').toggleClass('icon-plus-sign icon-minus-sign');
$(this).attr('text', ($(this).attr('text') == 'Close') ? 'Open' : 'Close');
return false;
});
Here is an example of where I want to use it:
<div class="toggle">
<p class="tab-index tab-closed">
<a href="#" title="Close"><i class="icon-plus-sign"></i> text</a></p>
<div class="content-tab closed">
<p>text</p>
</div>
</div>
I expected it to have the animation every time I opened or closed the toggle, but I am new to CSS and I don't know what exactly isn't working. Thank you in advance for helping me out!
答案1
得分: 0
如果我更改代码以否定visible
属性,动画会正常工作,但content-tab
中的文本会溢出。
以下是新代码:
$('.toggle-content:not(.opened), .content-tab:not(.visible)').hide();
$('.tab-index a').click(function () {
$(this).parent().next().slideToggle(300, 'easeOutExpo', function () {
if ($(this).is(':visible'))
$(this).css('display', 'table-row');
else
$(this).css('display', 'hidden');
});
$(this).parent().toggleClass('tab-opened tab-closed');
$(this).children('i').toggleClass('icon-plus-sign icon-minus-sign');
$(this).attr('text', ($(this).attr('text') == 'Close') ? 'Open' : 'Close');
return false;
});
和.content-tab
类:
.toggle .content-tab {
border-left: 1px solid #CFCFCF;
margin: 0 0 20px 10px;
overflow-y: auto;
}
HTML代码保持与我原始问题中相同。
英文:
If I change the code to negate the visible property, the animation works, but the text in the content-tab overflows.
Here's the new code:
$('.toggle-content:not(.opened), content-tab:not(.visible)').hide();
$('.tab-index a').click(function () {
$(this).parent().next().slideToggle(300, 'easeOutExpo', function () {
if ($(this).is(!':visible'))
$(this).css('display', 'table-row');
else
$(this).css('display', 'hidden')
});
$(this).parent().toggleClass('tab-opened tab-closed');
$(this).children('i').toggleClass('icon-plus-sign icon-minus-sign');
$(this).attr('text', ($(this).attr('text') == 'Close') ? 'Open' : 'Close');
return false;
});
and the .content-tab class
.toggle .content-tab {
border-left: 1px solid #CFCFCF;
margin: 0 0 20px 10px;
overflow-y:auto;
}
The HTML code remains like my original question
答案2
得分: 0
你在你的slideToggle
函数中添加了多个参数,移除duration
(300)或easing
,它就会工作。这里是一个例子
$('.toggle-content:not(.opened), .content-tab:not(.opened)').hide();
$('.tab-index a').click(function () {
$(this).parent().next().slideToggle('easeOutExpo', function () {
if ($(this).is(':visible'))
$(this).css('display', 'table-row');
else
$(this).css('display', 'hidden')
});
$(this).parent().toggleClass('tab-opened tab-closed');
$(this).children('i').toggleClass('icon-plus-sign icon-minus-sign');
$(this).attr('text', ($(this).attr('text') == 'Close') ? 'Open' : 'Close');
return false;
});
简单的方法:
也许你故意使用了其他检查和指令,但如果不是的话,这里有一个简单的方法,只需使用slideToggle
函数,它会处理显示项目,而不需要其他检查:
$('.toggle-content:not(.opened), .content-tab:not(.opened)').hide();
$('.tab-index a').click(function () {
$(this).parent().next().slideToggle('easeOutExpo');
});
英文:
You added multiple parameters in your slideToggle
function, Remove the duration
(300) or the easing
, And it will work, Here is an example
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$('.toggle-content:not(.opened), .content-tab:not(.opened)').hide();
$('.tab-index a').click(function () {
$(this).parent().next().slideToggle('easeOutExpo', function () {
if ($(this).is(':visible'))
$(this).css('display', 'table-row');
else
$(this).css('display', 'hidden')
});
$(this).parent().toggleClass('tab-opened tab-closed');
$(this).children('i').toggleClass('icon-plus-sign icon-minus-sign');
$(this).attr('text', ($(this).attr('text') == 'Close') ? 'Open' : 'Close');
return false;
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="toggle">
<p class="tab-index tab-closed">
<a href="#" title="Close"><i class="icon-plus-sign"></i> text 1</a>
</p>
<div class="content-tab closed">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eu diam non diam sagittis accumsan. Duis at quam semper, lobortis justo in, accumsan odio. Cras non nisl a libero tempus luctus a ac urna. Aenean egestas tellus posuere sapien mattis congue. Interdum et malesuada fames ac ante ipsum primis in faucibus. Praesent faucibus est urna, consectetur mattis tortor efficitur ac. Morbi malesuada ligula fermentum, bibendum felis dapibus, faucibus odio.</p>
</div>
<p class="tab-index tab-closed">
<a href="#" title="Close"><i class="icon-plus-sign"></i> text 2</a>
</p>
<div class="content-tab closed">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eu diam non diam sagittis accumsan. Duis at quam semper, lobortis justo in, accumsan odio. Cras non nisl a libero tempus luctus a ac urna. Aenean egestas tellus posuere sapien mattis congue. Interdum et malesuada fames ac ante ipsum primis in faucibus. Praesent faucibus est urna, consectetur mattis tortor efficitur ac. Morbi malesuada ligula fermentum, bibendum felis dapibus, faucibus odio.</p>
</div>
<p class="tab-index tab-closed">
<a href="#" title="Close"><i class="icon-plus-sign"></i> text 3</a>
</p>
<div class="content-tab closed">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eu diam non diam sagittis accumsan. Duis at quam semper, lobortis justo in, accumsan odio. Cras non nisl a libero tempus luctus a ac urna. Aenean egestas tellus posuere sapien mattis congue. Interdum et malesuada fames ac ante ipsum primis in faucibus. Praesent faucibus est urna, consectetur mattis tortor efficitur ac. Morbi malesuada ligula fermentum, bibendum felis dapibus, faucibus odio.</p>
</div>
</div>
<!-- end snippet -->
Simple way:
Maybe you are using the other checks and instructions by purpose, but if not, here is an easy way, just use the slideToggle
function, it will handle displaying the items for you without the other checks:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$('.toggle-content:not(.opened), .content-tab:not(.opened)').hide();
$('.tab-index a').click(function () {
$(this).parent().next().slideToggle('easeOutExpo');
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="toggle">
<p class="tab-index tab-closed">
<a href="#" title="Close"><i class="icon-plus-sign"></i> text 1</a>
</p>
<div class="content-tab closed">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eu diam non diam sagittis accumsan. Duis at quam semper, lobortis justo in, accumsan odio. Cras non nisl a libero tempus luctus a ac urna. Aenean egestas tellus posuere sapien mattis congue. Interdum et malesuada fames ac ante ipsum primis in faucibus. Praesent faucibus est urna, consectetur mattis tortor efficitur ac. Morbi malesuada ligula fermentum, bibendum felis dapibus, faucibus odio.</p>
</div>
<p class="tab-index tab-closed">
<a href="#" title="Close"><i class="icon-plus-sign"></i> text 2</a>
</p>
<div class="content-tab closed">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eu diam non diam sagittis accumsan. Duis at quam semper, lobortis justo in, accumsan odio. Cras non nisl a libero tempus luctus a ac urna. Aenean egestas tellus posuere sapien mattis congue. Interdum et malesuada fames ac ante ipsum primis in faucibus. Praesent faucibus est urna, consectetur mattis tortor efficitur ac. Morbi malesuada ligula fermentum, bibendum felis dapibus, faucibus odio.</p>
</div>
<p class="tab-index tab-closed">
<a href="#" title="Close"><i class="icon-plus-sign"></i> text 3</a>
</p>
<div class="content-tab closed">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eu diam non diam sagittis accumsan. Duis at quam semper, lobortis justo in, accumsan odio. Cras non nisl a libero tempus luctus a ac urna. Aenean egestas tellus posuere sapien mattis congue. Interdum et malesuada fames ac ante ipsum primis in faucibus. Praesent faucibus est urna, consectetur mattis tortor efficitur ac. Morbi malesuada ligula fermentum, bibendum felis dapibus, faucibus odio.</p>
</div>
</div>
<!-- end snippet -->
答案3
得分: -1
<!-- 开始代码片段: js 隐藏: false 控制台: true babel: false -->
<!-- 语言: lang-js -->
document.addEventListener('DOMContentLoaded', () => {
const accordions = document.querySelectorAll('.accordion');
accordions.forEach(accordion => {
const toggles = accordion.querySelectorAll('.accordion-toggle');
toggles.forEach(toggle => {
toggle.addEventListener('click', () => {
const activeToggle = accordion.querySelector('.active');
if (activeToggle && activeToggle !== toggle) {
activeToggle.classList.remove('active');
activeToggle.nextElementSibling.style.maxHeight = 0;
}
toggle.classList.toggle('active');
const content = toggle.nextElementSibling;
if (toggle.classList.contains('active')) {
content.style.maxHeight = content.scrollHeight + 'px';
} else {
content.style.maxHeight = 0;
}
});
});
});
});
<!-- 语言: lang-css -->
.accordion-toggle {
cursor: pointer;
padding: 10px;
background-color: #eee;
border: none;
text-align: left;
outline: none;
font-size: 18px;
transition: background-color 0.3s ease-out;
}
.accordion-toggle:hover {
background-color: #ddd;
}
.accordion-content {
padding: 0 10px 10px 10px;
background-color: #f9f9f9;
overflow: hidden;
transition: max-height 0.3s ease-out;
max-height: 0;
}
.active {
background-color: #ddd;
}
.active + .accordion-content {
max-height: 500px;
transition: max-height 0.3s ease-out;
}
.padding {
padding: 30px 0;
}
<!-- 语言: lang-html -->
<div class="accordion">
<div class="accordion-toggle">Section 1</div>
<div class="accordion-content">
<div class="padding">Section 1的内容在这里。</div>
</div>
<div class="accordion-toggle">Section 2</div>
<div class="accordion-content">
<div class="padding">Section 2的内容在这里。</div>
</div>
<div class="accordion-toggle">Section 3</div>
<div class="accordion-content">
<div class="padding">Section 3的内容在这里。</div>
</div>
</div>
<!-- 结束代码片段 -->
LE: **在这里查看工作代码** 享受
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.addEventListener('DOMContentLoaded', () => {
const accordions = document.querySelectorAll('.accordion');
accordions.forEach(accordion => {
const toggles = accordion.querySelectorAll('.accordion-toggle');
toggles.forEach(toggle => {
toggle.addEventListener('click', () => {
const activeToggle = accordion.querySelector('.active');
if (activeToggle && activeToggle !== toggle) {
activeToggle.classList.remove('active');
activeToggle.nextElementSibling.style.maxHeight = 0;
}
toggle.classList.toggle('active');
const content = toggle.nextElementSibling;
if (toggle.classList.contains('active')) {
content.style.maxHeight = content.scrollHeight + 'px';
} else {
content.style.maxHeight = 0;
}
});
});
});
});
<!-- language: lang-css -->
.accordion-toggle {
cursor: pointer;
padding: 10px;
background-color: #eee;
border: none;
text-align: left;
outline: none;
font-size: 18px;
transition: background-color 0.3s ease-out;
}
.accordion-toggle:hover {
background-color: #ddd;
}
.accordion-content {
padding: 0 10px 10px 10px;
background-color: #f9f9f9;
overflow: hidden;
transition: max-height 0.3s ease-out;
max-height: 0;
}
.active {
background-color: #ddd;
}
.active + .accordion-content {
max-height: 500px;
transition: max-height 0.3s ease-out;
}
.padding {
padding: 30px 0;
}
<!-- language: lang-html -->
<div class="accordion">
<div class="accordion-toggle">Section 1</div>
<div class="accordion-content">
<div class="padding">Content for section 1 goes here.</div>
</div>
<div class="accordion-toggle">Section 2</div>
<div class="accordion-content">
<div class="padding">Content for section 2 goes here.</div>
</div>
<div class="accordion-toggle">Section 3</div>
<div class="accordion-content">
<div class="padding">Content for section 3 goes here.</div>
</div>
</div>
<!-- end snippet -->
LE: Working code here Enjoy
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论