【发布时间】:2014-05-18 01:10:19
【问题描述】:
让我从很长的帖子开始,我很抱歉。 我正在尝试使用引导轮播,不幸的是,我得到的图片并不统一。例如,有些是 100x200,doe 是 150x100 等。纵横比不同,字母与横向。我尝试了很多事情,包括在轮播中加载我的每个图像时使用以下辅助函数:
function ScaleImage(srcwidth, srcheight, targetwidth, targetheight, fLetterBox) {
var result = { width: 0, height: 0, fScaleToTargetWidth: true };
if ((srcwidth <= 0) || (srcheight <= 0) || (targetwidth <= 0) || (targetheight <= 0)) {
return result;
}
// scale to the target width
var scaleX1 = targetwidth;
var scaleY1 = (srcheight * targetwidth) / srcwidth;
// scale to the target height
var scaleX2 = (srcwidth * targetheight) / srcheight;
var scaleY2 = targetheight;
// now figure out which one we should use
var fScaleOnWidth = (scaleX2 > targetwidth);
if (fScaleOnWidth) {
fScaleOnWidth = fLetterBox;
}
else {
fScaleOnWidth = !fLetterBox;
}
if (fScaleOnWidth) {
result.width = Math.floor(scaleX1);
result.height = Math.floor(scaleY1);
result.fScaleToTargetWidth = true;
}
else {
result.width = Math.floor(scaleX2);
result.height = Math.floor(scaleY2);
result.fScaleToTargetWidth = false;
}
result.targetleft = Math.floor((targetwidth - result.width) / 2);
result.targettop = Math.floor((targetheight - result.height) / 2);
return result;
}
function OnImageLoad(evt) {
var img = evt.currentTarget;
// what's the size of this image and it's parent
var w = $(img).prop('naturalWidth');
var h = $(img).prop('naturalHeight');
//var tw = $(img).parent().width();
//var th = $(img).parent().height();
var tw = $(img).parent().parent().parent().parent().width();
var th = $(img).parent().parent().parent().parent().height();
// compute the new size and offsets
var result = ScaleImage(w, h, tw, th, true);
// adjust the image coordinates and size
img.width = result.width;
img.height = result.height;
$(img).css("left", result.targetleft);
$(img).css("top", result.targettop);
}
并为轮播的每个图像使用以下内容
<img src="~/Images/Img1_Tall.jpg" alt="Tall" id="firstImage" onload="OnImageLoad(event);" />
对于轮播中的第一张图片效果很好,但之后的每一张图片似乎都只是自然大小并且水平居中但正好靠在轮播的顶部边界。
我什至更改了“onload”以传递图像的长度和宽度的值,但这也不起作用,在调试中似乎只有第一个图像启动了“onload”事件。
我想要的效果是,如果容器的比例是 3:4,图像的比例是 1:2,图像会拉伸到左右边缘并垂直居中并在上方有字母框及以下,但容器不会改变,因此轮播的导航按钮不会移动。如果图像是 2:1,图像将拉伸到水平居中的顶部和底部,左右两侧有信箱,同样保持导航按钮不动。
任何帮助将不胜感激...包括:
你正在尝试做的事情很疯狂
【问题讨论】:
标签: css twitter-bootstrap carousel