我的脚本需要页面刷新才能运行

Ang*_*elo 1 javascript jquery

每次我第一次加载页面时我的脚本都没有加载,我需要刷新页面至少3次才能运行.如何在没有页面刷新的情况下运行脚本?这个特殊的脚本在BABEL中.

'use strict';

var deg = 0;
var index = 0;

$('#' + index).css('opacity', '1');
$('.navbar').contextmenu(function () {
    return false;
});
$('.navbar').on('mousewheel', function (e) {
    var move = -60;
    var nextIndex = nextIndex = (index + 1) % 6;
    if (e.originalEvent.wheelDelta / 120 <= 0) {
        // wheel up
        move = 60;
        nextIndex = index  -1 < 0 ? 5 : index - 1;
    }
    $('.scroll-list').css('transform', 'rotateX(' + (deg + move) + 'deg)');
    $('#' + index).css('opacity', '0.01');
    $('#' + nextIndex).css('opacity', '1');
    index = nextIndex;
    deg = deg + move;
    event.stopPropagation();
    console.timeEnd('cache');
});
Run Code Online (Sandbox Code Playgroud)

Cht*_*lek 5

如果你将代码包装在window.onload函数中怎么办?

window.onload = function() { 
  // your code
}
Run Code Online (Sandbox Code Playgroud)

您的代码仅在html文档"准备就绪"并完全加载时运行.