CSS动画只能执行一次。

huangapple go评论75阅读模式
英文:

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.

Image example here

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 -->

$(&#39;.toggle-content:not(.opened), .content-tab:not(.opened)&#39;).hide();
$(&#39;.tab-index a&#39;).click(function () {
    $(this).parent().next().slideToggle(&#39;easeOutExpo&#39;, function () {
        if ($(this).is(&#39;:visible&#39;))
            $(this).css(&#39;display&#39;, &#39;table-row&#39;);
        else
            $(this).css(&#39;display&#39;, &#39;hidden&#39;)
    });
    $(this).parent().toggleClass(&#39;tab-opened tab-closed&#39;);
    $(this).children(&#39;i&#39;).toggleClass(&#39;icon-plus-sign icon-minus-sign&#39;);
    $(this).attr(&#39;text&#39;, ($(this).attr(&#39;text&#39;) == &#39;Close&#39;) ? &#39;Open&#39; : &#39;Close&#39;);
    return false;
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;toggle&quot;&gt;
    &lt;p class=&quot;tab-index tab-closed&quot;&gt;
        &lt;a href=&quot;#&quot; title=&quot;Close&quot;&gt;&lt;i class=&quot;icon-plus-sign&quot;&gt;&lt;/i&gt; text 1&lt;/a&gt;
    &lt;/p&gt;
    &lt;div class=&quot;content-tab closed&quot;&gt;
        &lt;p&gt;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.&lt;/p&gt;
    &lt;/div&gt;
    
    &lt;p class=&quot;tab-index tab-closed&quot;&gt;
        &lt;a href=&quot;#&quot; title=&quot;Close&quot;&gt;&lt;i class=&quot;icon-plus-sign&quot;&gt;&lt;/i&gt; text 2&lt;/a&gt;
    &lt;/p&gt;
    &lt;div class=&quot;content-tab closed&quot;&gt;
        &lt;p&gt;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.&lt;/p&gt;
    &lt;/div&gt;
    
    &lt;p class=&quot;tab-index tab-closed&quot;&gt;
        &lt;a href=&quot;#&quot; title=&quot;Close&quot;&gt;&lt;i class=&quot;icon-plus-sign&quot;&gt;&lt;/i&gt; text 3&lt;/a&gt;
    &lt;/p&gt;
    &lt;div class=&quot;content-tab closed&quot;&gt;
        &lt;p&gt;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.&lt;/p&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- 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 -->

$(&#39;.toggle-content:not(.opened), .content-tab:not(.opened)&#39;).hide();
$(&#39;.tab-index a&#39;).click(function () {
    $(this).parent().next().slideToggle(&#39;easeOutExpo&#39;);
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;toggle&quot;&gt;
    &lt;p class=&quot;tab-index tab-closed&quot;&gt;
        &lt;a href=&quot;#&quot; title=&quot;Close&quot;&gt;&lt;i class=&quot;icon-plus-sign&quot;&gt;&lt;/i&gt; text 1&lt;/a&gt;
    &lt;/p&gt;
    &lt;div class=&quot;content-tab closed&quot;&gt;
        &lt;p&gt;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.&lt;/p&gt;
    &lt;/div&gt;
    
    &lt;p class=&quot;tab-index tab-closed&quot;&gt;
        &lt;a href=&quot;#&quot; title=&quot;Close&quot;&gt;&lt;i class=&quot;icon-plus-sign&quot;&gt;&lt;/i&gt; text 2&lt;/a&gt;
    &lt;/p&gt;
    &lt;div class=&quot;content-tab closed&quot;&gt;
        &lt;p&gt;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.&lt;/p&gt;
    &lt;/div&gt;
    
    &lt;p class=&quot;tab-index tab-closed&quot;&gt;
        &lt;a href=&quot;#&quot; title=&quot;Close&quot;&gt;&lt;i class=&quot;icon-plus-sign&quot;&gt;&lt;/i&gt; text 3&lt;/a&gt;
    &lt;/p&gt;
    &lt;div class=&quot;content-tab closed&quot;&gt;
        &lt;p&gt;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.&lt;/p&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: -1

&lt;!-- 开始代码片段: js 隐藏: false 控制台: true babel: false --&gt;

&lt;!-- 语言: lang-js --&gt;

    document.addEventListener('DOMContentLoaded', () =&gt; {
    	const accordions = document.querySelectorAll('.accordion');

    	accordions.forEach(accordion =&gt; {
    	  const toggles = accordion.querySelectorAll('.accordion-toggle');

    	  toggles.forEach(toggle =&gt; {
    		toggle.addEventListener('click', () =&gt; {
    		  const activeToggle = accordion.querySelector('.active');
    		  if (activeToggle &amp;&amp; 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;
    		  }
    		});
    	  });
    	});

    });

&lt;!-- 语言: lang-css --&gt;

    .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;
    }


&lt;!-- 语言: lang-html --&gt;

    &lt;div class="accordion"&gt;
    	  &lt;div class="accordion-toggle"&gt;Section 1&lt;/div&gt;
    	  &lt;div class="accordion-content"&gt;
    		&lt;div class="padding"&gt;Section 1的内容在这里。&lt;/div&gt;
    	  &lt;/div&gt;
    	  &lt;div class="accordion-toggle"&gt;Section 2&lt;/div&gt;
    	  &lt;div class="accordion-content"&gt;
    		&lt;div class="padding"&gt;Section 2的内容在这里。&lt;/div&gt;
    	  &lt;/div&gt;
    	  &lt;div class="accordion-toggle"&gt;Section 3&lt;/div&gt;
    	  &lt;div class="accordion-content"&gt;
    		&lt;div class="padding"&gt;Section 3的内容在这里。&lt;/div&gt;
    	  &lt;/div&gt;
    	&lt;/div&gt;

&lt;!-- 结束代码片段 --&gt;

LE: **在这里查看工作代码** 享受
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.addEventListener(&#39;DOMContentLoaded&#39;, () =&gt; {
	const accordions = document.querySelectorAll(&#39;.accordion&#39;);

	accordions.forEach(accordion =&gt; {
	  const toggles = accordion.querySelectorAll(&#39;.accordion-toggle&#39;);

	  toggles.forEach(toggle =&gt; {
		toggle.addEventListener(&#39;click&#39;, () =&gt; {
		  const activeToggle = accordion.querySelector(&#39;.active&#39;);
		  if (activeToggle &amp;&amp; activeToggle !== toggle) {
			activeToggle.classList.remove(&#39;active&#39;);
			activeToggle.nextElementSibling.style.maxHeight = 0;
		  }
		  toggle.classList.toggle(&#39;active&#39;);
		  const content = toggle.nextElementSibling;
		  if (toggle.classList.contains(&#39;active&#39;)) {
			content.style.maxHeight = content.scrollHeight + &#39;px&#39;;
		  } 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 -->

&lt;div class=&quot;accordion&quot;&gt;
	  &lt;div class=&quot;accordion-toggle&quot;&gt;Section 1&lt;/div&gt;
	  &lt;div class=&quot;accordion-content&quot;&gt;
		&lt;div class=&quot;padding&quot;&gt;Content for section 1 goes here.&lt;/div&gt;
	  &lt;/div&gt;
	  &lt;div class=&quot;accordion-toggle&quot;&gt;Section 2&lt;/div&gt;
	  &lt;div class=&quot;accordion-content&quot;&gt;
		&lt;div class=&quot;padding&quot;&gt;Content for section 2 goes here.&lt;/div&gt;
	  &lt;/div&gt;
	  &lt;div class=&quot;accordion-toggle&quot;&gt;Section 3&lt;/div&gt;
	  &lt;div class=&quot;accordion-content&quot;&gt;
		&lt;div class=&quot;padding&quot;&gt;Content for section 3 goes here.&lt;/div&gt;
	  &lt;/div&gt;
	&lt;/div&gt;

<!-- end snippet -->

LE: Working code here Enjoy

huangapple
  • 本文由 发表于 2023年3月9日 17:12:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682499.html
  • css
  • html
  • javascript

在Nunjucks模板中访问数组在 :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定