我一直在试图弄清楚如何在滚动到视图中时出现动画,但几乎没有运气。我找到了一个JQuery插件“Waypoints”,但我缺乏使用它的技能。我正在尝试使用CSS3关键帧,当它们滚动到视图中时会生成动画。我取得了突破,接近我想要完成的任务,但它并不是我想要的地方。我有一个名为“bounceinright”的类,它使项目反弹到屏幕右侧。这是CSS的原因:
@-webkit-keyframes bounceinright {
0% {
opacity: 0;
-webkit-transform: translateX(2000px);
}
60% {
opacity: 1;
-webkit-transform: translateX(-30px);
}
80% {
-webkit-transform: translateX(10px);
}
100% {
-webkit-transform: translateX(0);
}
}
@-moz-keyframes bounceinright {
0% {
opacity: 0;
-moz-transform: translateX(2000px);
}
60% {
opacity: 1;
-moz-transform: translateX(-30px);
}
80% {
-moz-transform: translateX(10px);
}
100% {
-moz-transform: translateX(0);
}
}
@-o-keyframes bounceinright {
-o-transform: translateX(2000px);
}
60% {
opacity: 1;
-o-transform: translateX(-30px);
}
80% {
-o-transform: translateX(10px);
}
100% {
-o-transform: translateX(0);
}
}
@keyframes bounceinright {
0% {
opacity: 0;
transform: translateX(2000px);
}
60% {
opacity: 1;
transform: translateX(-30px);
}
80% {
transform: translateX(10px);
}
100% {
transform: translateX(0);
}
}
.bounceinright {
display: none;
}
.bounceinright.start {
-webkit-animation: bounceinright 1s ease-out forwards;
-moz-animation: bounceinright 1s ease-out forwards;
-ms-animation: bounceinright 1s ease-out forwards;
-o-animation: bounceinright 1s ease-out forwards;
animation: eigbounceinrighthty 1s ease-out forwards;
display: block;
}
然后我有一个小的JQuery片段,它会在滚动到的时候用“start”类触发动画。
////////////////////////////////
//scroll events
////////////////////////////////
function isElementInViewport(elem) {
var $elem = $(elem);
// Get the scroll position of the page.
var scrollElem = ((navigator.userAgent.toLowerCase().indexOf('webkit') != -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.
function checkAnimation() {
var $elem = $('.row .wpb_content_element');
// If the animation has already been started
if ($elem.hasClass('start')) return;
if (isElementInViewport($elem)) {
// Start the animation
$elem.addClass('start');
}
}
// Capture scroll events
$(window).scroll(function(){
checkAnimation();
});
我的问题是,当一个带有“bounceinright”的项目滚动到时,它会触发所有具有相同类别的项目。我想知道是否有办法让他们单独触发但是同一个班级。任何帮助将不胜感激。
谢谢!
答案 0 :(得分:2)
将checkAnimation()
更改为
// Check if it's time to start the animation.
function checkAnimation() {
var $elem = $('.row .wpb_content_element');
$elem.each(function(){
var $singleElement = $(this);
// If the animation has already been started
if ($singleElement.hasClass('start')) return;
if (isElementInViewport($singleElement)) {
// Start the animation
$singleElement.addClass('start');
}
});
}