图片/图形

jqthumb 按比例生成图片缩略图插件

阿里云


jqthumb 是一款实用的响应式按比例生成图片缩略图的 jQuery 插件。jqthumb 可以按照用户设定的比例、尺寸、位置等属性来生成新的缩略图,在老的浏览器中它还能够替代 background-size 属性。jqthumb 兼容性超强,可以工作在所有现代浏览器甚至是 IE6+上,jQuery 1.3 以上版本即可运行。它还可以在 Zepto(通过 zepto-data 插件)v1.1.3+上运行。

这个 jquery 插件可以帮助我们按比例生成图片缩略图。大家可能知道在处理缩略图的时候使用 background-size: cover; 可以解决许多棘手问题。但是 background-size: cover; 在 IE6、IE7 和 IE8 下不能正常工作。而该插件正是弥补了这个缺陷。

也想出现在这里?联系我们
创客主机

使用方法

使用以下的简单 html 结构:

  1. <div style="width: 100%; height: 400px;">
  2.     <img src="path/picture.jpg" class="example1" />
  3. </div>
  4. <div style="width: 400px; height: 400px;">
  5.     <img src="path/picture.jpg" class="example2" />
  6. </div>
  7. <button id="kill">Kill</button>
  8. <button id="kill-all">Kill All</button>

在页面中引入 jQuery 和 jqthumb.min.js 文件:

  1. <script type="text/javascript" src="scripts/jquery.min.js"></script>
  2. <script type="text/javascript" src="scripts/jqthumb.min.js"></script>

然后按下面方法调用插件:

  1. <script type="text/javascript">
  2.     $(function(){
  3.         // plugin initialization
  4.         $('img').jqthumb({
  5.             classname  : 'jqthumb',          // class name. DEFUALT IS jqthumb
  6.             width      : '100%',             // new image width after cropping. DEFAULT IS 100px.
  7.             height     : '100%',             // new image height after cropping. DEFAULT IS 100px.
  8.             position   : {
  9.                 x : '50%',                   // x position of the image. DEFAULT is 50%. 50% also means centerize the image.
  10.                 y : '50%'                    // y position of the image. DEFAULT is 50%. 50% also means centerize the image.
  11.             },
  12.             source     : 'src',              // to specify the image source attribute. DEFAULT IS src.
  13.             show       : false,              // TRUE = show immediately after processing. FALSE = do not show it. DEFAULT IS TRUE.
  14.             responsive : 20,                 // used by older browsers only. 0 to disable. DEFAULT IS 20
  15.             zoom       : 1,                  // zoom the output, 2 would double of the actual image size. DEFAULT IS 1
  16.             method     : 'auto',             // 3 methods available: "auto", "modern" and "native". DEFAULT IS auto
  17.             before     : function(oriImage){ // callback before each image starts processing.
  18.                 alert("I'm about to start processing now...");
  19.             },
  20.             after      : function(imgObj){   // callback when each image is cropped.
  21.                 console.log(imgObj);
  22.             },
  23.             done       : function(imgArray){ // callback when all images are cropped.
  24.                 for(i in imgArray){
  25.                     $(imgArray[i]).fadeIn();
  26.                 }
  27.             }
  28.         });
  29.  
  30.         // kill command
  31.         $('#kill').click(function(){
  32.             $('.example1').jqthumb('kill');
  33.         });
  34.  
  35.         // kill all command
  36.         $('#kill').click(function(){
  37.             $.jqthumb('killall');
  38.         });
  39.     });
  40. </script>

可用参数

source:图片的 URL 属性。例如:<img src="path/image.jpg" />的 source 是 src。

  1. $('img').jqthumb({
  2.         source : 'attr-src' // DEFAULT: src
  3.     });

classname:生成的缩略图的 class 名称。当你想使用外部 css 来渲染缩略图时该参数十分有用。

  1. $('img').jqthumb({
  2.         width  : 200,   // DEFAULT: 100
  3.         height : '100%' // DEFAULT: 100
  4.     });

position:通过 X 和 Y 作为关键参数来定义一个对象。y 用于跳转缩略图上下位置,x 用于跳转缩略图的左右位置。注意: position.x 和 position.y 必须在定义的 width 和 height 的范围里面。如果你用百分比来定义 position.x 和 position.y,请确保它们在 0-100%之间。

  1. $('img').jqthumb({
  2.         position: {
  3.             x : 20,   // DEFAULT: '50%'
  4.             y : '30%' // DEFAULT: '50%'
  5.         }
  6.     });

show:是否在处理完成后显示缩略图:

  1. $('img').jqthumb({
  2.         show : false // DEFAULT: true
  3.     });

responsive:该参数只是在浏览器不支持 CSS3 的时候才使用。为了在旧的浏览器上完成响应式效果,该插件在$(window).resize()事件被触发的时候会重新计算。设置为 0 则在旧的浏览器中不使用响应式效果。在现代浏览器中不支持禁用响应式特性,可以使用 method :"native"来禁止它。

  1. /* responsive only works for native method / older browsers */
  2.     $('img').jqthumb({
  3.         responsive : 10 // DEFAULT: 20
  4.     });
  5.  
  6.     /* to disable responsive feature in modern method / browsers, switch method to native */
  7.     $('img').jqthumb({
  8.         method     : 'native', // DEFAULT: auto
  9.         responsive : 0         // DEFAULT: 20
  10.     });

