简体   繁体   English

使用外部图像设置div的宽度和高度?

[英]Set div width and height using an external image?

I'm trying to get the width and height of an external image and use the width and height to set a Div's width and height in my page. 我正在尝试获取外部图像的宽度和高度,并使用该宽度和高度在页面中设置Div的宽度和高度。

I can easily get the image width and height but I cannot use the width/height for my div for some strange reason! 我可以轻松获得图像的宽度和高度,但是由于某些奇怪的原因,我无法为我的div使用宽度/高度!

To explain this better, I've created this fiddle: 为了更好地解释这一点,我创建了这个小提琴:

https://jsfiddle.net/qtc3q6sd/2/ https://jsfiddle.net/qtc3q6sd/2/

And this is my entire code: 这是我的整个代码:

var img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.width);
  $("#mydiv").css("width", $(this.width()));
  $("#mydiv").css("height", $(this.height()));
}
img.src = 'https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';

could someone please advise on this issue? 有人可以建议这个问题吗?

Thanks in advance. 提前致谢。

You are getting the width and the height the wrong way. 您以错误的方式获取宽度和高度。

var img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.height);
  $("#droppable").css("width", this.width);
  $("#droppable").css("height", this.height);
}
img.src = 'https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';

See jsfiddle . 参见jsfiddle

Nothing wrong with the logic of your code. 代码的逻辑没有错。 just a syntax error. 只是语法错误。

it should be $(this).width() instead of $(this.width()) 应该是$(this).width()而不是$(this.width())

var img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.width);
  $("#mydiv").css("width", $(this).width());
  $("#mydiv").css("height", $(this).height());
}
img.src = 'https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';

Syntax is just a bit off, 语法只是一点点,

try this 尝试这个

var img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.height);
  $("#mydiv").css("width", this.width);
  $("#mydiv").css("height", this.height);
}
img.src = 'https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';

Here is your solution. 这是您的解决方案。 Inside your callback function, this is the image 在您的回调函数中, this是图像

var img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.height);
  $("#mydiv").css("width", this.width + 'px');
  $("#mydiv").css("height", this.height + 'px');
}
img.src =   'https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';

Your code has two mistakes. 您的代码有两个错误。

First, inside the onload function 'this' represents the image object. 首先,在onload函数中,“ this”代表图像对象。 You're trying to call the with() and height() functions of the image, but they're not functions, they are properties. 您试图调用图像的with()和height()函数,但它们不是函数,而是属性。 You must lose the '()'. 您必须丢失'()'。

Second, when you try to do this $(this.width()) you're encapsulating the width value in a jQuery object. 其次,当您尝试执行$(this.width())$(this.width()) width值封装在jQuery对象中。 You don't need that, just use the width property this.width . 您不需要它,只需使用width属性this.width

The code below will work: 下面的代码将起作用:

var img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.width);
  $("#mydiv").css("width", this.width);
  $("#mydiv").css("height", this.height);
}
img.src = 'https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM