当前位置:网站首页>图片的大小限制显示
图片的大小限制显示
2022-07-17 05:09:00 【IChocolateKapa】
<span style="font-size:24px;">function setImgWidthHeight(imgJqObj){
imgJqObj.each(function() {
var $thisImg = $(this);
console.log($thisImg.height());
console.log($thisImg.width());
var maxWidth = 500; // 图片最大宽度
var maxHeight = 500; // 图片最大高度
$("<img/>").attr("src", $thisImg.attr("src")).load(function() {
var realWidth = this.width;
var realHeight = this.height;
if(realWidth >= maxWidth){
$thisImg.attr("width", maxWidth);
// $thisImg.attr("height", maxHeight);
}
if(realHeight >= maxHeight){
// $thisImg.attr("width", maxWidth);
$thisImg.attr("height", maxHeight);
}
})
})
}</span>在html页面中,<img>标签中如果没有设置width和height属性,就拿不到
<$thisImg.height() 和 $thisImg.width(),[结果是0],要是设置了的话就拿到的是设置的值。要根据图片实际大小来设置高宽,过大显示最大设定值,过小就按本身值显示,所以要拿到图片真实的宽和高:
$("<img/>").attr("src", $thisImg.attr("src")).load(function() {
var realWidth = this.width;
var realHeight = this.height;
}) //必须用load方法边栏推荐
- [bug solution] org apache. ibatis. type. TypeException: The alias ‘xxxx‘ is already mapped to the value ‘xxx‘
- Face scum counter attack: thread pool lethal serial eighteen questions, the interviewer praised me straight
- Minor problems of GCC compiling C language in ubantu
- redis 源码分析 跳表实现
- gradle
- Beginner's Guide to learning penetration testing
- The latest news of spring recruitment in 2022: the average salary of it Internet industry is 18500 yuan
- 操作系统常见面试题
- Performance bottleneck finding - Flame graph analysis
- ArcMap creates a constant grid and tessellates it into a new grid
猜你喜欢
随机推荐
Use of log4j
函数与参数
特殊指针的一些应用
MySQL学习笔记(5)——JOIN联表查询,自连接查询,分页和排序,子查询与嵌套查询
H5 how to obtain intranet IP and public IP
共用(联合)体
东软跨境电商数仓开发进度
MySQL cache solution problem solving
Rxjs source code analysis (I) observable
Implementation of synchronization interface of 6 libcurl based on libco
SQL练习题集合
Redis source code analysis - data structure and Implementation (Dictionary dict)
MySQL--触发器与视图
用Flink SQL流化市场数据2:盘中风险价值
Swagger configuration and use
redis 源码分析3 间断遍历的实现
Network command: network card information, netstat, ARP
线程池如何监控,才能帮助开发者快速定位线上错误?
Use Flink SQL to transfer market data 1: transfer VWAP
Pointer function of C language









