パーツ番号 03-15-06 ・ JavaScript使用
画像サムネイル付き商品候補
入力に応じてサムネイル画像・商品名・価格をセットにした候補が表示されるEC向けオートコンプリート。WooCommerceの商品検索窓の演出に応用できます。
プレビュー
ソースコード
HTML
<div class="d03-15-06-search">
<input type="text" class="d03-15-06-input" placeholder="商品名で検索" aria-label="商品検索">
<ul class="d03-15-06-list"></ul>
</div>CSS
.prev.d03-15-06{align-items:flex-start}
.d03-15-06-search{width:100%;max-width:300px;margin:0 auto}
.d03-15-06-input{width:100%;box-sizing:border-box;padding:10px 13px;border:1.5px solid #e5e7eb;border-radius:11px;font-size:13.5px;color:#1f2937;outline:none}
.d03-15-06-input:focus{border-color:#a5b4fc}
.d03-15-06-list{list-style:none;margin:8px 0 0;padding:0;display:flex;flex-direction:column;gap:6px}
.d03-15-06-list li{display:flex;align-items:center;gap:9px;padding:6px;background:#f8fafc;border:1px solid #eef2f7;border-radius:8px}
.d03-15-06-thumb{flex:0 0 34px;width:34px;height:34px;border-radius:6px;background:linear-gradient(135deg,#818cf8,#c084fc)}
.d03-15-06-name{flex:1;font-size:12.5px;color:#374151}
.d03-15-06-price{font-size:12px;font-weight:700;color:#4f46e5}JavaScript
(function(){
var root=document.querySelector('.d03-15-06');if(!root)return;
var input=root.querySelector('.d03-15-06-input');
var list=root.querySelector('.d03-15-06-list');
var data=[['木製デスクスタンド','3,980円'],['木目調 収納ボックス','2,480円'],['木製フォトフレーム','1,280円']];
input.addEventListener('input',function(){
var q=input.value.trim();
list.innerHTML='';
if(!q)return;
data.filter(function(d){return d[0].indexOf(q)>-1;}).forEach(function(d){
var li=document.createElement('li');
li.innerHTML='<span class="d03-15-06-thumb"></span><span class="d03-15-06-name">'+d[0]+'</span><span class="d03-15-06-price">'+d[1]+'</span>';
list.appendChild(li);
});
});
})();クラス名 .d03-15-06 は本ライブラリ内での識別用です。複数のパーツを同じページで使う場合は、重複しないよう名前を変更してください。