我有一个模板,其中照片显示在一个框架中(每个框架对于不同的图像是不同的).我已经编写了一个功能,使用图像原始的高度和宽度,并给我自定义的宽度和高度为该特定框架,以便恢复宽高比.现在我通过onload调用该函数,因为图像在特定时刻加载.
我的feed.hbs(模板)
<img src = "{{photo.0.photo_url}}" onload = "OnImageLoad(event);" {{action "imgOverlay0" photo}}/>
Run Code Online (Sandbox Code Playgroud)
功能
function OnImageLoad(evt) {
var img = evt.currentTarget;
// what's the size of this image and it's parent
var w = $(img).width();
var h = $(img).height();
var tw = $(img).parent().width();
var th = $(img).parent().height();
// compute the new size and offsets
var result = scaling(w, h, tw, th,false);
// adjust the image coordinates and size
img.width = result.width;
img.height = result.height;
$(img).css("margin-left", result.targetleft);
$(img).css("margin-top", result.targettop);
// console.log("result",result)
return …Run Code Online (Sandbox Code Playgroud)