基于jquery实现图片相关操作(重绘、获取尺寸、调整大小、缩放)


Posted in Javascript onDecember 25, 2015

本文为大家分享了四个jquery图片常见操作,供大家参考,具体内容如下

1、关于图片大小的重绘,你可以在服务端来实现,也可以通过JQuery在客户端实现。

$(window).bind("load", function() {
   // IMAGE RESIZE
   $('#product_cat_list img').each(function() {
     var maxWidth = 120;
     var maxHeight = 120;
     var ratio = 0;
     var width = $(this).width();
     var height = $(this).height();
 
     if(width > maxWidth){
      ratio = maxWidth / width;
      $(this).css("width", maxWidth);
      $(this).css("height", height * ratio);
      height = height * ratio;
     }
     var width = $(this).width();
     var height = $(this).height();
     if(height > maxHeight){
      ratio = maxHeight / height;
      $(this).css("height", maxHeight);
      $(this).css("width", width * ratio);
      width = width * ratio;
     }
   });
   //$("#contentpage img").show();
   // IMAGE RESIZE
});

2、jQuery获取<img>图片实际尺寸的方法

$(function(){
 var imgSrc = $("#image").attr("src");
 getImageWidth(imgSrc,function(w,h){
 console.log({width:w,height:h});
 });
});

function getImageWidth(url,callback){
 var img = new Image();
 img.src = url;
 
 // 如果图片被缓存,则直接返回缓存数据
 if(img.complete){
   callback(img.width, img.height);
 }else{
      // 完全加载完毕的事件
   img.onload = function(){
 callback(img.width, img.height);
   }
    }
 
}

3、jquery 自动调整图片大小

$(document).ready(function(){
        $('img').each(function() {  
        var maxWidth =500; // 图片最大宽度  
        var maxHeight =500;  // 图片最大高度  
        var ratio = 0; // 缩放比例 
         var width = $(this).width();  // 图片实际宽度  
         var height = $(this).height(); // 图片实际高度   // 检查图片是否超宽  
         if(width > maxWidth){    
         ratio = maxWidth / width;  // 计算缩放比例    
         $(this).css("width", maxWidth); // 设定实际显示宽度    
         height = height * ratio;  // 计算等比例缩放后的高度    
         $(this).css("height", height); // 设定等比例缩放后的高度  
         }   // 检查图片是否超高 
          if(height > maxHeight){    
          ratio = maxHeight / height; // 计算缩放比例   
          $(this).css("height", maxHeight);  // 设定实际显示高度    
          width = width * ratio;  // 计算等比例缩放后的高度    
          $(this).css("width", width);  // 设定等比例缩放后的高度  
          }});
      });

4、使用jQuery对图片进行大小缩放

$(window).bind("load", function() {
  // IMAGE RESIZE
  $('#product_cat_list img').each(function() {
    var maxWidth = 120;
    var maxHeight = 120;
    var ratio = 0;
    var width = $(this).width();
    var height = $(this).height();
 
    if(width > maxWidth){
      ratio = maxWidth / width;
      $(this).css("width", maxWidth);
      $(this).css("height", height * ratio);
      height = height * ratio;
    }
    var width = $(this).width();
    var height = $(this).height();
    if(height > maxHeight){
      ratio = maxHeight / height;
      $(this).css("height", maxHeight);
      $(this).css("width", width * ratio);
      width = width * ratio;
    }
  });
  //$("#contentpage img").show();
  // IMAGE RESIZE
});

以上就是本文的全部内容,帮助大家实现图片重绘、图片获取尺寸、图片调整大小以及图片缩放,希望大家喜欢。

