jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 拉伸图像以填充div,但不倾斜大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将图像拉伸到div中.但是我希望它溢出,以便填满整个div. div使用页面调整大小.我想要一个类似于jQuery bgstretcher插件( http://www.ajaxblender.com/bgstretcher-2-jquery-stretch-background-plugin-updated.html)的效果,但是我不能使用该插件,因为我已经将它用于页面上的背景图像,当你尝试激活它的多个实例时,它似乎不起作用.

所以我要问的是某种简单的jQuery函数,它调整图像的大小以完全填充隐藏溢出的div,这样就没有间隙,但不会使图像偏斜.

编辑:

http://jsfiddle.net/R7UCZ/

这就是我所追求的,但我希望图像能够填满整个div,而不会产生偏差.如果图像有点偏离div,那很好,但是我需要用div来调整大小,div会调整页面的高度和宽度.

解决方法

首先,您需要获取图像属性并查看哪个更大,高度或宽度,然后在窗口调整大小后根据div大小调整图像大小,如下所示:
//this would load up initially,you will need this data for future processing


   div = $("div");
   imgsrc = div.find('img').attr('src');

   var img = new Image()
   img.onload = function(){
        imgw = img.width;
        imgh = img.height;

        //after its loaded and you got the size..
        //you need to call it the first time of course here and make it visible:

        resizeMyImg(imgw,imgh);
        div.find('img').show();

        //Now you can use your resize function
        $(window).resize(function(){ resizeMyImg(imgw,imgh) });

        }
   img.src = imgsrc


   function resizeMyImg(w,h){     
      //the width is larger
      if (w > h) {
        //resize the image to the div
        div.find('img').width(div.innerWidth() + 'px').height('auto');
      }        
      else {
        // the height is larger or the same
        //resize the image to the div
        div.find('img').height(div.innerHeight() + 'px').width('auto');
      }

     }

您可以在这里找到完整的解决方案:
http://jsfiddle.net/CDywS/1

大佬总结

以上是大佬教程为你收集整理的jquery – 拉伸图像以填充div,但不倾斜全部内容,希望文章能够帮你解决jquery – 拉伸图像以填充div,但不倾斜所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: