基于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 相关文章推荐
JSQL SQLProxy 的 php 版本代码
May 05 Javascript
JavaScript 面向对象编程(1) 基础
May 18 Javascript
jquery实现漂浮在网页右侧的qq在线客服插件示例
May 13 Javascript
js仿百度有啊通栏展示效果实现代码
May 28 Javascript
解析javascript 浏览器关闭事件
Jul 08 Javascript
ajax跨域调用webservice的实现代码
May 09 Javascript
Jquery表单验证失败后不提交的解决方法
Oct 18 Javascript
功能强大的jquery.validate表单验证插件
Nov 07 Javascript
关于jQuery EasyUI 中刷新Tab选项卡后一个页面变形的解决方法
Mar 02 Javascript
javascript 中事件冒泡和事件捕获机制的详解
Sep 01 Javascript
JS实现分页浏览横向图片(类轮播)实例代码
Nov 06 Javascript
小程序实现左滑删除功能
Oct 30 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 mysql索引问题
2008/06/07 PHP
PHP实现的购物车类实例
2015/06/17 PHP
php文档工具PHP Documentor安装与使用方法
2016/01/25 PHP
PHP chop()函数讲解
2019/02/11 PHP
PHP单元测试配置与使用方法详解
2019/12/27 PHP
基于jquery的一个图片hover的插件
2010/04/24 Javascript
JSON语法五大要素图文介绍
2012/12/04 Javascript
js定时器(执行一次、重复执行)
2014/03/07 Javascript
基于jQuery实现文本框缩放以及上下移动功能
2014/11/24 Javascript
轻松学习jQuery插件EasyUI EasyUI实现拖放商品放置购物车
2015/11/30 Javascript
莱鸟介绍window.print()方法
2016/01/06 Javascript
jQuery 限制输入字符串长度
2016/06/20 Javascript
JSONP跨域请求
2017/03/02 Javascript
对存在JavaScript隐式类型转换的四种情况的总结(必看篇)
2017/08/31 Javascript
vue.js过滤器+ajax实现事件监听及后台php数据交互实例
2018/05/22 Javascript
JS实现方形抽奖效果
2018/08/27 Javascript
微信小程序3种位置API的使用方法详解
2019/08/05 Javascript
vue悬浮可拖拽悬浮按钮的实例代码
2019/08/20 Javascript
Vue中computed及watch区别实例解析
2020/08/01 Javascript
微信小程序onShareTimeline()实现分享朋友圈
2021/01/07 Javascript
Python中处理unchecked未捕获异常实例
2015/01/17 Python
Python matplotlib绘图可视化知识点整理(小结)
2018/03/16 Python
python如何拆分含有多种分隔符的字符串
2018/03/20 Python
简单了解python关系(比较)运算符
2019/07/08 Python
利用Python检测URL状态
2019/07/31 Python
用opencv给图片换背景色的示例代码
2020/07/08 Python
如何在VSCode下使用Jupyter的教程详解
2020/07/13 Python
python使用隐式循环快速求和的实现示例
2020/09/11 Python
Python之字符串的遍历的4种方式
2020/12/08 Python
CSS3 实现时间轴动画
2020/11/25 HTML / CSS
英国和世界各地预订便宜的酒店:LateRooms.com
2019/05/05 全球购物
银行内勤岗位职责
2014/04/09 职场文书
行政秘书工作自我鉴定
2014/09/15 职场文书
2015年少先队活动总结
2015/03/25 职场文书
化工厂员工工作总结
2015/10/15 职场文书
Python 读取千万级数据自动写入 MySQL 数据库
2022/06/28 Python