在输入文件中更改DIV背景为jpg
Change DIV background to jpg in input file
当我上传文件时,它会更新IMG SRC。我想用我从输入文件中上传的图像更新#gal01的背景。下面是代码:
HTML:<img id="mlogo" src="#" alt=" " />
<br>
<div id="gal01">
<input type="file" name="fileToUpload" accept="image/jpeg" class="fileInput" id="fileToUpload" onchange="readURL(this);" />
</div>
我有一个带有透明输入文件的Div。
JavaScript:try {function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#mlogo')
.attr('src', e.target.result)
.width(95)
.height(95);
};
reader.readAsDataURL(input.files[0]);
}
}
} catch (error) { throw error; }
CSS: img {
width: 95px;
height: 95px;
}
#gal01 {
width: 95px;
height: 95px;
overflow: hidden;
position: relative;
cursor: pointer;
background-color: #F00;
background: url("http://www.dari.com/images/galimg.png") no-repeat scroll 0px 0px rgba(0, 0, 0, 0);
}
.fileInput {
cursor: pointer;
height: 100%;
position:absolute;
top: 0;
right: 0;
font-size:50px;
opacity: 0;
-moz-opacity: 0;
filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0)
}
谢谢你的帮助!
JSFiddle
为你的小提琴制作了一个新版本:
http://jsfiddle.net/se0kssLf/3/你需要设置div的背景:
$('#gal01').attr('style', 'background: url('+e.target.result+');background-size: 95px 95px;');
找到$('#gal01')
并将其css background
属性设置为e.target.result
:
$('#gal01')
.css('background', 'url('+e.target.result+') no-repeat scroll 0px 0px rgba(0, 0, 0, 0)')
.css( 'background-size', 'cover');
inside reader.onload = function (e) {...
工作小提琴:http://jsfiddle.net/se0kssLf/2/
相关文章:
- 如何设置DIV背景图像css jquery
- 根据其宽度更改DIV背景颜色
- Div背景图像打印空白
- 链路翻转时的 DIV 背景图像交换
- 将功能元素 IMG 更改为 DIV 背景
- JS - 使用变量设置 Div 背景颜色
- 从我的 Div 背景图像属性中循环 3 个图像
- 更改中继器DIV背景
- jQuery 使用 css() 更改 DIV 背景
- AJAX/jQuery:根据其中的数字值更改 DIV 背景
- Mousover 父 DIV 用于更改子 DIV 背景
- 通过点击更改日期字符串并更改 DIV 背景颜色
- 更改表单中所有输入的 DIV 背景颜色
- 使用jQuery交换Div背景图像
- Div背景更改OnClick
- JavaScript改变DIV背景使用ID
- 更改DIV背景颜色
- 如何移动/动画DIV背景图像平滑垂直
- 要剪切Div背景的掩码的文本
- 在输入文件中更改DIV背景为jpg