简体   繁体   English

手机上的jQuery CSS动画错误

[英]Jquery CSS animation bug on mobile

I have an animation using JQuery and CSS for sliding divs into view. 我有一个动画,使用JQuery和CSS将div滑入视图。

This is my javascript code: 这是我的JavaScript代码:

(function($) {
  $.fn.visible = function(partial) {
      var $t            = $(this),
          $w            = $(window),
          viewTop       = $w.scrollTop(),
          viewBottom    = viewTop + $w.height(),
          _top          = $t.offset().top,
          _bottom       = _top + $t.height(),
          compareTop    = partial === true ? _bottom : _top,
          compareBottom = partial === true ? _top : _bottom;
    return ((compareBottom <= viewBottom) && (compareTop >= viewTop));
  }; 
})(jQuery);


$(window).scroll(function(event) {

  $(".slide-up").each(function(i, el) {
    var el = $(el);
    if (el.visible(true)) {
      el.addClass("come-up"); 
    } 
  });

});

$(document).ready(function() {
  $(".heading-slide-down").each(function(i, el) {
    var el = $(el);
    if (el.visible(true)) {
      el.addClass("come-down"); 
    } 
  });

});

$(window).scroll(function(event) {

  $(".slide-left").each(function(i, el) {
    var el = $(el);
    if (el.visible(true)) {
      el.addClass("come-left"); 
    } 
  });

});

$(window).scroll(function(event) {

  $(".slide-right").each(function(i, el) {
    var el = $(el);
    if (el.visible(true)) {
      el.addClass("come-right"); 
    } 
  });

});

And this is my CSS 这是我的CSS

/** FADE IN SLIDING FROM BOTTOM TO TOP **/
.come-up {
  transform: translateY(150px);
  animation: comeup 0.8s ease forwards;
}
.come-up:nth-child(odd) {
  animation-duration: 0.6s;
}
@keyframes comeup {
  to { transform: translateY(0); }
}


/** FADE IN SLIDING FROM TOP TO BOTTOM **/
.come-down {
  transform: translateY(-100px);
  animation: comedown 0.8s ease forwards;
}
.come-down:nth-child(odd) {
  animation-duration: 0.6s;
}
@keyframes comedown {
  to { transform: translateY(0); }
}


/** FADE IN SLIDING FROM RIGHT TO LEFT **/
.come-left {
  transform: translateX(100px);
  animation: comeleft 0.8s ease forwards;
}
.come-left:nth-child(odd) {
  animation-duration: 0.6s;
}
@keyframes comeleft {
  to { transform: translateX(0); }
}


/** FADE IN SLIDING FROM LEFT TO RIGHT **/
.come-right {
  transform: translateX(-100px);
  animation: comeright 0.8s ease forwards;
}
.come-right:nth-child(odd) {
  animation-duration: 0.6s;
}
@keyframes comeright {
  to { transform: translateX(0); }
}

With my divs that need sliding I just apply the classes slide-up or slide-left etc. 对于需要滑动的div,我只应用了slide-upslide-left等类。

Live demo: http://www.shivampaw.com 现场演示: http//www.shivampaw.com

On my laptop it works fine, however on my phone (iPhone) the divs are already in the correct position and as I scroll towards them I see them transform away and then animate to where they should be. 在我的笔记本电脑上,它可以正常工作,但是在我的手机(iPhone)上,div已经处于正确的位置,并且当我向它们滚动时,我看到它们逐渐消失,然后动画到它们应该位于的位置。

I'm not sure how else I can explain this, if possible try and take a look for yourself and just scroll down the site slowly and you will see it. 我不确定我还能如何解释这一点,如果可以的话,请尝试一下自己,然后慢慢向下滚动网站,您会看到它。

How come this is happening and is there a fix? 这是怎么发生的,是否有解决方法?

Thanks! 谢谢!

Update: The problem is that on mobile safari on an iPhone SE latest iOS the divs that should be starting positioned downwards so they can slide up into place are starting in the right place and then moving down and sliding backup when they are in view. 更新:问题在于,在iPhone SE最新iOS上的移动浏览器中,应该开始向下定位以便它们可以向上滑动到适当位置的div从正确的位置开始,然后在视线范围内向下移动并滑动备份。

I'm experiencing the exact same issue. 我遇到了完全相同的问题。 The problem seems to be that on mobile devices, .visible() only becomes true some time AFTER the element has entered the screen (rather than EXACTLY when it enters the screen), making the element visible to you already before the animation is played. 问题似乎在于,在移动设备上,.visible()仅在元素进入屏幕后的某个时间才变为真(而不是在进入屏幕时准确地变为真),从而使元素在播放动画之前已经对您可见。 I quick-fixed this by giving the elements an opacity of 0 and changing this only when the animation plays. 我通过将元素的不透明度设置为0并仅在播放动画时更改了它来快速解决此问题。

You would have to add this to your CSS: 您必须将其添加到CSS中:

.slide-up, .heading-slide-down, .slide-left, .slide-right {
opacity:0;
}

.come-up, .come-down, .come-left, .come-right {
opacity:1;
-webkit-transition: opacity 0.8s ease-in;
-moz-transition: opacity 0.8s ease-in;
-o-transition: opacity 0.8s ease-in;
-ms-transition: opacity 0.8s ease-in;
transition: opacity 0.8s ease-in;
}

.come-up:nth-child(odd), .come-down:nth-child(odd), .come-left:nth-child(odd), .come-right:nth-child(odd) {
opacity:1;
-webkit-transition: opacity 0.6s ease-in;
-moz-transition: opacity 0.6s ease-in;
-o-transition: opacity 0.6s ease-in;
-ms-transition: opacity 0.6s ease-in;
transition: opacity 0.6s ease-in;
}

To make sure these animations aren't played on items that are already in view when the page loads, you could add this to your jQuery: 为了确保这些动画不会在页面加载时已在视图中的项目上播放,您可以将其添加到jQuery中:

  $(".slide-up, .slide-left, .slide-right").each(function() {
                    if ($(this).visible(true)) {
                        $(this).addClass("already-visible");
                        }
                    });

And at the bottom of your CSS: 在CSS的底部:

.already-visible {
opacity:1;
transform: translateY(0);
transform: translateX(0);
animation: none;
-webkit-transition: none;
-moz-transition: none;
-o-transition: none;
-ms-transition: none;
transition: none;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM