簡體   English   中英

圖像調整大小以適應屏幕

[英]Image resize to fit screen

考慮到導航欄的可用空間,我創建了此代碼以調整照片/圖像的大小以適應屏幕。

該腳本將在圖像加載和導航點擊時觸發。

有沒有人建議改善這一點並確保瀏覽器兼容性?

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
    }
  };

我知道這個問題已經過時了,但這里有一個解決方案(雖然我確信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/

嘗試使用jQuery-Backgrounder插件 您可以調整它來做您需要的事情。 這是一個例子:

<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>

我寫了一個插件!

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();

我是這樣做的:

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

看起來不錯,但我建議將調整大小函數附加到jQuery的Window Resize Event處理程序。 然后圖像將隨頁面拉伸和縮小。

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';  
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM