gpt4 book ai didi

javascript - 试图在 View 中将类添加到 div

转载 作者:行者123 更新时间:2023-11-28 02:17:22 25 4
gpt4 key购买 nike

我对 JavaScript 或除 html 以外的任何代码都非常陌生。我找到了一个适合我需要的脚本。但它似乎只适用于 Firefox。在 Firefox 中,当元素滚动到 View 中时,它会将 .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>

最佳答案

问题是你如何添加类

改变这个

$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>

关于javascript - 试图在 View 中将类添加到 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48388970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com