パーツ番号 11-05-12 ・ JavaScript使用
検索結果の表示形式切替(グリッド/リスト)
アイコンボタンで検索結果をグリッド表示とリスト表示に切り替えられます。WordPressの商品一覧・記事一覧で好みの見た目を選べるようにしたい場合に使えます。
プレビュー
ソースコード
HTML
<div class="d11-05-12-wrap">
<div class="d11-05-12-toolbar">
<span class="d11-05-12-count">4件中 4件表示</span>
<div class="d11-05-12-switch">
<button type="button" class="d11-05-12-vbtn is-active" data-view="grid" aria-label="グリッド表示">
<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/></svg>
</button>
<button type="button" class="d11-05-12-vbtn" data-view="list" aria-label="リスト表示">
<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2"><path d="M4 6h16M4 12h16M4 18h16"/></svg>
</button>
</div>
</div>
<div class="d11-05-12-results" id="d1598Results">
<div class="d11-05-12-card">商品A<span>¥3,200</span></div>
<div class="d11-05-12-card">商品B<span>¥2,600</span></div>
<div class="d11-05-12-card">商品C<span>¥4,100</span></div>
<div class="d11-05-12-card">商品D<span>¥1,900</span></div>
</div>
</div>CSS
.prev.d11-05-12{align-items:flex-start;justify-content:flex-start}
.d11-05-12-wrap{width:100%}
.d11-05-12-toolbar{display:flex;align-items:center;justify-content:space-between;margin-bottom:10px}
.d11-05-12-count{font-size:11.5px;color:#6b7280}
.d11-05-12-switch{display:flex;gap:4px;background:#f1f5f9;border-radius:8px;padding:3px}
.d11-05-12-vbtn{border:none;background:transparent;color:#9ca3af;width:26px;height:26px;border-radius:6px;
display:flex;align-items:center;justify-content:center;cursor:pointer}
.d11-05-12-vbtn.is-active{background:#fff;color:#4f46e5;box-shadow:0 1px 3px rgba(0,0,0,.12)}
.d11-05-12-results{display:grid;grid-template-columns:1fr 1fr;gap:8px}
.d11-05-12-results.is-list{display:flex;flex-direction:column}
.d11-05-12-card{display:flex;justify-content:space-between;align-items:center;background:#f8f9fb;border-radius:8px;padding:10px 12px;font-size:12px;color:#1f2937}
.d11-05-12-card span{font-weight:700;color:#4f46e5}JavaScript
(function(){
var root=document.querySelector('.d11-05-12-wrap');
if(!root)return;
var results=root.querySelector('#d1598Results');
root.querySelectorAll('.d11-05-12-vbtn').forEach(function(btn){
btn.addEventListener('click',function(){
root.querySelectorAll('.d11-05-12-vbtn').forEach(function(b){b.classList.remove('is-active');});
btn.classList.add('is-active');
results.classList.toggle('is-list',btn.getAttribute('data-view')==='list');
});
});
})();クラス名 .d11-05-12 は本ライブラリ内での識別用です。複数のパーツを同じページで使う場合は、重複しないよう名前を変更してください。