Javascript 相关文章推荐
javascript 强制刷新页面的实现代码
Dec 13 Javascript
使用Jquery Aajx访问WCF服务(GET、POST、PUT、DELETE)
Mar 16 Javascript
一个简单的弹性返回顶部JS代码实现介绍
Jun 09 Javascript
开发 Internet Explorer 右键功能表(ContextMenu)
Jul 03 Javascript
我的Node.js学习之路(四)--单元测试
Jul 06 Javascript
js鼠标点击图片切换效果实现代码
Nov 19 Javascript
概述如何实现一个简单的浏览器端js模块加载器
Dec 07 Javascript
jQuery实现表格奇偶行显示不同背景色 就这么简单
Mar 13 Javascript
AngularJS实现进度条功能示例
Jul 05 Javascript
详解vscode中vue代码颜色插件
Oct 11 Javascript
React父子组件间的传值的方法
Nov 13 Javascript
基于vue实现简易打地鼠游戏
Aug 21 Javascript
使用堆实现Top K算法(JS实现)
Dec 25 #Javascript
原生js和jQuery实现淡入淡出轮播效果
Dec 25 #Javascript
jQuery实现模仿微博下拉滚动条加载数据效果
Dec 25 #Javascript
尝试动手制作javascript放大镜效果
Dec 25 #Javascript
js操作cookie保存浏览记录的方法
Dec 25 #Javascript
js实现跨域的多种方法
Dec 25 #Javascript
jquery.cookie.js用法实例详解
Dec 25 #Javascript
You might like
php简单开启gzip压缩方法(zlib.output_compression)
2013/04/13 PHP
php绘图中显示不出图片的原因及解决
2014/03/05 PHP
php实现留言板功能(代码详解)
2017/03/28 PHP
PHP下 Mongodb 连接远程数据库的实例代码
2017/08/30 PHP
LaravelS通过Swoole加速Laravel/Lumen详解
2018/03/02 PHP
thinkPHP5.0框架事务处理操作简单示例
2018/09/07 PHP
JavaScript 给汉字排序实例代码
2008/06/28 Javascript
JavaScript高级程序设计 阅读笔记(十七) js事件
2012/08/14 Javascript
javascript五图轮播切换实用版
2012/08/17 Javascript
jquery中常用的SET和GET$(”#msg”).html循环介绍
2013/10/09 Javascript
Nodejs中自定义事件实例
2014/06/20 NodeJs
javascript用函数实现对象的方法
2015/05/14 Javascript
深入理解Javascript中的自执行匿名函数
2016/06/03 Javascript
jQuery用noConflict代替$的实现方法
2017/04/12 jQuery
angular bootstrap timepicker TypeError提示怎么办
2017/06/13 Javascript
浅谈vue的iview列表table render函数设置DOM属性值的方法
2017/09/30 Javascript
微信小程序排坑指南详解
2018/05/23 Javascript
微信小程序如何实现精确的日期时间选择器
2020/01/21 Javascript
jquery实现鼠标悬浮弹出气泡提示框
2020/12/23 jQuery
python字符串加密解密的三种方法分享(base64 win32com)
2014/01/19 Python
Python3 批量扫描端口的例子
2019/07/25 Python
Pycharm添加虚拟解释器报错问题解决方案
2020/10/13 Python
pycharm配置安装autopep8自动规范代码的实现
2021/03/02 Python
canvas实现按住鼠标移动绘制出轨迹的示例代码
2018/02/05 HTML / CSS
日本7net购物网:书籍、漫画、杂志、DVD、游戏邮购
2017/02/17 全球购物
美国奢侈品在线团购网站:Gilt City
2017/11/16 全球购物
Nike德国官网:Nike.com (DE)
2018/11/13 全球购物
莱德杯高尔夫欧洲官方商店:Ryder Cup Shop
2019/08/14 全球购物
Ticketmaster意大利:音乐会、节日、艺术和剧院的官方门票
2019/12/23 全球购物
医学生自我鉴定范文
2013/11/08 职场文书
幸福家庭事迹材料
2014/02/03 职场文书
电台编导求职信
2014/05/06 职场文书
介绍信怎么写
2015/01/30 职场文书
幼儿园感恩节活动总结
2015/03/24 职场文书
2016教师六五普法学习心得体会
2016/01/21 职场文书
python爬取某网站原图作为壁纸
2021/06/02 Python