簡體   English   中英

比較上傳圖片的寬度和高度

[英]Comparing width and height of uploaded picture

我有一個文件輸入(圖片),我想根據更大的值設置其divclass :其widthheight

我的代碼不起作用(只有最后一個if語句),無論上傳的圖像如何, div的類都不會改變。

這是我的HTML:

<div class="row">
    <div class="col-md-12">
        <div id="blah" class="profilePic">
            <img src="profilkep.jpg" />
        </div>
    </div>
</div>

<br>

<div class="row">
    <div class="col-md-12">
        <input type="file" onchange="previewFile()">
    </div>
</div>

這是我的JavaScript:

function previewFile(){
    var preview = document.querySelector('img'); //selects the query named img
    var file    = document.querySelector('input[type=file]').files[0]; //sames as here
    var reader  = new FileReader();

    reader.onloadend = function () {
        preview.src = reader.result;
    }

    if (file) {
        reader.readAsDataURL(file); //reads the data as a URL
    } else {
        preview.src = "";
    }

    if (file.width < file.height) {
        document.getElementById("blah").setAttribute("class", "blahblah");
    } else {
        document.getElementById("blah").setAttribute("class", "profilePic");
    }
}

您的代碼似乎很好。 這是一個工作片段

 function previewFile(){ var preview = document.querySelector('img'); //selects the query named img var file = document.querySelector('input[type=file]').files[0]; //sames as here var reader = new FileReader(); reader.onloadend = function () { preview.src = reader.result; } if (file) { reader.readAsDataURL(file); //reads the data as a URL } else { preview.src = ""; } if (file.width < file.height) { document.getElementById("blah").setAttribute("class", "blahblah"); } else { document.getElementById("blah").setAttribute("class", "profilePic"); } } 
 .blahblah { border: 1px solid red; } .profilePic { border: 1px solid blue; } 
  <div class="row"> <div class="col-md-12"> <div id="blah" class="profilePic"> <img src="http://i.imgur.com/MxPGcXu.gif" /> </div> </div> </div> <br> <div class="row"> <div class="col-md-12" id="blah"> <input type="file" onchange="previewFile()"> </div> </div> 

問題是,你有兩個相同的ID blah 所有ID必須是唯一的。 file.widthfile.height始終未定義。 解決方法是:

 <div class="row">
  <div class="col-md-12">
    <div id="blah">
      <img id="profileImage" src="profilkep.jpg" />
    </div>
  </div>
</div>

<br>

<div class="row">
  <div class="col-md-12">
    <input type="file" onchange="previewFile()">
  </div>
</div>
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <script type="text/javascript">
    function previewFile(){
        var preview = document.querySelector('img'); //selects the query named img
        var file    = document.querySelector('input[type=file]').files[0]; //sames as here
        var reader  = new FileReader();

        reader.onloadend = function () {
          preview.src = reader.result;
          var imageWidth = $('#profileImage').width();
          var imageHeight = $('#profileImage').height();
          if (imageWidth < imageHeight) {
          console.log('width < height');
          document.getElementById("blah").setAttribute("class", "blahblah");
        } else {
          console.log('width > height');
          document.getElementById("blah").setAttribute("class", "profilePic");
        }
        }

        if (file) {
          reader.readAsDataURL(file); //reads the data as a URL
        } else {
          preview.src = "";
        }

      }
    </script>    

暫無
暫無

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

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