首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使用html输出标记时,有控制图像大小的方法吗?

在使用html输出标记时,有控制图像大小的方法吗?
EN

Stack Overflow用户
提问于 2015-03-14 03:12:49
回答 1查看 1.4K关注 0票数 0

在将图像传递到我的服务器之前,我使用下面的代码上传图像以供查看。我想使用css来限制图像的大小。CSS对图像没有影响,因为它是由输出呈现的。有办法绕道吗?我尝试将CSS属性添加到其周围的div中,但这也不起作用。

HTML:

代码语言:javascript
复制
  <input type="file" id="files" name="files[]" file-model="myFile"/>
  <output id="list" class="resize-image" alt="Image"></output>

CSS:

代码语言:javascript
复制
#list{
  max-width: 15px;
  max-height: 7px;
}

Javascript:

代码语言: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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-14 03:54:10

简单用法是使用height width img widthimg标记属性.

代码语言:javascript
复制
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 中重写标记,如:

代码语言:javascript
复制
img{
  max-width: 100px;
  max-height: 100px;
}

在这里您可以测试这两种方法:background-size

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29045024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档