laravel admin 在详情(show 类)中点击图片放大,通过jQ渲染编写
控制器中添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| use Encore\Admin\Facades\Admin; . . .
protected function detail($id) { $show = new Show(User::findOrFail($id)); $html = '<div id="outerdiv" style="position:fixed;top:0;left:0;background:rgba(0,0,0,0.7);z-index:2;width:100%;height:100%;display:none;">'; $html .= '<div id="innerdiv" style="position:absolute;"><img id="bigimg" style="border:5px solid #fff;" src="" /></div>';
Admin::html($html);
$show->field('avatar', __('头像'))->unescape()->as(function ($avatar){ return "<img src='".config('app.url').'/'.$avatar ."' width=120 height=120 class='pic' />"; }); . . .
return $show; }
|
注意 img
标签的class必须是pic,这个是以js文件对应的
js 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| $(function () { $(".pic").click(function () { console.log(123); var _this = $(this); imgShow("#outerdiv", "#innerdiv", "#bigimg", _this); }); });
function imgShow(outerdiv, innerdiv, bigimg, _this) { var src = _this.attr("src"); $(bigimg).attr("src", src); $("<img/>").attr("src", src).load(function () { var windowW = $(window).width(); var windowH = $(window).height(); var realWidth = this.width; var realHeight = this.height; var imgWidth, imgHeight; var scale = 0.8; if (realHeight > windowH * scale) { imgHeight = windowH * scale; imgWidth = imgHeight / realHeight * realWidth; if (imgWidth > windowW * scale) { imgWidth = windowW * scale; } } else if (realWidth > windowW * scale) { imgWidth = windowW * scale; imgHeight = imgWidth / realWidth * realHeight; } else { imgWidth = realWidth; imgHeight = realHeight; } $(bigimg).css("width", imgWidth); var w = (windowW - imgWidth) / 2; var h = (windowH - imgHeight) / 2; $(innerdiv).css({"top": h, "left": w}); $(outerdiv).fadeIn("fast"); }); $(outerdiv).click(function () { $(this).fadeOut("fast"); }); }
|
把这个js文件引入到 app/Admin/bootstrap.php
中就可以了
1
| Admin::js('/your/javascript/path/js.js');
|
最终呈现