在将图像传递到我的服务器之前,我使用下面的代码上传图像以供查看。我想使用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);发布于 2015-03-14 03:54:10
简单用法是使用height width img 和widthimg标记属性.。
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,但必须将background属性设置为css中的图像的url。
您还可以简单地在img css 中重写标记,如:
img{
max-width: 100px;
max-height: 100px;
}在这里您可以测试这两种方法:background-size
https://stackoverflow.com/questions/29045024
复制相似问题