javascript - ontransitionend 在 CSS Translate3d 之后不绑定(bind)

标签 javascript jquery css css-transforms translate3d

我正在实现页面的滚动劫持,并且喜欢使用事件处理程序 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 msTransitionEndtransitionend','.footer', function(e) { is_moving = false; });

关于javascript - ontransitionend 在 CSS Translate3d 之后不绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32834288/

相关文章:

javascript - 在 JavaScript 的 for 循环中移动数组

javascript - ExtJS 默认过滤器处于事件状态,但网格未过滤

javascript - 组合两个 Javascript 对象

javascript - 以编程方式选择剑道网格行

javascript - Jquery 点击自动化有延迟?

jquery - 如何在单击 "Enter"按钮时添加标签(不是 HTML),然后将其保存为字符串格式?

javascript - 谷歌脚本: Delete row if a value in it exists in another sheet

javascript - JS函数不显示任何东西

html - 如何仅使用 css 隐藏一个选项元素

html - IE 无法识别网站的媒体查询