Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行jquery函数onload和focus_Jquery_Focus_Onload - Fatal编程技术网

运行jquery函数onload和focus

运行jquery函数onload和focus,jquery,focus,onload,Jquery,Focus,Onload,我有一个功能: function skillbar() { $('.skillbar').each(function(){ var percent = $(this).attr('data-percent'); $(this).find('.skillbar-bar').delay(400).animate({width:percent},1500); }); } 我希望在加载页面时执行此函数。所以我是这样运行的: $(window).load(

我有一个功能:

function skillbar() {
    $('.skillbar').each(function(){
        var percent = $(this).attr('data-percent');
        $(this).find('.skillbar-bar').delay(400).animate({width:percent},1500);
    });
}
我希望在加载页面时执行此函数。所以我是这样运行的:

$(window).load(function(){
    skillbar();
});
但我也希望这个功能只在浏览器选项卡处于焦点时运行。 我试过这个:

$(window).load(function(){
    $(window).focus(function(){
        skillbar();
    });
});
问题是,当我使用.focus时;加载页面时,我必须切换浏览器选项卡一次,然后它才能工作。 这意味着我必须专注于窗口加载,但如果有人在另一个浏览器选项卡上呢


演示

您需要$document.readyfunction{ 这将在加载时启动该功能

$(document).ready(function(){
skillbar();
function skillbar() {
    $('.skillbar').each(function(){
        var percent = $(this).attr('data-percent');
        $(this).find('.skillbar-bar').delay(400).animate({width:percent},1500);
    });
}
});

我添加了一个演示我的意思,如果它有帮助的话: