2017-08-13 6 views
0

border-radius50%<img>タグの画像の中心を円の画像として表示しようとしています。問題は、imageがURLの代わりにfirebase storageファイルから表示されることです。ファイアベースのJavaScriptを使用したセンター画像

私は画像の中心を示すために、JavaScriptコードを試してみましたが、それは私がこれを行う場合は、単に素晴らしい作品:

.box { 
 
    height: 100px; 
 
    width: 100px; 
 
    overflow: hidden 
 
} 
 

 
.wh { 
 
    height: 100%!important 
 
} 
 

 
.ww { 
 
    width: 100%!important 
 
}
<div class="box" style="border-radius:50%;"> 
 
    <img src="1.jpg" /> 
 
</div> 
 
<input type="file" id="fileButton" /> <input type="submit" value="Upload" id="uploadButton" />

を私は何私であるこの結果を、取得

enter image description here

しかし、私はfirebase(からの画像を使用する場合は、完全である:欲しいです同じ画像がところで)その後、私はこの結果が得られます。また、上記URLの画像を作るに使用された画像を()を中心に使用され、また、JavaScriptがここ

enter image description here

がFirebaseとコードです:

var fileButton = document.getElementById('fileButton'); 
 
var uploadButton = document.getElementById('uploadButton'); 
 

 
fileButton.addEventListener('change', function(e) { 
 
    var file = e.target.files[0]; 
 

 
    //Create a storage ref in firebase 
 
    var storageRef = firebase.storage().ref('test/profilePic.jpg'); 
 

 
    console.log(file.height); 
 

 
    uploadButton.addEventListener('click', function(e) { 
 
    storageRef.put(file).then(function() { 
 
     window.open('profil.php?error=1', '_top'); 
 
    }); 
 

 

 
    }); 
 
}); 
 

 
var storage = firebase.storage(); 
 
var storageRef = storage.ref(); 
 
var spaceRef = storageRef.child('test/profilePic.jpg'); 
 

 
storageRef.child('test/profilePic.jpg').getDownloadURL().then(function(url) { 
 

 
    var test = url; 
 
    document.getElementById('profilePicture').src = test; 
 

 
}).catch(function(error) { 
 

 
}); 
 

 

 

 
$('.box').each(function() { 
 

 

 
    //set size 
 
    var th = $(this).height(), //box height 
 
    tw = $(this).width(), //box width 
 
    im = $(this).children('img'), //image 
 
    ih = im.height(), //inital image height 
 
    iw = im.width(); //initial image width 
 
    if (ih > iw) { //if portrait 
 
    im.addClass('ww').removeClass('wh'); //set width 100% 
 
    } else { //if landscape 
 
    im.addClass('wh').removeClass('ww'); //set height 100% 
 
    } 
 
    //set offset 
 
    var nh = im.height(), //new image height 
 
    nw = im.width(), //new image width 
 
    hd = (nh - th)/2, 
 
    wd = (nw - tw)/2; 
 
    if (nh < nw) { //if portrait 
 
    im.css({ 
 
     marginLeft: '-' + wd + 'px', 
 
     marginTop: 0 
 
    }); //offset left 
 
    } else { 
 
    im.css({ 
 
     marginTop: '-' + hd + 'px', 
 
     marginLeft: '0px' 
 
    }); //offset top 
 
    } 
 
});
.box { 
 
    height: 100px; 
 
    width: 100px; 
 
    overflow: hidden 
 
} 
 

 
.wh { 
 
    height: 100%!important 
 
} 
 

 
.ww { 
 
    width: 100%!important 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="box" style="border-radius:50%;"> 
 
    <img id="profilePicture" /> 
 
</div> 
 
<input type="file" id="fileButton" /> <input type="submit" value="Upload" id="uploadButton" />

誰もがこの問題を解決する方法を知っていますか?

+0

'uploadButton'と' fileButton'はあなたのコードで何が何であるかである ? – Dekel

+0

スニペットが実行されていません。 jQueryを追加して、他に何かが欠けていないかどうかを確認してください。 – Juan

+0

@Dekelボタンを追加しました。これは単なるファイルボタンであり、イメージをfirebaseに保存するボタンです。これらの関数は機能していますが、問題は、firebaseでsrcを受け取る代わりに、imgタグの中でsrcを使用しているときと同じように、境界半径の中心として画像の中心を表示するスクリプトです。 – FeReTu

答えて

0

ここにはいくつかの問題があります。

  1. (Firebaseがデータを返す前にJavaScriptでスタイルを設定しています)非同期の問題が発生している可能性があります。
  2. CSSであなたのスタイリングをしているはずです。
  3. 絶対に使用しないでください!重要で理解して使用してくださいCSS specificity

CSSを正しく設定すると、これが処理されます。私はfirebaseとそれを清潔に保つために他の多くの不要な部品を取り出した。問題は、非同期の問題か、CSSを正しく使用していないかのどちらかです。私はJavaScriptでsetTimeoutで非同期変更をシミュレートします。

setTimeout(function(){ 
 
    document.getElementById("imageid").src="http://www.threeriversschools.org/pages/district-blog/image/section-image/soccer-194.jpg"; 
 
},3000);
.box{ 
 
height:100px; 
 
width:100px; 
 
overflow:hidden 
 
} 
 
.box img{ 
 
    width : 100%; 
 
    height : 100%; 
 
}
<div class="box" style="border-radius:50%;" > 
 
<img id="imageid" src="http://cdn-mf1.heartyhosting.com/sites/mensfitness.com/files/styles/wide_videos/public/soccer-kick-goal-kickoff-1280.jpg?itok=z-E-ZQem" /> 
 
</div>

関連する問題