使用 HTML 输出标记查看可缩放图像并分配 src
Working with HTML output tag to view scalable image and assign src
我有以下代码,它使用 HTML 的输出属性返回图像。 我希望能够以最大宽度和最大高度缩放/编辑此图像。我还想为此映像分配一个 SRC。
我的网页:
<input type="file" id="files" name="files[]" file-model="myFile"/>
<output id="list" class="resize-image" alt="Image"></output>
我的JS:
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 height="35" width="75" 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);
所以如果你真的想在这种情况下使用css
属性,你必须使用style
属性内联注入它们
var span = document.createElement('span');
span.innerHTML = ['<img height="35" width="75" class="thumb" src="',
e.target.result,
'style="max-height:25px; max-width:25px;"', // You can manipulate that dynamically using
'" title="',
escape(theFile.name),
'"/>'].join('');
document.getElementById('list').insertBefore(span, null);
};
检查这个动态 css 操作:http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_css_setcolor
为什么不在你的css中.thumb{max-height:35px;max-width:75px;}
,当你创建新元素时,设置class属性,css将应用于该元素。另外,为什么要创建一个范围,然后将内部HTML设置为图像?为什么不直接创建映像?
.CSS
.thumb{max-height:35px;max-width:75px;}
创建镜像
var Img = document.createElement('img');
// .setAttribute(AttributeType, Attribute Value);
Img.setAttribute('class','thumb'); //Class Attribute
Img.setAttribute('src',e.target.result);//Src Attribute
document.getElementById('list').insertBefore(Img, null);
我希望这有所帮助。祝您编码愉快!
相关文章:
- 图像缩放和平移
- Phonegap IOS应用程序图像缩放多点触控
- JavaScript 图像缩放和平移(拖动)
- 画布绘制图像缩放
- 使用多层图像缩放产品图像
- 为jquery图像缩放插件定义一个选择器
- 图像缩放效果
- 可以使全屏图像缩放
- 如何在jQuery/javascript中复制这种图像缩放动画效果
- JavaScript图像缩放-圆形边框
- Jquery自定义旋转木马内的图像缩放模式
- 防止移动浏览器上的图像缩放
- 通过javascript禁用Firefox图像缩放
- 图像缩放只在点击缩略图后在灯箱中工作
- 交换图像和显示图像缩放
- 将背景图像缩放为其容器宽度的100%
- 像素化的跨浏览器图像缩放
- 是否有一个好的jQuery (Javascript)库来做快速视频(不是图像)缩放/平移?(目标:iPhone通过Pho
- 比例图像缩放
- 图像缩放+模态