22 图片缩放居中 v1.3
33 BY:le
44*/
5- define ( function ( require , exports , moudles ) {
6- ( function ( $ ) {
7- $ . fn . imagezoom = function ( parameter , getApi ) {
8- parameter = parameter || { } ;
9- var defaults = {
10- width : null , //图片外层宽度
11- height : null , //图片外层宽度
12- resizeable : true , //窗口大小改变时是否重新调整图片位置
13- effect :'out' , //图片处理
14- condition : 'img' , //默认筛选条件
15- hoverEvent :false , //鼠标悬浮时是否放大
16- hoverRatio :1.2 , //鼠标悬浮时放大比例
17- duration :300 //鼠标悬浮时放大动画时长
18- } ;
19- var options = $ . extend ( { } , defaults , parameter ) ;
20- var $window = $ ( window ) ;
21- return this . each ( function ( index ) {
22- var $this = $ ( this ) . css ( "overflow" , "hidden" ) ;
23- var _api = { } ;
24- var _duration = options . duration ;
25- var _hoverRatio = options . hoverRatio ;
26- var _outer_width = parameter . width || $this . width ( ) ;
27- var _outer_height = parameter . height || $this . height ( ) ;
28- $this . find ( options . condition ) . each ( function ( ) {
29- var $img = $ ( this ) ;
30- $img . css ( 'display' , 'block' ) . removeAttr ( 'width' ) . removeAttr ( 'height' ) ;
31- var _width = this . width ;
32- var _height = this . height ;
33- var _ratio = 1 ;
34- if ( this . complete && _width ) { //防止图片未加载时就开始计算
5+ ( function ( $ ) {
6+ $ . fn . imagezoom = function ( parameter , getApi ) {
7+ parameter = parameter || { } ;
8+ var defaults = {
9+ width : null , //图片外层宽度
10+ height : null , //图片外层宽度
11+ resizeable : true , //窗口大小改变时是否重新调整图片位置
12+ effect :'out' , //图片处理
13+ condition : 'img' , //默认筛选条件
14+ hoverEvent :false , //鼠标悬浮时是否放大
15+ hoverRatio :1.2 , //鼠标悬浮时放大比例
16+ duration :300 //鼠标悬浮时放大动画时长
17+ } ;
18+ var options = $ . extend ( { } , defaults , parameter ) ;
19+ var $window = $ ( window ) ;
20+ return this . each ( function ( index ) {
21+ var $this = $ ( this ) . css ( "overflow" , "hidden" ) ;
22+ var _api = { } ;
23+ var _duration = options . duration ;
24+ var _hoverRatio = options . hoverRatio ;
25+ var _outer_width = parameter . width || $this . width ( ) ;
26+ var _outer_height = parameter . height || $this . height ( ) ;
27+ $this . find ( options . condition ) . each ( function ( ) {
28+ var $img = $ ( this ) ;
29+ $img . css ( 'display' , 'block' ) . removeAttr ( 'width' ) . removeAttr ( 'height' ) ;
30+ var _width = this . width ;
31+ var _height = this . height ;
32+ var _ratio = 1 ;
33+ if ( this . complete && _width ) { //防止图片未加载时就开始计算
34+ getRatio ( ) ;
35+ } else {
36+ this . onload = function ( ) {
37+ _width = this . width ;
38+ _height = this . height ;
3539 getRatio ( ) ;
36- } else {
37- this . onload = function ( ) {
38- _width = this . width ;
39- _height = this . height ;
40- getRatio ( ) ;
41- }
42- }
43- //私有方法
44- function getRatio ( ) {
45- //数值计算
46- if ( options . effect == 'out' ) { //在不放大图片失真的情况下,最大面积地展示图片
47- if ( _width > _height ) {
48- if ( _height > _outer_height ) {
49- _ratio = Math . max ( _outer_width / _width , _outer_height / _height ) ;
50- }
51- } else {
52- if ( _width > _outer_width ) {
53- _ratio = Math . max ( _outer_width / _width , _outer_height / _height ) ;
54- }
55- }
56- } else if ( options . effect == 'in' ) { //在不放大图片失真的情况下,最大清晰度地展示完整图片
57- if ( _width > _height ) {
58- if ( _width > _outer_width ) {
59- _ratio = Math . max ( _outer_width / _width ) ;
60- }
61- } else {
62- if ( _height > _outer_height ) {
63- _ratio = Math . max ( _outer_height / _height ) ;
64- }
65- }
66- }
67- zoom ( _ratio ) ;
68- } ;
69- //缩放动画
70- function zoom ( ratio , isAnimate ) { //ratio:放大比例,isAnamate:是否动画(默认不动画)
71- var obj = {
72- 'width' :Math . ceil ( _width * ratio ) ,
73- 'height' :Math . ceil ( _height * ratio ) ,
74- 'margin-left' :Math . ceil ( ( _outer_width - _width * ratio ) / 2 ) ,
75- 'margin-top' :Math . ceil ( ( _outer_height - _height * ratio ) / 2 )
76- } ;
77- if ( isAnimate ) {
78- $img . stop ( ) . animate ( obj , _duration ) ;
40+ }
41+ }
42+ //私有方法
43+ function getRatio ( ) {
44+ //数值计算
45+ if ( options . effect == 'out' ) { //在不放大图片失真的情况下,最大面积地展示图片
46+ if ( _width > _height ) {
47+ if ( _height > _outer_height ) {
48+ _ratio = Math . max ( _outer_width / _width , _outer_height / _height ) ;
49+ }
7950 } else {
80- $img . css ( obj ) ;
81- }
82- } ;
83- //事件绑定
84- if ( options . hoverEvent ) {
85- $this . on ( {
86- 'mouseenter' :function ( ) {
87- zoom ( _ratio * _hoverRatio , true ) ;
88- } ,
89- 'mouseleave' :function ( ) {
90- zoom ( _ratio , true ) ;
51+ if ( _width > _outer_width ) {
52+ _ratio = Math . max ( _outer_width / _width , _outer_height / _height ) ;
9153 }
92- } ) ;
54+ }
55+ } else if ( options . effect == 'in' ) { //在不放大图片失真的情况下,最大清晰度地展示完整图片
56+ if ( _width > _height ) {
57+ if ( _width > _outer_width ) {
58+ _ratio = Math . max ( _outer_width / _width ) ;
59+ }
60+ } else {
61+ if ( _height > _outer_height ) {
62+ _ratio = Math . max ( _outer_height / _height ) ;
63+ }
64+ }
9365 }
94- if ( options . resizeable ) {
95- $window . resize ( function ( ) {
96- _outer_width = parameter . width || $this . width ( ) ;
97- _outer_height = parameter . height || $this . height ( ) ;
98- getRatio ( ) ;
99- } ) ;
66+ zoom ( _ratio ) ;
67+ } ;
68+ //缩放动画
69+ function zoom ( ratio , isAnimate ) { //ratio:放大比例,isAnamate:是否动画(默认不动画)
70+ var obj = {
71+ 'width' :Math . ceil ( _width * ratio ) ,
72+ 'height' :Math . ceil ( _height * ratio ) ,
73+ 'margin-left' :Math . ceil ( ( _outer_width - _width * ratio ) / 2 ) ,
74+ 'margin-top' :Math . ceil ( ( _outer_height - _height * ratio ) / 2 )
75+ } ;
76+ if ( isAnimate ) {
77+ $img . stop ( ) . animate ( obj , _duration ) ;
78+ } else {
79+ $img . css ( obj ) ;
10080 }
101- } ) ;
81+ } ;
82+ //事件绑定
83+ if ( options . hoverEvent ) {
84+ $this . on ( {
85+ 'mouseenter' :function ( ) {
86+ zoom ( _ratio * _hoverRatio , true ) ;
87+ } ,
88+ 'mouseleave' :function ( ) {
89+ zoom ( _ratio , true ) ;
90+ }
91+ } ) ;
92+ }
93+ if ( options . resizeable ) {
94+ $window . resize ( function ( ) {
95+ _outer_width = parameter . width || $this . width ( ) ;
96+ _outer_height = parameter . height || $this . height ( ) ;
97+ getRatio ( ) ;
98+ } ) ;
99+ }
102100 } ) ;
103- } ;
104- } ) ( jQuery ) ;
105- } ) ;
101+ } ) ;
102+ } ;
103+ } ) ( jQuery ) ;
0 commit comments