[英]Fade in elements when they come into viewport
每当出现这些元素时,我都会尝试使用“ fade-me”类淡入淡出。 我发现了这个小提琴: http : //jsfiddle.net/tcloninger/e5qad/ ,它确实做了这件事,但是它将不透明度值反复添加到所看到的元素中。 如果我尝试使用Velocity的过渡slideUpIn而不是不透明度,则会创建一个循环动画。 所以我有以下代码:
$(document).ready(function() {
/* Every time the window is scrolled ... */
$(window).scroll( function(){
/* Check the location of each desired element */
$('.hideme').each( function(i){
var bottom_of_object = $(this).offset().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
/* If the object is completely visible in the window, fade it it */
if( bottom_of_window > bottom_of_object ){
$(this).velocity('transition.slideUpIn', { stagger: 700 }).delay(1000)
}
});
});
});
它可以工作,但是会循环播放SlideUpIn动画。 如何使它在出现的那个元素上只运行一次动画?
切换CSS类作为指标,然后检查它:
var $window = $(window);
var $doc = $(document);
var $hideMe = $('.hideme');
$doc.ready(function () {
$window.scroll(function () {
var bottom_of_window = $window.scrollTop() + $window.height();
$hideMe.each(function (i) {
var $elm = $hideMe.eq(i);
var bottom_of_object = $elm.offset().top + $elm.outerHeight();
if (bottom_of_window > bottom_of_object) {
if (!$elm.hasClass('in-viewport')) {
$elm.addClass('in-viewport');
$elm.velocity('transition.slideUpIn', { stagger: 700 }).delay(1000);
}
} else {
$elm.removeClass('in-viewport');
}
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.