加载图像慢laravel jQuery


Loading image slow laravel jQuery

请查看下面我的js代码。它是为从文件夹中获取配置文件图像而编写的。我有得到个人资料的图像,它正确显示。

我有一个默认图像,如果用户没有配置文件图像,则显示默认图像。

我的问题是当页面刷新时,如果用户有配置文件图像默认图像最初显示(毫秒),然后配置文件图像显示。我怎么解决这个问题呢?我认为这是jQuery的加载问题。

. js

$(function () {
    $.get("/getDetails")
        .done(function (data) {
            $("#profile_image").css('background-image', 'url(/profile.png)');
            if (data.filename == 'Profile') {
                $("#profile_image").css('background-image', 'url(' + data.route + ')');
                $("#deleteProfileImage").show();
            }
    });
});

页面:

<div id="profile_image" style="background: url(/profile.png) no-repeat center center;">

控制器页面

public function show()
{
    $id                  = Auth::user()->id;
    $details             = User::select('id', 'created_at')->findOrFail($id);
    $encrypt             = md5($details->id.$details->created_at);
    $directories         = Storage::files($encrypt);                                             // Listout Files
    foreach($directories as $values){
        $split_folder_file = explode('/', $values);           //08d16e9f44699e9334834833c02b7b8e/Profile.png
        $splitted_file     = end($split_folder_file);         //Profile.png
        $explode_filename  = explode('.', $splitted_file);    //explode(Profile.png)
        $explode_name      = $explode_filename[0];            //Profile
        $file_extension    = $explode_filename[1];            //png
        if ($explode_name == 'Profile') {
            switch( $file_extension ) {
                case "gif": $ctype="image/gif"; break;
                case "png": $ctype="image/png"; break;
                case "jpeg":
                case "jpg": $ctype="image/jpeg"; break;
                default:
            }
            $file = Storage::disk('local')->get($encrypt.'/'.$splitted_file);
            return response($file, 200)
                ->header('Content-Type', $ctype);
        }
    }
}

试试这个:

$(function () {
    $("#profile_image").css('background-image', 'url(/profile.png)');
    $.get("/getDetails")
        .done(function (data) {
            if (data.filename == 'Profile') {
                $("#profile_image").css('background-image', 'url(' + data.route + ')');
                $("#deleteProfileImage").show();
            }
    });
});

它显示了一个初始的毫秒,因为它在你的代码中写,它首先显示默认的图像,然后检查图像是否为用户存在,然后用默认的图像替换它。

我将默认图像移动到else条件,以便仅在上传的用户图像不可用时显示。