javascript
  • jquery
  • html
  • css
  • 2017-01-20 8 views 1 likes 
    1

    divにアップロードされた画像のプレビューを合わせるにはどうすればいいですか?

    <div style="display:none" id="previewImage" height="300px" width="300px"> 
        <img src="" id="previewImg" height="400px" width="450px"> 
    </div> 
    <div> 
        <input type='file' class="inputfile" name='file' id='file' size='50' accept='image/*' /> 
        <input type='submit' value='Upload' name='Upload image'/> 
    </div> 
    
    +0

    'jquery'を使用していますか? –

    +0

    あなたは 'max-width:100%;'をimgに適用しようとしましたか? – Shashi

    +0

    また、http://stackoverflow.com/questions/3029422/how-do-i-auto-resize-an-image-to-fit-a-div-container – mc20

    答えて

    3

    function readURL(input) { 
     
        if (input.files && input.files[0]) { 
     
        var reader = new FileReader(); 
     
    
     
        reader.onload = function(e) { 
     
         $('#passport_photo') 
     
         .attr('src', e.target.result) 
     
         .width(150) 
     
         .height('auto'); 
     
        }; 
     
    
     
        reader.readAsDataURL(input.files[0]); 
     
        } 
     
    } 
     
    $("#imgpath").change(function() { 
     
        readURL(this); 
     
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
     
    <input type="file" id="imgpath" name="file" accept="image/*"> 
     
    
     
    <div class=" ffset-2" > 
     
        <img id="passport_photo" src="#" alt="image" style="width:150px;height:auto;"> 
     
    </div>

    +0

    をチェックしてください。角度の? :D – tanmay

    +0

    @tanmay ha ha yeah ..私のスニペットからそれを削除することを忘れないでください –

    0

    基本的には次のようにdiv要素

    私のコードの背景画像として画像を行わずにdivの内側に画像をフィットする必要があります。

    width:100%; 
    height:auto; 
    

    https://jsfiddle.net/3scdng5h/

    関連する問題