jquery 可调整大小的图像分辨率问题
以下代码在单击按钮时创建图像。您可以看到在 firebug DOM 中创建的图像。但高度和宽度都是0px。未检测到图像尺寸。
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/themes/base/jquery-ui.css" type="text/css" media="all">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript">
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js" type="text/javascript">
</script>
<button style="width:100px; height:30px;">test</button>
<p style="height:400px; width:500px;border:2px solid red;"></p>
<script type="text/javascript">
var spanid = 1;
$("button").click(function() {
var elm = $('<img id=spanId' + spanid + ' src="http://www.carsyouwilldrive.com/wp-content/uploads/2009/06/futurecar1.jpg"/>');
elm.resizable().parent().draggable({
cursor: "move"
}).appendTo('p');
spanid++;
});</script>
The below code creates an image when the button is clicked on. You can see the image created in firebug DOM. But height and width is 0px. Image dimensions is not being detected.
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/themes/base/jquery-ui.css" type="text/css" media="all">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript">
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js" type="text/javascript">
</script>
<button style="width:100px; height:30px;">test</button>
<p style="height:400px; width:500px;border:2px solid red;"></p>
<script type="text/javascript">
var spanid = 1;
$("button").click(function() {
var elm = $('<img id=spanId' + spanid + ' src="http://www.carsyouwilldrive.com/wp-content/uploads/2009/06/futurecar1.jpg"/>');
elm.resizable().parent().draggable({
cursor: "move"
}).appendTo('p');
spanid++;
});</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为
.ressized()
根据目标计算初始大小。在你的例子中,你在将图像附加到 DOM 之前先调整图像的大小,因此图像的大小未知,因为它尚未加载,这会导致大小为 0。如果你附加图像在调整其大小之前,它应该可以正常工作。像这样:
另外,我建议您禁用浏览器默认的图像拖动和选择功能。您可以通过将
.disableSelection()
(当前未记录)添加到图像元素来实现此目的。This is because
.resizable()
calculates the initial size based on the target. In your case, you are making the image resizable before appending it to the DOM, and as such the size of the image is not known since it hasn't been loaded yet, which results in a size of 0.If you append the image before making it resizable it should work just fine. Like this:
Also, I recommend you to disable the browsers default image drag and selection. You can do this by adding
.disableSelection()
(currently undocumented) to your image element.对我来说,这段代码是工作的(请参阅 jquery 可调整大小附加图像大小问题,但我在 load() 中也添加了 darggable)
For me work this code (see jquery resizable append image size problem, but I added darggable in load() too)