js如何显示缩略图和点击放大查看???

JavaScript016

js如何显示缩略图和点击放大查看???,第1张

所谓的缩略图其实就是设置了一个长宽小点的<img>去存放这张图片,在<img>的点击事件中再去更改它的长宽达到放大效果,当然这样可能会打乱页面布局,所以你可以做成像QQ空间那样,点击图片后利用遮罩层显示放大的图片。

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结构:

<div style="width: 100%height: 400px">

<img src="path/picture.jpg" class="example1" />

</div>

<div style="width: 400pxheight: 400px">

<img src="path/picture.jpg" class="example2" />

</div>

<button id="kill">Kill</button>

<button id="kill-all">Kill All</button>

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

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

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

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

<script type="text/javascript">

$(function(){

// plugin initialization

$('img').jqthumb({

classname : 'jqthumb', // class name. DEFUALT IS jqthumb

width : '100%', // new image width after cropping. DEFAULT IS 100px.

height : '100%', // new image height after cropping. DEFAULT IS 100px.

position : {

x : '50%', // x position of the image. DEFAULT is 50%. 50% also means centerize the image.

y : '50%'// y position of the image. DEFAULT is 50%. 50% also means centerize the image.

},

source : 'src', // to specify the image source attribute. DEFAULT IS src.

show : false, // TRUE = show immediately after processing. FALSE = do not show it. DEFAULT IS TRUE.

responsive : 20, // used by older browsers only. 0 to disable. DEFAULT IS 20

zoom : 1, // zoom the output, 2 would double of the actual image size. DEFAULT IS 1

method : 'auto', // 3 methods available: "auto", "modern" and "native". DEFAULT IS auto

before : function(oriImage){ // callback before each image starts processing.

alert("I'm about to start processing now...")

},

after : function(imgObj){ // callback when each image is cropped.

console.log(imgObj)

},

done : function(imgArray){ // callback when all images are cropped.

for(i in imgArray){

$(imgArray[i]).fadeIn()

}

}

})

// kill command

$('#kill').click(function(){

$('.example1').jqthumb('kill')

})

// kill all command

$('#kill').click(function(){

$.jqthumb('killall')

})

})

</script>

BOWER

bower install jqthumb

可用参数

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

$('img').jqthumb({

source : 'attr-src' // DEFAULT: src

})

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

$('img').jqthumb({

width : 200, // DEFAULT: 100

height : '100%' // DEFAULT: 100

})

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

$('img').jqthumb({

position: {

x : 20, // DEFAULT: '50%'

y : '30%' // DEFAULT: '50%'

}

})

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

$('img').jqthumb({

show : false // DEFAULT: true

})

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

/* responsive only works for native method / older browsers */

$('img').jqthumb({

responsive : 10 // DEFAULT: 20

})

/* to disable responsive feature in modern method / browsers, switch method to native */

$('img').jqthumb({

method : 'native', // DEFAULT: auto

responsive : 0 // DEFAULT: 20

})

zoom:放大或缩小缩略图:

$('img').jqthumb({

zoom : 3 // DEFAULT: 1

})

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

$('img').jqthumb({

method : 'native' // Availability: "auto", "modern", "native". DEFAULT: auto

})

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

$('img').jqthumb({

before : function(originalImage){

console.log(originalImage)

}

})

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

$('img').jqthumb({

after : function(newThumb){

$(newThumb).fadeIn()

}

})

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

$('img').jqthumb({

done : function(thumbnails){

for(i in thumbnails)

$(thumbnails[i]).fadeIn()

}

})

可用命令

$('img').jqthumb('kill')// destroy the plugin

$.jqthumb('killall')// destroy all generated thumbnails on the page

更多的使用方法

...

<img src="path/image.jpg" />

...

<script type="text/javascript">

$(function(){

$('img').jqthumb({

width : 300,

height : 200

})

})

</script>

...

<div data-jqthumb-src="path/image.jpg"></div>

...

<script type="text/javascript">

$(function(){

$('div').jqthumb({

source : 'data-jqthumb-src'

})

})

</script>

...

<div style="width: 100%height:500px">

<img src="path/image.png" />

</div>

...

<script type="text/javascript">

$(function(){

$('div').jqthumb({

width : '100%',

height : '100%'

})

})

</script>

...

<img class="my-img" data-jqthumb-src="path/image1.png" data-jqthumb-width="200" data-jqthumb-height="200" />

<img class="my-img" data-jqthumb-src="path/image2.png" data-jqthumb-width="200" data-jqthumb-height="180" />

<img class="my-img" data-jqthumb-src="path/image3.png" data-jqthumb-width="200" data-jqthumb-height="160" />

<img class="my-img" data-jqthumb-src="path/image4.png" data-jqthumb-width="200" data-jqthumb-height="140" />

<img class="my-img" data-jqthumb-src="path/image5.png" data-jqthumb-width="200" data-jqthumb-height="120" />

...

<script type="text/javascript">

$(function(){

$('.my-img').each(function(){

var $img = $(this)

$img.jqthumb({

source : $img.attr('data-jqthumb-src'),

width : $img.attr('data-jqthumb-width'),

height : $img.attr('data-jqthumb-height')

})

})

})

</script>

...

<img class="my-img" src="path/image.jpg" />

...

<script type="text/javascript">

$(function(){

$('.my-img').jqthumb({

width : 300,

height : 300,

show : false, // By default the image would be shown immediately after processing. To disable, set it to false

after : function(croppedImg){ // This callback returns an object

$(croppedImg).fadeIn()// This would fade in the cropped image

}

})

})

</script>

...

<img class="my-img" src="path/image1.jpg" />

<img class="my-img" src="path/image2.jpg" />

<img class="my-img" src="path/image3.jpg" />

...

<script type="text/javascript">

$(function(){

$('.my-img').jqthumb({

width : 300,

height : 300,

show : false, // By default the image would be shown immediately after processing. To disable, set it to false

done : function(allCroppedImgs){ // This callback returns an array

for(i in allCroppedImgs){

$(allCroppedImgs[i]).fadeIn()// This would fade in the cropped images one by one

}

}

})

})

</script>

使用:缩略图水印组件wsImage3.5

功能特点:

1、导入多种图片格式,目前支持BMP,JPG,GIF等。

2、输出JPG格式缩略图,自定义输出压缩参数

3、高效率生成缩略图,4种导出方式,方便灵活。

4、图片裁减功能,自定义裁减区域。

5、能购获取图片尺寸。

6、支持文字水印,可自定义字体、颜色、旋转角度等。

7、支持图片水印,图片水引支持透明度。

8、性能更稳定,消耗系统资源少。

下载地址:http://ishare.edu.sina.com.cn/cgi-bin/fileid.cgi?fileid=420662