我正在实现页面的滚动劫持,并且喜欢在ontransitionend上使用事件处理程序,以跟踪CSS3转换的结束,translate3d。
但是,有时我会“丢失”事件处理程序,并且它不会触发ontransitionend。
有谁知道会发生什么?
我不想使用jQuery动画,因为它们运行缓慢,并且设置超时会导致延迟/闪烁。不知道其他实现此效果的好方法。
.content {
width: 100%;
height: 100%;
display: block;
position: relative;
padding: 0;
.transition(all 1500ms ease); // LESS mixin w/ transition prefixes
}
.page {
width: 100%;
height: 100vh;
}
<div class="content-wrapper">
<div class="content">
<section class="page target" id="One"></section>
<section class="page target" id="Two"></section>
<section class="page target" id="Three"></section>
</div>
<div class="footer">
</div>
</div>
var total_sections = $('.page.target').length;
var is_moving = false;
$(window).on({
'DOMMouseScroll mousewheel': detectScroll
});
// transitionend, runs into = problems
$('.content, .footer').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(e) {
is_moving = false;
});
function detectScroll (e) {
if(is_moving) {
return false;
}
is_moving = true;
(function() {
scrollPage(e.originalEvent.wheelDelta > 0 ? 'up' : 'down');
})();
// return false;
}
var curr_section = 0; // we always start at top of page
function scrollPage(dir) {
setTarget(curr_section, dir);
}
function setTarget(curr, dir) {
var target;
if(dir == 'up') {
target = curr-1;
}
if(dir == 'down') {
target = curr+1;
}
var h = $('.page.target').height();
target = target * h * -1;
is_moving = true;
// jquery animations run slower than css3 transitions :(
// $('.content, .footer').animate({
// 'top': target
// }, 50);
$('.content, .footer').css({
'transform': 'translate3d(0px, ' + target + 'px, 0px)'
});
// is_moving = false after transition ends
// using settimeout to reset is_moving causes a "flicker" / jump
// var transition_speed = 1600;
// window.setTimeout(function(){
// is_moving = false;
// }, transition_speed);
// reset current section
if(dir == 'up') {
curr_section = curr_section-1;
}
if(dir == 'down') {
curr_section = curr_section+1;
}
}
最佳答案
如何从一个切换到打开?$('.content').on('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend','.footer', function(e) { is_moving = false; });