最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于jquery實(shí)現(xiàn)圖片相關(guān)操作(重繪、獲取尺寸、調(diào)整大小、縮放)

 更新時(shí)間:2015年12月25日 11:37:08   投稿:lijiao  
這篇文章主要介紹了基于jquery實(shí)現(xiàn)圖片相關(guān)操作,包括圖片重繪、圖片獲取尺寸、圖片調(diào)整大小、圖片縮放,感興趣的小伙伴們可以參考一下

本文為大家分享了四個(gè)jquery圖片常見操作,供大家參考,具體內(nèi)容如下

1、關(guān)于圖片大小的重繪,你可以在服務(wù)端來實(shí)現(xiàn),也可以通過JQuery在客戶端實(shí)現(xiàn)。

$(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>圖片實(shí)際尺寸的方法

$(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;
 
 // 如果圖片被緩存,則直接返回緩存數(shù)據(jù)
 if(img.complete){
   callback(img.width, img.height);
 }else{
      // 完全加載完畢的事件
   img.onload = function(){
 callback(img.width, img.height);
   }
    }
 
}

3、jquery 自動(dòng)調(diào)整圖片大小

$(document).ready(function(){
        $('img').each(function() {  
        var maxWidth =500; // 圖片最大寬度  
        var maxHeight =500;  // 圖片最大高度  
        var ratio = 0; // 縮放比例 
         var width = $(this).width();  // 圖片實(shí)際寬度  
         var height = $(this).height(); // 圖片實(shí)際高度   // 檢查圖片是否超寬  
         if(width > maxWidth){    
         ratio = maxWidth / width;  // 計(jì)算縮放比例    
         $(this).css("width", maxWidth); // 設(shè)定實(shí)際顯示寬度    
         height = height * ratio;  // 計(jì)算等比例縮放后的高度    
         $(this).css("height", height); // 設(shè)定等比例縮放后的高度  
         }   // 檢查圖片是否超高 
          if(height > maxHeight){    
          ratio = maxHeight / height; // 計(jì)算縮放比例   
          $(this).css("height", maxHeight);  // 設(shè)定實(shí)際顯示高度    
          width = width * ratio;  // 計(jì)算等比例縮放后的高度    
          $(this).css("width", width);  // 設(shè)定等比例縮放后的高度  
          }});
      });

4、使用jQuery對(duì)圖片進(jìn)行大小縮放

$(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
});

以上就是本文的全部?jī)?nèi)容,幫助大家實(shí)現(xiàn)圖片重繪、圖片獲取尺寸、圖片調(diào)整大小以及圖片縮放,希望大家喜歡。

相關(guān)文章

最新評(píng)論

宕昌县| 金堂县| 灵丘县| 建平县| 张家界市| 顺义区| 来安县| 浠水县| 平昌县| 兴海县| 阿城市| 潮州市| 元谋县| 绿春县| 西昌市| 泌阳县| 西华县| 泰顺县| 临清市| 清流县| 龙口市| 大连市| 锦屏县| 梁平县| 赣榆县| 贵德县| 岳西县| 普兰店市| 吉木乃县| 咸丰县| 五原县| 金堂县| 临漳县| 修水县| 积石山| 资源县| 石泉县| 武义县| 临桂县| 东方市| 察哈|