/*//////////////////////////*/ /*//////////////////////////*/ /*/ jquery imagefit /*/ /*/ 1.0.0 /*/ /*//////////////////////////*/ /*//////////////////////////*/ (function( $ ) { $.fn.imagefit = function(options) { //default setting var opts = $.extend({}, $.fn.imagefit.defaults, options); //public vars var html = $('html'); html.data('main', this); html.data('mode', opts.mode); html.data('hor', opts.halign); html.data('ver', opts.valign); html.data('force', opts.force); //private vars var mw = html.data('main').width(); var mh = html.data('main').height(); var nw = 0, nh = 0; var imgs = 0; //container css html.data('main').css('overflow', 'hidden'); html.data('main').css('position', 'relative'); //look for imgs html.data('main').find('img').each(function() { var img = $(this); img.css('position', 'absolute'); var iw = $(this).actual('width');//$(this).width() var ih = $(this).actual('height');//$(this).height() var top = 0; var left = 0; var ratio = 1; var size = []; ++imgs; $("").attr("src", img.attr('src')).load(function() { size['w'] = this.width; size['h'] = this.height; ratio = size['w'] / size['h']; if(html.data('mode') == 'outside') { if (mw / mh > iw / ih) { if(html.data('force') === false && (mw > size['w'] || mh > size['h'])) { nw = size['w']; nh = size['h']; top = math.round((mh-nh)/2); if(html.data('hor') == 'left') { left = 0; } else if (html.data('hor') == 'right') { left = left = math.round(((mw-nw)/2)*2); } else { left = math.round((mw-nw)/2); } } else { nw = mw; nh = math.round((nw * ih) / iw); top = -math.abs(math.round((nh-mh)/2)); } if(html.data('ver') == 'top') { img.css('top', '0'); img.css('left', left+'px'); } else if(html.data('ver') == 'bottom') { img.css('top', (top*2)+'px'); img.css('left', left+'px'); } else { img.css('top', top+'px'); img.css('left', left+'px'); } } else { if(html.data('force') === false && (mw > size['w'] || mh > size['h'])) { nw = size['w']; nh = size['h']; top = math.round((mh-nh)/2); left = math.round((mw-nw)/2); } else { nh = mh; nw = math.round((nh * iw) / ih); left = -math.abs(math.round((nw-mw)/2)); } if(html.data('hor') == 'left') { img.css('top', top+'px'); img.css('left', '0'); } else if(html.data('hor') == 'right') { img.css('left', (left*2)+'px'); img.css('top', top+'px'); } else { img.css('left', left+'px'); img.css('top', top+'px'); } } img.css('maxwidth', 'none'); img.css('maxheight', 'none'); img.width(nw); img.height(nh); img.css('width', nw); img.css('height', nh); } else if(html.data('mode') == 'inside') { if (mw / mh > iw / ih) { if(html.data('force') === false && mw > size['w'] && mh > size['h']) { nw = size['w']; nh = size['h']; top = math.round((mh-nh)/2); left = math.round((mw-nw)/2); } else { nh = mh; nw = math.round((nh * iw) / ih); left = math.round((mw-nw)/2); } img.css('left', left+'px'); img.css('top', top+'px'); if(html.data('hor') == 'left') { img.css('left', '0'); } if(html.data('ver') == 'top') { img.css('top', '0'); } if(html.data('hor') == 'right') { img.css('left', (left*2)+'px'); } if(html.data('ver') == 'bottom') { img.css('top', (top*2)+'px'); } } else { if(html.data('force') === false && mw > size['w'] && mh > size['h']) { nw = size['w']; nh = size['h']; top = math.round((mh-nh)/2); left = math.round((mw-nw)/2); } else { nw = mw; nh = math.round((nw * ih) / iw); top = math.round((mh-nh)/2); } img.css('left', left+'px'); img.css('top', top+'px'); if(html.data('hor') == 'left') { img.css('left', '0'); } if(html.data('ver') == 'top') { img.css('top', '0'); } if(html.data('hor') == 'right') { img.css('left', (left*2)+'px'); } if(html.data('ver') == 'bottom') { img.css('top', (top*2)+'px'); } } img.width(nw); img.height(nh); img.css('width', nw); img.css('height', nh); } else {} }); }); //console.log('imagefit found '+imgs+' images.'); }; $.fn.imagefit.defaults = { mode : 'outside', // 'inside' halign : 'center', // 'left', 'right' valign : 'middle', // 'top', 'bottom' force : true, // false }; })( jquery ); /*图片处理*/ $(function () { function func_imgauto() { $('.auto-img img').each(function () { //$(this).closest(".auto-img").width($(this).closest(".auto-img").parent().actual('width'));//新加 var per = $(this).data("height") / $(this).data("width"); if ($(this).parent().hasclass("auto-img")) { $(this).parent(".auto-img").height($(this).parent(".auto-img").actual('width') * per); } else { $(this).parentsuntil(".auto-img").parent(".auto-img").height($(this).parentsuntil(".auto-img").parent(".auto-img").actual('width') * per); } //存在缓存中,立即处理 }); $('.auto-img').each(function () { $(this).imagefit({ mode : 'outside', force : 'true', halign : 'center', valign : 'middle' }); }); }; /*执行图片自动化处理*/ $(document).imagesloaded( function() { func_imgauto(); } ) /*窗口改变*/ $(window).resize(function() { func_imgauto(); } ); /*新闻轮播*/ //$(".cm-tit2").hover(function(){$(".newsbg").show();swiper2.reinit();swiper2.swipeto(0);func_imgauto();}); var swiper2 = new swiper('.swiper-roll-2', { slidesperview: 1, //slidespergroup : (window.innerwidth >= 480 || document.documentelement.clientwidth >= 480) ? 5 : 2, autoplaydisableoninteraction : false, autoplay :4000, //slidespergroup:(window.innerwidth >= 480 || document.documentelement.clientwidth >= 480) ? 5 : 1, pagination: '.swiper-roll-2 .swiper-pagination', loop:true, grabcursor: true, spacebetween :0, //observer:true, //observeparents:true, paginationclickable: true }); })