【发布时间】:2016-01-18 22:38:57
【问题描述】:
请检查我下面的 js 代码。它是为从文件夹中获取个人资料图像而编写的。我得到了个人资料图片,并且显示正确。
我有一个默认图片,如果用户没有个人资料图片,则会显示默认图片。
我的问题是当页面刷新时,如果用户有个人资料图片默认图像最初显示(一毫秒),然后显示个人资料图片。我该如何解决这个问题。我认为这是 jQuery 的加载问题。
test.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();
}
});
});
html页面:
<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);
}
}
}
【问题讨论】:
-
将它作为变量从控制器传递到视图文件...使用 php 逻辑返回默认或自定义照片名称。
-
是的,这是一个加载问题。您的 css 将默认显示为 bg。当 ajax 等待响应时,直到响应没有到达,背景图像就会显示出来。也许在本地并缓存它一毫秒,但也可能是几秒钟......为什么你用 ajax 加载图像而不是默认设置?
-
那么更好的办法是在没有 ajax 的情况下加载图像,对吧?
标签: php jquery laravel laravel-5