简体   繁体   English

如何使这个 animation 从屏幕中间开始,而不是在视图中?

[英]How to make this animation begin in middle of screen, instead of when in view?

The below code is working exactly how I want, but I want the animation to happen not upon being viewed, but in the middle of the screen.下面的代码完全按照我的意愿工作,但我希望 animation 不是在被查看时发生,而是在屏幕中间发生。 I don't really want to slow down the animation, so I'm hoping for a way to change the JS to make this work...我真的不想放慢 animation 的速度,所以我希望有一种方法可以改变 JS 来完成这项工作......

(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);

var win = $(window);

var allModsL = $(".img-l-slide");
var allModsR = $(".img-r-slide");

win.scroll(function(event) {

allModsL.each(function(i, el) {
  var el = $(el);
  if (el.visible(true)) {
    el.addClass("come-in-l"); 
  } 
});
  allModsR.each(function(i, el) {
    var el = $(el);
    if (el.visible(true)) {
      el.addClass("come-in-r"); 
    } 
  });

});

you need test if an element in viewport, for more information read this tutorial您需要测试视口中是否有元素,有关更多信息,请阅读本教程

How to test if an element is in the viewport with vanilla JavaScript 如何使用 vanilla JavaScript 测试元素是否在视口中

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

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