javascript - jQuery:CSS 动画不流畅

标签 javascript html jquery css

我有一个简单的代码,其中 jQuery 以 2 秒的间隔在标题中隐藏和显示更改的内容。

添加 CSS 类时此脚本中的问题以及添加新类后的脚本。动画不流畅,我真的不知道有什么可以让它更流畅。

我用 CSS 和脚本尝试了很多东西,它仍在这样做。

编辑

我需要完成 CSS 动画。

setTimeout(function() {
   $('.top-slider').toggleClass('active');
     $('#slider-headline').on('transitionend webkitTransitionEnd', function() {
            $('.top-slider').removeClass('active');
            $("#slider-headline").text("Hello world!");
    });
}, 2000);
#slider-headline {
            text-transform: uppercase;
            font-size: 2.5em;
            padding: 0 8px;
            overflow: hidden;
            max-height: 1000px;
            transition: max-height 2s cubic-bezier(0, 1, 0, 1);
            position: relative;
            z-index: 2;
      background: #bada55
}

.top-slider.active #slider-headline {
    max-height: 0;
    transition: max-height 2s ease-in-out;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<section class="top-slider">
            <div class="content">
                <div class="slider-headline">
                    <h2 id="slider-headline">Web design</h2>
                </div>
            </div>
</section>

最佳答案

尝试从 .top-slider.active #slider-headline 开始过渡,并在 #slider-headline 上使用它。

setTimeout(function() {
   $('.top-slider').toggleClass('active');
   $('#slider-headline').on('transitionend webkitTransitionEnd', function() {
     $('.top-slider').removeClass('active');
     $("#slider-headline").text("Hello world!");
   });
}, 2000);
#slider-headline {
  text-transform: uppercase;
  font-size: 2.5em;
  padding: 0 8px;
  overflow: hidden;
  max-height: 600px;
  position: relative;
  z-index: 2;
  background: #bada55;
  transition: max-height 2s ease-in-out;
}

.top-slider.active #slider-headline {
  max-height: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<section class="top-slider">
  <div class="content">
    <div class="slider-headline">
      <h2 id="slider-headline">Web design</h2>
    </div>
  </div>
</section>

关于javascript - jQuery:CSS 动画不流畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63153942/

相关文章:

javascript - 使用数组对象获取最大的更大键值

css - 带有 Bootstrap 的图片库

javascript - 使用 PHP 将用户输入和时间记录到文本文件

php - 删除未关闭的 html 标签 - 同时将内容粘贴到带有 contenteditable 的 div

javascript - 更改元素的 ID 不适用于 jQuery

jquery - 如何在 jQuery 中对多行文本 4x35 进行正则表达式验证

javascript - 在 JavaScript 中返回条件语句

javascript - 通过google打包的app访问网站的DOM

c# - 从重定向页面返回后如何更新网页?

javascript - 如何在 Javascript 中向上移动表格的所有行?