【问题标题】:How to get actual image width and height after load in colorbox plugin?在 colorbox 插件中加载后如何获取实际的图像宽度和高度?
【发布时间】:2012-01-09 16:23:50
【问题描述】:

我在颜色框中打开一个 MVC 视图。该视图加载了我让用户添加注释的图像。在我的注释插件工作并渲染画布之前,我需要 知道加载图像的宽度和高度。这将设置画布尺寸。

我能够在 FF、Chrome、Opera 中很好地获得图像的宽度和高度。 但在 IE8 中,我总是将图像宽度和高度设为 0

有没有人可以帮助我获得一次图像尺寸 图片加载了吗?

这是我如何调用 colorbox 来打开我的 MVC 视图:

<a href="site/photos/album/1234" class="notes colorbox"></a>

这是视图:

<div id="dvImg">
    <img id="imgAnnotate" src="path/abc.jpg" />
</div>

$(function () {
    $("#imgAnnotate").load(function () {
        Width = this.width;
        Height = this.height;
        alert('W=' + Width + ' H=' + Height); <-- IE shows as W=0 H=0
    });
});

我也尝试了一个 imagesLoaded 插件,但在 colorbox 上不起作用:

$(function () {
    var $dvImg = $('#dvImg');
    function loopImages($images) {
        $images.each(function () {
            alert('h6=' + $(this).height() + ' w6=' + $(this).width()); //FF says 0
            alert('h7=' + this.height + ' w7=' + this.width); //FF OK!!! not IE
        });
    }

    $dvImg.imagesLoaded(function ($images) {
        loopImages($images);
    });
});

另外一点:视图中的$(window).load() 根本没有触发。可能是因为它 是在彩盒中加载的吗?

无论如何,这似乎很难做到!

【问题讨论】:

  • 您的代码在 IE 中对我来说似乎运行良好。检查这个小提琴(在 IE 中):jsfiddle.net/gERZe
  • 代码必须与 COLORBOX jquery 插件配合使用。
  • techfoobar:您是否可以尝试使用 colorbox 插件,看看您的代码是否仍然表现相同?只需在颜色框弹出窗口中加载相同的图像。非常感谢。
  • 当然。我会用 colorbox 试试看,然后回复你。

标签: jquery asp.net-mvc-3 jquery-plugins colorbox


【解决方案1】:

试试这个

var Width = $(this).css("width");
var Height = $(this).css("height");

【讨论】:

  • 这与 $(this).width() 和 $(this).height() 有何不同?
  • 它读取实际的 css 也许它的工作方式是这样的?这只是一个建议,你可以尝试:) 两者都以不同的方式做同样的事情......应该是相同的结果......我会尝试两者,也许这可以解决你的问题......问候
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-24
  • 2012-11-23
  • 2014-06-09
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
相关资源
最近更新 更多