【问题标题】:trying to add a class to a div when it is in view尝试在视图中将类添加到 div
【发布时间】:2018-01-23 03:58:06
【问题描述】:

我对 JavaScript 或除 html 之外的任何代码都非常陌生。我找到了一个适合我需要的脚本。但它似乎只适用于 Firefox。在 Firefox 中,当元素滚动到视图中时,它会将.animate.slideToLeft 类添加到div。 CSS 来自animate.css

为什么它不能在 chrome 中工作?

Code


function isElementInViewport(elem) {
  var $elem = $(elem);

  // Get the scroll position of the page.
  var scrollElem = ((navigator.userAgent.toLowerCase().indexOf('move_Damn') != -1) ? 'body' : 'html');
  var viewportTop = $(scrollElem).scrollTop();
  var viewportBottom = viewportTop + $(window).height();

  // Get the position of the element on the page.
  var elemTop = Math.round($elem.offset().top);
  var elemBottom = elemTop + $elem.height();

  return ((elemTop < viewportBottom) && (elemBottom > viewportTop));
}

// Check if it's time to start the animation.

var $elem = $("maybe");

function checkAnimation() {
  if (isElementInViewport($elem)) {
    // Start the animation

    $elem.addClass('animated', 'slideInLeft');
  } else {
    $elem.removeClass('animated', 'slideInLeft');
  }
}

// Capture scroll events
$(window).scroll(function() {
  checkAnimation();
});
#move_Damn {
  width: 50px;
  height: 50px;
  background: #f00;
  padding: 3px;
  text-align: center;
}

.animated {
  animation-duration: 1s;
  animation-fill-mode: both;
}

.slideInLeft {
  animation-name: slideInLeft;
}

@keyframes slideInLeft {
  from {
    transform: translate3d(-100%, 0, 0);
    visibility: visible;
  }
  to {
    transform: translate3d(0, 0, 0);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="move_Damn" class="wontWork">
  why not
</div>

【问题讨论】:

  • 您可以发布您的代码而不是链接到它吗?
  • 请添加您正在使用的代码,包括任何相关的 HTML、CSS 和 JavaScript。您可以使用 CodepenJSFiddle 之类的内容创建代码副本供我们查看。

标签: javascript html css google-chrome firefox


【解决方案1】:

问题是你如何添加类

改变这个

$elem.addClass('animated', 'slideInLeft');

到这里

$elem.addClass('animated slideInLeft');

function isElementInViewport(elem) {
  var $elem = $(elem);

  // Get the scroll position of the page.
  var scrollElem = ((navigator.userAgent.toLowerCase().indexOf('move_Damn') != -1) ? 'body' : 'html');
  
  var viewportTop = $(scrollElem).scrollTop();
  var viewportBottom = viewportTop + $(window).height();

  // Get the position of the element on the page.
  var elemTop = Math.round($elem.offset().top);
  var elemBottom = elemTop + $elem.height();

  return ((elemTop < viewportBottom) && (elemBottom > viewportTop));
}

// Check if it's time to start the animation.

var $elem = $("#move_Damn");

function checkAnimation() {
  if (isElementInViewport($elem)) {
    // Start the animation

    $elem.addClass('animated slideInLeft');
  } else {
    $elem.removeClass('animated slideInLeft');
  }
}

// Capture scroll events
$(window).scroll(function() {
  checkAnimation();
});
#move_Damn {
  width: 50px;
  height: 50px;
  background: #f00;
  padding: 3px;
  text-align: center;
}

.animated {
  animation-duration: 1s;
  animation-fill-mode: both;
}

.slideInLeft {
  animation-name: slideInLeft;
}

@keyframes slideInLeft {
  from {
    transform: translate3d(-100%, 0, 0);
    visibility: visible;
  }
  to {
    transform: translate3d(0, 0, 0);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="move_Damn" class="wontWork">
  why not
</div>
<br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 2013-08-02
    • 2012-01-20
    • 2020-10-23
    • 1970-01-01
    相关资源
    最近更新 更多