【问题标题】:Choppy / glitchy CSS animations with toggleClass带有 toggleClass 的 Choppy / glitchy CSS 动画
【发布时间】:2016-12-13 13:28:33
【问题描述】:

我想知道是否有任何方法可以优化我网站上的 CSS 动画。我的标题中有两个按钮,它们可以激活页面两侧的 toggleClass 动画,带出一个滑动菜单,用它推动页面的其余部分。

这是一个例子:https://jsfiddle.net/8nj5y4t1/62/

我注意到,虽然有时它们非常平滑,但有时它们的移动似乎也很生涩/有问题。这在 Safari 中最常发生。我刚买了一台规格已满的新 macbook,但这种情况仍在发生,所以很可能是我做错了。

我还注意到,我得到这个想法的网站几乎总是很流畅。据我所知,它也在使用 toggleClass – www.etq-amsterdam.com/

谁能告诉我如何改进下面的代码?

我的代码如下:

HTML:

<header class="header">
  <span id="button-one"></span>
  <span id="button-two"></span>
  <div class="push-menu-one"></div>
  <div class="push-menu-two"></div>
  <div class="overlay"></div>
</header>

<div class="content"></div>

<footer class="footer"></footer>

CSS:

html {
  position:relative;
  height:100%;
  left:0;
  right:0;
  background-color:pink;
  -webkit-transition: all .6s cubic-bezier(.645,.045,.355,1);
  transition: all .6s cubic-bezier(.645,.045,.355,1); 
}

body {
  min-height:100%;
  margin:0; 
  padding:0; 

  display:-webkit-box;
  display:-webkit-flex;
  display:-ms-flexbox;
    display:flex;

  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
    flex-direction: column;
}

.header {
  height:70px;
  width:100%;
  background-color:white;
}

.content {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  -ms-flex: 1;
    flex: 1;

  width:85%;
  margin-top:50px;
  margin-left:auto;
  margin-right:auto;
}

.footer {
  display:-webkit-box;
  display:-webkit-flex;
  display:-ms-flexbox;
    display:flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
    flex-direction: column;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
    align-items: center;

  height: auto;
  width: 100%;      
  padding: 10px 0 10px 0;
  background-color: #efefef;
 }

/* PUSH MENUS */

#button-one {
  display:inline-block;
  width:30px;
  height:30px;
  margin:20px;
  background-color:green;
  cursor:pointer;
}

#button-two {
  display:inline-block;
  float:right;
  width:30px;
  height:30px;
  margin:20px;
  background-color:orange;
  cursor:pointer;
}

.push-menu-one {
  position:fixed;
  top:0px;
  left:-295px;
  width:295px;
  height:100%;
  margin:0;
  padding:0;
  background-color:wheat;
  -webkit-transition: all .6s cubic-bezier(.645,.045,.355,1);
  transition: all .6s cubic-bezier(.645,.045,.355,1);
}

.push-menu-two {
   position:fixed;
   top:0px;
   right:-295px;
   width:295px;
   height:100%;
   margin:0;
   padding:0;
   background-color:darkred;
   -webkit-transition: all .6s cubic-bezier(.645,.045,.355,1);
   transition: all .6s cubic-bezier(.645,.045,.355,1);
}

.overlay {
   position:fixed;
   z-index:9;
   top:0px;
   left:0px;
   width:0px;
   height:0px;
   background-color:#000000;
   opacity:0;

   transition: opacity 1s, width 0s ease 1s, height 0s ease 1s;
 }

.overlay.open-right,
.overlay.open-left {
   width:100%;
   height:100%;
   opacity:0.4;

   transition: opacity 1s;
}
/* TOGGLE CLASSES */

html.open-left {
  left:295px;
}

.push-menu-one.open-left {
  left:0;
}

html.open-right {
  left:-295px;
}

.push-menu-two.open-right {
  right:0;
}

jQuery:

jQuery(document).ready(function($) {    

    $('#button-one').click(function() {
        $('html, .overlay, .push-menu-one').toggleClass('open-left');

 });

 $('#button-two').click(function() {
        $('html, .overlay, .push-menu-two').toggleClass('open-right');

});

$('.overlay').click(function() {
        $('html, .overlay, .push-menu-one, .push-menu-two').removeClass('open-left');

$('html, .overlay, .push-menu-one, .push-menu-two').removeClass('open-right');

});

});

【问题讨论】:

  • 我用 safari 在 macbook 中尝试了你的 jsfiddle,但我看不到故障,它运行得非常顺利。
  • 这些抖动动画错误通常很难复制,因为它们与您的操作系统、浏览器、硬件以及 CPU/GPU 的繁忙程度有关。查看我对类似问题的回答here
  • 我不确定这是否与我的操作系统或硬件有关,因为我在前几天死掉的旧 2009 macbook 上尝试了这个,然后是全新的 2015 macbook 和他们俩的表现都一样。
  • 实际的网站是lucieaverillphotography.co.uk/home——我很想知道是否有人可以让它在 Safari 中像在 jsfiddle 中一样流畅。由于某种原因,jsfiddle 更流畅。

标签: jquery css


【解决方案1】:

这段代码没有问题。问题出在你的电脑上。尝试关闭其他一些选项卡或程序。我唯一能想到的就是添加代码以适应不同的浏览器,如下所示:

-moz-transition: opacity 1.5s;
-webkit-transition: opacity 1.5s;
-o-transition: opacity 1.5s;
transition: opacity 1.5s;

【讨论】:

  • 嗯,我试过了,但它在 safari 中仍然表现不佳。我看不出它会成为我的电脑,尤其是在我尝试过的每台机器上——无论是旧的还是新的。据我所知,我上面提到的网站:etq-amsterdam.com 也使用了 toggleClass,唯一的区别是他们使用了变换而不是左右来为菜单设置动画。我已经尝试在我的上使用 transform 属性,但它仍然不能解决我的问题。在我尝试过的每台计算机上,他们的工作正常,而我的则支离破碎:/
  • 在 Safari 中没有什么能很好地工作。您应该切换到 Google Chrome,尤其是作为开发人员。
猜你喜欢
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
  • 2017-02-20
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 2015-02-02
  • 1970-01-01
相关资源
最近更新 更多