前文Hexo Next主题添加友链从其他博客获取的样式有一些问题:
- 只在网页全屏时友链卡片比例显示正常,调整分辨率后不同行的卡片比例会乱掉
- 图片没有按比例显示,样式不一致
即没有自适应,这里使用Cursor优化下样式,并添加了图片自适应样式,按网页大小调整列数,最多显示6列,默认4列,并且文字仅显示一行,超出的显示省略号.
这是修改后的效果
修改
links.js
下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| function shuffle(arr) { let i = arr.length; while (i) { let j = Math.floor(Math.random() * i--); [arr[j], arr[i]] = [arr[i], arr[j]]; } }
function renderlink(data) { shuffle(data); const linkContainer = document.querySelector(".link-navigation"); linkContainer.innerHTML = data.map(item => ` <div class="card"> <div class="thumb" style="background-image: url('${item.avatar}');"></div> <div class="card-header"> <a href="${item.site}" target="_blank">${item.name}</a> </div> </div> `).join(''); }
fetch('/links/linklist.json') .then(response => response.json()) .then(res => renderlink(res));
|
links.css
下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| .link-navigation { margin-top: 1rem; display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; max-width: 100%; }
@media (max-width: 768px) { .link-navigation { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 768px) and (max-width: 992px) { .link-navigation { grid-template-columns: repeat(3, 1fr); } }
@media (min-width: 1920px) { .link-navigation { grid-template-columns: repeat(5, 1fr); gap: 24px; } }
@media (min-width: 3840px) { .link-navigation { grid-template-columns: repeat(6, 1fr); gap: 32px; } }
.card { font-size: 1rem; padding: 0; border-radius: 4px; transition-duration: .15s; margin-bottom: 1rem; box-shadow: 0 2px 6px 0 rgba(0, 0, 0, .12); background: #f5f5f5; width: 100%; aspect-ratio: 1; overflow: hidden; display: flex; flex-direction: column; position: relative; }
.card:hover { transform: scale(1.1); box-shadow: 0 2px 6px 0 rgba(0, 0, 0, .12), 0 0 6px 0 rgba(0, 0, 0, .04); }
.card .thumb { position: absolute; top: 0; left: 0; width: 100%; height: calc(100% - 40px); background-position: center !important; background-repeat: no-repeat !important; background-size: contain !important; }
.posts-expand .post-body img { margin: 0; padding: 0; border: 0; }
.card .card-header { position: absolute; bottom: 0; left: 0; width: 100%; height: 40px; padding: 8px; background: rgba(255, 255, 255, 0.9); display: flex; align-items: center; justify-content: center; }
.card .card-header a { font-style: normal; color: #2bbc8a; font-weight: 700; text-decoration: none; border: 0; display: block; width: 100%; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; transition: all 0.3s; }
.card:hover .card-header a { white-space: normal; overflow: visible; }
|