图片自动缩放,前台读富文本编辑器里图片用的
2016-07-06 14:03
260 查看
//#region 图片自动缩放,前台读富文本编辑器里图片用的
/*
使用方法:
一、
<div class="divaaa">
@Html.Raw(ViewBag.Model.NewsContent)
</div>
二、
<script type="text/javascript">
window.onload = function () {
UnLoadImg($(".divaaa"));
}
</script>
*/
function UnLoadImg(obj) {
$obj = $(obj);
var $img = $obj.find("img")
$img.each(function () {
if ($(this).parents("a").length == 0) {
$(this).css({ "cursor": "pointer" });
$(this).click(function () {
window.open($(this).attr("src"));
});
}
});
var maxWidth = $obj.width();
for (var i = 0; i < $img.length; i++) {
if ($img.eq(i).width() > maxWidth) {
$img.eq(i).css({ width: maxWidth + "px", height: AutoResizeImage(maxWidth, 0, $img.eq(i)) + "px" });
}
}
}
function AutoResizeImage_H(maxWidth, maxHeight, objImg) {
var img = objImg;
var hRatio;
var wRatio;
var Ratio = 1;
var w = img.width();
var h = img.height();
wRatio = maxWidth / w;
hRatio = maxHeight / h;
if (maxWidth == 0 && maxHeight == 0) {
Ratio = 1;
} else if (maxWidth == 0) {
if (hRatio < 1) Ratio = hRatio;
} else if (maxHeight == 0) {
if (wRatio < 1) Ratio = wRatio;
} else if (wRatio < 1 || hRatio < 1) {
Ratio = (wRatio <= hRatio ? wRatio : hRatio);
}
if (Ratio < 1) {
w = w * Ratio;
h = h * Ratio;
}
return h;
}
//#endregion
/*
使用方法:
一、
<div class="divaaa">
@Html.Raw(ViewBag.Model.NewsContent)
</div>
二、
<script type="text/javascript">
window.onload = function () {
UnLoadImg($(".divaaa"));
}
</script>
*/
function UnLoadImg(obj) {
$obj = $(obj);
var $img = $obj.find("img")
$img.each(function () {
if ($(this).parents("a").length == 0) {
$(this).css({ "cursor": "pointer" });
$(this).click(function () {
window.open($(this).attr("src"));
});
}
});
var maxWidth = $obj.width();
for (var i = 0; i < $img.length; i++) {
if ($img.eq(i).width() > maxWidth) {
$img.eq(i).css({ width: maxWidth + "px", height: AutoResizeImage(maxWidth, 0, $img.eq(i)) + "px" });
}
}
}
function AutoResizeImage_H(maxWidth, maxHeight, objImg) {
var img = objImg;
var hRatio;
var wRatio;
var Ratio = 1;
var w = img.width();
var h = img.height();
wRatio = maxWidth / w;
hRatio = maxHeight / h;
if (maxWidth == 0 && maxHeight == 0) {
Ratio = 1;
} else if (maxWidth == 0) {
if (hRatio < 1) Ratio = hRatio;
} else if (maxHeight == 0) {
if (wRatio < 1) Ratio = wRatio;
} else if (wRatio < 1 || hRatio < 1) {
Ratio = (wRatio <= hRatio ? wRatio : hRatio);
}
if (Ratio < 1) {
w = w * Ratio;
h = h * Ratio;
}
return h;
}
//#endregion
相关文章推荐
- Qt之qInstallMessageHandler(重定向至文件)
- python break,pass,continue 循环语句
- Qt之qInstallMessageHandler(重定向至文件)
- ANR log分析
- 布隆过滤器
- 使用jdbc的基本步骤
- 处理大并发之二 对epoll的理解,epoll客户端服务端代码
- @OneToMany或@ManyToOne的用法-annotation关系映射篇(上)
- java jvm 参数 -Xms -Xmx -Xmn -Xss 调优总结
- ListView Item多种布局优化
- Linux后台进程管理
- 怎样用ZBrush雕刻人体造型
- 腾讯管家与本地运行的jquery冲突,影响页面请求速度
- 三星将提升资本支出 维持手机业务复兴势头
- docker小用有感
- Java类型限界
- python for 循环
- 百度搜索Spider3.0对于网站优化有什么利好
- 解决QCombobox调用clear()崩溃的问题
- OZ工具安装使用