有没有办法在使用 html 输出标签时控制图像的大小
Is there a way to control the size of an image when using html output tag?
我使用以下代码上传图像以供查看,然后再将其传递到我的服务器。 我想使用 css 对图像的大小进行约束。 CSS 对图像没有影响,因为它是通过输出呈现的。有没有办法解决这个问题?我尝试将 CSS 属性添加到其周围的div 中,但这也不起作用。
该 HTML:
<input type="file" id="files" name="files[]" file-model="myFile"/>
<output id="list" class="resize-image" alt="Image"></output>
CSS:
#list{
max-width: 15px;
max-height: 7px;
}
Javascript:
var handleFileSelect = function (evt) {//already set up for multiple files; not what i want to do.
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Render thumbnail.
var span = document.createElement('span');
span.innerHTML = ['<img class="thumb" src="', e.target.result,
'" title="', escape(theFile.name), '"/>'].join('');
document.getElementById('list').insertBefore(span, null);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
简单的方法是使用height
和width
img
标签属性。
var span = document.createElement('span');
span.innerHTML = ['<img height="7" width="15" class="thumb" src="', e.target.result,
'" title="', escape(theFile.name), '"/>'].join('');
document.getElementById('list').insertBefore(span, null);
如果您确实想使用此任务.css
可以使用background-size
,但是必须使用css
中的图像的 url 设置background
属性。
您也可以简单地覆盖css
中的img
标签,如下所示:
img{
max-width: 100px;
max-height: 100px;
}
在这里你可以测试这两种方法: http://www.w3schools.com/cssref/tryit.asp?filename=trycss3_background-size
相关文章:
- 使用jQuery控制来自图像的音频
- 有没有办法在使用 html 输出标签时控制图像的大小
- 如何在“控制缩放”下的“打开图层”上添加一些图像
- 使用左右控制循环图像
- Google Map JS API-加载地图分幅,但所有图像(标记、缩放控制等)都不加载;t负载2分钟
- FabricJS将图像添加到组会导致奇怪的控制行为
- 输入类型='文件'如何控制图像质量
- 具有速度控制的图像动画
- 控制首先加载哪个图像
- 如何控制图像's大小
- 我应该在什么时候用CSS和Javascript控制背景图像的变化
- 可点击图片阵列图像输出大小-can't控制各个输出图片的尺寸
- 如何在用户另存为时控制base64图像
- 我如何控制图像预加载请求
- 改变默认谷歌地图Api'的默认缩放控制图像
- 当我不能修改一些源代码时,我如何控制图像加载?
- 根据以前访问过的页面历史来控制图像
- 上传到ASP.Net时控制图像文件大小
- 如何控制图像的缩放
- 控制图像的宽度和高度时,上传图像