在页面中,常会有这样的需求,比如做一个相册,相册外部的div是固定,而我们需要向相册中添加照片,而照片的大小不一定刚好等于当前的这个div的大小,所以我们就需要对这张照片处理,使其等比例缩放后,显示图片最中间的位置。现在就将部分实现展示如下。
for(var n = 0 ;n <length ;n ++){
(function(n){
var img=document.createElement("img");
img.src = that.basepath + that.array[n];
img.onload = function(){
var oldWidth = img.width;//原始图片宽高
var oldHeight = img.height;
var scale = Math.max(LayoutManager.ExtractNumber(width)/ LayoutManager.ExtractNumber(oldWidth), LayoutManager.ExtractNumber(height) / LayoutManager.ExtractNumber(oldHeight));
var newWidth = oldWidth * scale + "px";
var newHeight = oldHeight * scale + "px";
var marginLeft = (LayoutManager.ExtractNumber(width) - LayoutManager.ExtractNumber(newWidth)) + "px";
var marginTop = (LayoutManager.ExtractNumber(height) - LayoutManager.ExtractNumber(newHeight)) + "px";
if(newWidth > newHeight){
marginLeft = (LayoutManager.ExtractNumber(that.photowidthstyle) - LayoutManager.ExtractNumber(newWidth))/2 + "px";
marginRight = marginLeft;
marginTop = "0px";
marginBottom = "0px";
}else{
marginTop = (LayoutManager.ExtractNumber(that.photowidthstyle) - LayoutManager.ExtractNumber(newHeight))/2 + "px";
marginBottom = marginTop ;
marginLeft = "0px";
marginRight = "0px";
}
var pos = {"marginTop":marginTop,"marginRight":marginRight,"marginBottom":marginBottom,"marginLeft":marginLeft,"newWidth":newWidth,"newHeight":newHeight };
newarraylist[n] = pos;
//如果宽度大于高度了,就按照宽度来设置
if(loadnumber + 1 == length){//全部加载完成的时候
if(enterflag == false){
for(var m = 0 ;m < length;m++){
if(that.editable && m == length-1){//最后一个图片的时候
list += "<li index="+m+" style='height: "+that.photowidthstyle+";width: "+that.photowidthstyle+";display: inline-block;position:relative;overflow: hidden;'><img height="+newarraylist[m].newHeight+" width="+newarraylist[m].newWidth+"
style='margin-left:"+newarraylist[m].marginLeft+";margin-top:"+newarraylist[m].marginTop+";margin-right:"+newarraylist[m].marginRight+";margin-bottom:"+newarraylist[m].marginBottom+"' src="+ that.basepath+that.array[m]+"><span class='photo_describe'>"+that.textarray[m]+"</span>"+
"<span id="+that.getObjId("addphoto")+" style='display:block;position:absolute;left:0px;bottom:0px;height:30px;width:35px;vertical-align:middle;inline-height:40px;border-radius:20px;color:white;text-align:center;'>"+
"<img height='100%' width='100%' src="+that.addphotoicon+"></span></li>";
}else{
list += "<li index="+m+" style='height:"+that.photowidthstyle+";width:"+that.photowidthstyle+";display: inline-block;position:relative;overflow: hidden;'><img height="+newarraylist[m].newHeight+" width="+newarraylist[m].newWidth+"
style='margin-left:"+newarraylist[m].marginLeft+";margin-top:"+newarraylist[m].marginTop+";margin-right:"+newarraylist[m].marginRight+";margin-bottom:"+newarraylist[m].marginBottom+"' src="+ that.basepath+that.array[m]+"><span class='photo_describe'>"+that.textarray[m]+"</span></li>";
}
}
enterflag = true;
}
}
that.appendChild(ul);
ul.innerHTML = list;
if(that.editable){
that.bind(that.getObjById("addphoto"),"click",function(e){
e.stopPropagation();
that.updateflag = "false";
that.getObjById("fileinput").click();
if($("editelement").style.display = "block" || $("editelement").style.display == "" ){
$("editelement").style.display = "none";
}
});
}
that.height = Math.ceil(length / 2) * (that.photowidth + 11) + "px";//图像的高度Math.ceil(length/2) * this.photowidth 加上25px为添加图像按钮的高度。
that.width = LayoutManager.getWindowWidth() - 20 +"px";//宽度等于页面宽度减去左右侧10px的宽度。
that.getDom().style.height = that.height;
that.getDom().style.width = that.width;
if(that.getDom().parentNode.layout.getSublayoutByType("Interaction").visible == "true" || that.getDom().parentNode.layout.getSublayoutByType("Interaction").visible == true){
that.getDom().parentNode.layout.height = LayoutManager.ExtractNumber(that.height) + 42 +"px";
}else{
that.getDom().parentNode.layout.height = LayoutManager.ExtractNumber(that.height) +"px";
}
that.getDom().parentNode.layout.setAttr("height",that.getDom().parentNode.style.height);
//设置整个组件的高度。
loadnumber ++;
}
以上,详细内容再更新。