zoom:放大或缩小缩略图:

  1. $('img').jqthumb({
  2.         zoom : 3 // DEFAULT: 1
  3.     });

method:该按比例是否缩略图插件提供两种方法:一种使在浏览器支持 CSS3 的时候使用,一种是浏览器不支持 CSS3 的时候使用。有时候你可能需要切换这两种方法来做些测试。默认情况下,该插件会自动检测浏览器是否支持 CSS3 然后调用相应的方法。

  1. $('img').jqthumb({
  2.         method : 'native' // Availability: "auto", "modern", "native". DEFAULT: auto
  3.     });

before:这是在计算开始前的一个回调函数。该函数以参数的形式返回原始图片的 source 和对象。如果你在初始化的时候使用了多个对象 class 名称,那么这个函数会被调用两次。

  1. $('img').jqthumb({
  2.         before : function(originalImage){
  3.             console.log(originalImage);
  4.         }
  5.     });

after:这是在计算结束后的一个回调函数。该函数以参数的形式返回新生成的缩略图对象。如果你在初始化的时候使用了多个对象 class 名称,那么这个函数会被调用两次。

  1. $('img').jqthumb({
  2.         after : function(newThumb){
  3.             $(newThumb).fadeIn();
  4.         }
  5.     });

done:这是在所有图片对象都被处理完毕后的一个回调函数。它返回所有缩略图的数组对象。

  1. $('img').jqthumb({
  2.         done : function(thumbnails){
  3.             for(i in thumbnails)
  4.                 $(thumbnails[i]).fadeIn();
  5.         }
  6.     });

可用命令

  1. $('img').jqthumb('kill'); // destroy the plugin
  2. $.jqthumb('killall');     // destroy all generated thumbnails on the page

使用方法

  1. ...
  2. <img src="path/image.jpg" />
  3. ...
  4. <script type="text/javascript">
  5.     $(function(){
  6.         $('img').jqthumb({
  7.             width  : 300,
  8.             height : 200
  9.         });
  10.     });
  11. </script>
  1. ...
  2. <div data-jqthumb-src="path/image.jpg"></div>
  3. ...
  4. <script type="text/javascript">
  5.     $(function(){
  6.         $('div').jqthumb({
  7.             source : 'data-jqthumb-src'
  8.         });
  9.     });
  10. </script>
  1. ...
  2. <div style="width: 100%; height:500px;">
  3.     <img src="path/image.png" />
  4. </div>
  5. ...
  6. <script type="text/javascript">
  7.     $(function(){
  8.         $('div').jqthumb({
  9.             width  : '100%',
  10.             height : '100%'
  11.         });
  12.     });
  13. </script>
  1. ...
  2. <img class="my-img" data-jqthumb-src="path/image1.png" data-jqthumb-width="200" data-jqthumb-height="200" />
  3. <img class="my-img" data-jqthumb-src="path/image2.png" data-jqthumb-width="200" data-jqthumb-height="180" />
  4. <img class="my-img" data-jqthumb-src="path/image3.png" data-jqthumb-width="200" data-jqthumb-height="160" />
  5. <img class="my-img" data-jqthumb-src="path/image4.png" data-jqthumb-width="200" data-jqthumb-height="140" />
  6. <img class="my-img" data-jqthumb-src="path/image5.png" data-jqthumb-width="200" data-jqthumb-height="120" />
  7. ...
  8. <script type="text/javascript">
  9.     $(function(){
  10.         $('.my-img').each(function(){
  11.             var $img = $(this);
  12.             $img.jqthumb({
  13.                 source : $img.attr('data-jqthumb-src'),
  14.                 width  : $img.attr('data-jqthumb-width'),
  15.                 height : $img.attr('data-jqthumb-height')
  16.             });
  17.         });
  18.     });
  19. </script>
  1. ...
  2. <img class="my-img" src="path/image.jpg" />
  3. ...
  4. <script type="text/javascript">
  5.     $(function(){
  6.         $('.my-img').jqthumb({
  7.             width  : 300,
  8.             height : 300,
  9.             show   : false, // By default the image would be shown immediately after processing. To disable, set it to false
  10.             after  : function(croppedImg){ // This callback returns an object
  11.                 $(croppedImg).fadeIn(); // This would fade in the cropped image
  12.             }
  13.         });
  14.     });
  15. </script>
  1. ...
  2. <img class="my-img" src="path/image1.jpg" />
  3. <img class="my-img" src="path/image2.jpg" />
  4. <img class="my-img" src="path/image3.jpg" />
  5. ...
  6. <script type="text/javascript">
  7.     $(function(){
  8.         $('.my-img').jqthumb({
  9.             width  : 300,
  10.             height : 300,
  11.             show   : false, // By default the image would be shown immediately after processing. To disable, set it to false
  12.             done   : function(allCroppedImgs){ // This callback returns an array
  13.                 for(i in allCroppedImgs){
  14.                     $(allCroppedImgs[i]).fadeIn(); // This would fade in the cropped images one by one
  15.                 }
  16.             }
  17.         });
  18.     });
  19. </script>

更多信息:https://github.com/pakcheong/jqthumb

jqthumb 按比例生成图片缩略图插件

已有 476 人购买
查看演示升级 VIP立刻购买

演示地址 下载地址
收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!