私はサーバーで生成された配列を持っています。これは、最大6つの配列アイテムを返すことができます。これらの配列アイテムはカテゴリに基づいて「カテゴリ」であり、異なるイメージを表示します(カテゴリごとに選択できる8つのカスタムイメージがありますが、これは裏で起こります)。カテゴリーごとに1つのイメージしか確立されていません。しかし、アップロードされた「カスタム」画像も存在する可能性があります(存在する場合)。実際には、サーバーはカテゴリーごとに1つまたは2つのイメージを返します。その上、ユーザは複数のカテゴリに分類することができるので、カテゴリに基づいてプライオリティレベルを追加する必要があります。したがって、これを効率的に処理して苦労する方法を理解しようとしています。特に優先分野では、配列に別の名前空間を追加して優先順位を指定し、最低値に基づいて指定する必要がありますか?配列の値を比較して結果を返す最も効率的な方法は何ですか?ダイナミックアレイに基づく画像の表示
編集:効率的であれば、スケーラビリティとメンテナンス性があり、必ずしもパフォーマンスが良いとは限りません。
console.clear();
// banner element
var banner = document.getElementById('banner');
// 3s timout
window.setTimeout(function() {
// add class (class handles fade transition)
banner.classList.add('fade-in');
}, 3000);
// Array generated from "Architecture Priorities" field, not all will display, but more than one can.
var arc = [
'Collaboration',
'Data Center',/*
'Enterprise Networks',
'Security',
'Services',*/
'Cross Architecture'
],
// manual array and image defaults (will need to utilize zval() for "path")
img = [{
name: 'Collaboration',
path: '//placehold.it/1200x400/fa4/fff', // value returned from server based on user input.
customPath: '' // value returned from server based on user input
}, {
name: 'Data Center',
path: '//placehold.it/1200x400/4af/fff',
customPath: ''
}, {
name: 'Enterprise Networks',
path: '//placehold.it/1200x400/af4/fff',
customPath: ''
}, {
name: 'Security',
path: '//placehold.it/1200x400/4fa/fff',
customPath: ''
}, {
name: 'Services',
path: '//placehold.it/1200x400/f4a/fff',
customPath: ''
}, {
name: 'Cross Architecture',
path: '//placehold.it/1200x400/a4f/fff',
customPath: '//placehold.it/1200x400/222/fff?text=custom'
}];
for (var prop in img) {
if (img.hasOwnProperty(prop)) {
for(var i = 0; i < arc.length; i++) {
if(img[prop].name === arc[i]){
if (img[prop].customPath !== '') {
\t banner.setAttribute('src', img[prop].customPath);
} else {
banner.setAttribute('src', img[prop].path);
}
}
}
}
}
.banner-wrapper {
max-width: 1200px;
background: transparent url('//placehold.it/1200x400/222/fff') no-repeat center center/cover;
}
img {
vertical-align: baseline;
display: block;
max-width: 100%;
height: auto;
opacity: 0;
transition: opacity 2s;
transform: translate3d(0, 0, 0);
backface-visibility: hidden;
}
img.fade-in {
opacity: 1;
}
<div class="banner-wrapper">
<img src="//placehold.it/1200x400/fff/222" width="1200" height="400" id="banner">
</div>
ええ、これは私が思っていたことですが、ちょっと面倒だったようです。 – darcher