【问题标题】:Image resize to fit screen调整图像大小以适应屏幕
【发布时间】:2011-02-03 21:08:07
【问题描述】:

考虑到导航栏的可用空间,我创建了这段代码来调整照片/图像的大小以适应屏幕。

该脚本在图像加载和导航点击时触发。

有没有人有关于改进它并确保浏览器兼容性的建议?

HTML

$(document).ready(function(){
 $("#photo").load(function(){
  resize();
 });

 $(".navigation img").click(function(){
  var imgPath = $(this).attr("src"); 
  $("#photo").attr({ src: imgPath });
  resize();
  return false;
       });
   });

Javascript

resize = function() {

    var borderVt=150; //value based on css style. bottom bar + padding of photoContain
    var borderHz=40; //value based on css style photoContain padding

    $("#photo").css("width", "auto").css("height", "auto"); // Remove existing CSS
    $("#photo").removeAttr("width").removeAttr("height"); // Remove HTML attributes   

    var origSizeW = $("#photo").width();
    var origSizeH = $("#photo").height();
    var ratioVt=(origSizeW/origSizeH);
    var ratioHz=(origSizeH/origSizeW);
    var winW = $(window).width();
    var winH = $(window).height();
    var screenSizeW=Math.round(winW-borderHz);
    var screenSizeH=Math.round(winH-borderVt);

    if (origSizeW>=origSizeH){

     var newHeight = Math.round(screenSizeW*ratioHz);
     if (newHeight <= screenSizeH){
      $("#photo").css("width", screenSizeW); // Set new width
      $("#photo").css("height", newHeight);
      }
     else{
      $("#photo").css("height", screenSizeH);
      }

    }
    else{
    $("#photo").css("height", screenSizeH); // Set new height
    }
  };

【问题讨论】:

    标签: javascript jquery image resize


    【解决方案1】:

    我知道这个问题已经很老了,但这里是 a 解决方案(尽管我确信 OP 也可以正常工作):

    这个 jQuery 插件似乎完全可以满足您的需求: http://code.google.com/p/jquery-imagefit-plugin/

    如果您在 100% 高、100% 宽的元素上执行此操作: http://www.tutwow.com/htmlcss/quick-tip-css-100-height/

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="http://jquery-imagefit-plugin.googlecode.com/svn/trunk/jquery.imagefit-0.2.js"></script>
    <div style="width: 100%; height: 100%">
        <img  id="h5" src="http://www.w3.org/html/logo/downloads/HTML5_Logo_512.png"/>
    </div>
    <script>
        jQuery('#h5').bind('load', function() {
            jQuery('div').imagefit();
        });
    </script>
    

    (演示:http://jsfiddle.net/nottrobin/9Pbdz/

    【讨论】:

    • 我投了赞成票,但由于我不是最初提出问题的人,所以我不能接受。
    • 只是一个问题,当图像尺寸较小时,这种方法是否会减少发送给客户端的数据量?
    【解决方案2】:

    尝试使用jQuery-Backgrounder plugin。你也许可以调整它来做你需要的事情。这是一个例子:

    <script src="jquery.backgrounder.js" type="text/javascript" charset="utf-8"></script>
    
    <script type="text/javascript" charset="utf-8">
      $(function() {
        $('#my_background').backgrounder({element : 'body'});
      });
    </script>
    
    [...]
    
    <div id="my_background"><img src="birthday.jpg" alt="Birthday party" /></div>
    

    【讨论】:

      【解决方案3】:

      我写了一个插件!

      jQuery.fn.positionMe = function () {
          var oH = $(window).height();
          var oW = $(window).width();
          var iH = this.outerHeight();
          var iW = this.outerWidth();
      
          // When the image is too small so, do nothing
          if(iW>oW && iH>oH){
      
              // Otherwise, proportionate the image relative 
              // to its container
              if(oH/iH > oW/iW){
                  this.css("width", oW);
                  this.css("height", iH*(oW/iW))
              } else {
                  this.css("height", oH);
                  this.css("width", iW*(oH/iH));
              }
      
          }
          return this;
      }
      

      用法:

      $("#photo").positionMe();
      

      【讨论】:

        【解决方案4】:

        这是我的做法:

         jQuery(document).ready(function($) {
              $('.wp-post-image').height($(window).height());
         });
        

        【讨论】:

          【解决方案5】:

          对我来说看起来不错,但我建议将您的调整大小函数附加到 jQuery 的窗口调整大小事件处理程序。然后图像会随页面伸缩。

          【讨论】:

            【解决方案6】:
            var w=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth||document.body.offsetWidth||window.screen.availWidth;
            var h=window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight||document.body.offsetHeight||window.screen.availHeight;
            
            function resize() {
            
                a=document.getElementsByClassName(' scale');
                for(var i=0; i<a.length; i++){
            
                    var aW = a[i].offsetWidth;
                    var aH = a[i].offsetHeight;
                    var d=w/h;
                    var mT, mL;
            
                    if (d>=1.5){
                        aW=w;
                        aH=w-(w*(34/100));
                        mT=(aH/2)*-1;   
                        mL=(aW/2)*-1;   
                    } else {
                        aH=h;
                        aW=h+(h*(66/100));
                        mT=(aH/2)*-1;
                        mL=(aW/2)*-1;
                    }
            
                    a[i].style.height=aH+'px';
                    a[i].style.width=aW+'px';
                    a[i].style.marginTop=mT+'px';
                    a[i].style.marginLeft=mL+'px';  
                }
            }
            

            【讨论】:

            • 以上代码是纯javascript。在所有浏览器中居中拟合图像。只需找出您的图像的比例,即图像大小 1500 x 1000= 1.5 现在使用此比例进行比较。
            • 虽然这可能会回答问题,但在您的答案中加入一些文字来解释您在做什么总是一个好主意。阅读how to write a good answer
            猜你喜欢
            • 2020-08-26
            • 1970-01-01
            • 1970-01-01
            • 2023-01-12
            • 2014-05-18
            • 2017-11-11
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多