jquery实现根据浏览器窗口大小自动缩放图片的方法_Javascript教程-查字典教程网
jquery实现根据浏览器窗口大小自动缩放图片的方法
jquery实现根据浏览器窗口大小自动缩放图片的方法
发布时间:2016-12-30 来源:查字典编辑
摘要:本文实例讲述了jquery实现根据浏览器窗口大小自动缩放图片的方法。分享给大家供大家参考。具体如下:(function($){$.fn.re...

本文实例讲述了jquery实现根据浏览器窗口大小自动缩放图片的方法。分享给大家供大家参考。具体如下:

(function($){ $.fn.resizeimage = function(){ var imgLoad = function (url, callback) { var img = new Image(); img.src = url; if (img.complete) { callback(img.width, img.height); } else { img.onload = function () { callback(img.width, img.height); img.onload = null; }; }; }; var original = { width:$(window).width() }; return this.each(function(i,dom){ var image = $(this); imgLoad(image.attr('src'),function(){ var img = { width:image.width(), height:image.height() },percentage=1; if(img.width<original.width){ percentage = 1; }else{ percentage = (original.width)/img.width; } image.width(img.w=img.width*percentage-30).height(img.h=img.height*percentage); $(window).resize(function(){ var w = $(this).width(); percentage = w/img.width>1?1:w/img.width; var newWidth = img.width*percentage-30; var newHeight = img.height*percentage; image.width(newWidth).height(newHeight); }); }); }); }; })(jQuery);

希望本文所述对大家的jquery程序设计有所帮助。

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类