Skip to content

Commit 6f8f725

Browse files
committed
feat: 预览点击触发逻辑修改
1 parent 57c5b51 commit 6f8f725

File tree

3 files changed

+101
-10
lines changed

3 files changed

+101
-10
lines changed

products/nmsl/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,17 @@ <h2 class="section-title" id="channel-heading">All Channels</h2>
939939
}
940940

941941
if (src) {
942+
// Collect all wallpaper URLs from the gallery to maintain order
943+
const allCards = document.querySelectorAll('.gallery-card img');
944+
const wallpaperList = Array.from(allCards).map(img => img.src);
945+
946+
// Save to sessionStorage for the preview page to access
947+
try {
948+
sessionStorage.setItem('preview_wallpapers', JSON.stringify(wallpaperList));
949+
} catch (e) {
950+
console.error('Failed to save wallpapers to sessionStorage', e);
951+
}
952+
942953
// Open Modal
943954
// Encode params
944955
const params = new URLSearchParams({
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"scene":"base.jpg","title":"Untitled Scene","device":"iphone","anchors":[{"x":0.3766,"y":0.1989},{"x":0.6522,"y":0.2195},{"x":0.5968,"y":0.8161},{"x":0.2809,"y":0.7782}],"config":{"radius":169,"bezel":50,"opacity":1,"bright":120,"glare":44,"noise":0,"edge":18}}
1+
{"scene":"base.jpg","title":"Untitled Scene","device":"iphone","anchors":[{"x":0.3766,"y":0.1989},{"x":0.6522,"y":0.2195},{"x":0.5968,"y":0.8161},{"x":0.2809,"y":0.7782}],"config":{"radius":169,"bezel":50,"opacity":1,"bright":100,"glare":44,"noise":0,"edge":18}}

products/nmsl/wallpaper-preview.html

Lines changed: 89 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,15 @@
208208

209209
/* 隐藏滚动条 */
210210
-ms-overflow-style: none; scrollbar-width: none;
211+
212+
/* Initial state hidden to prevent flicker */
213+
opacity: 0; transition: opacity 0.3s ease;
211214
}
215+
/* 临时禁用平滑滚动的类 */
216+
.scroll-track.no-smooth { scroll-behavior: auto !important; }
217+
/* 布局稳定后显示 */
218+
.scroll-track.ready { opacity: 1; }
219+
212220
.scroll-track::-webkit-scrollbar { display: none; }
213221

214222
/* 左右对齐方式不同 - 统一改为居中,因为现在是独立的侧边栏区域 */
@@ -736,7 +744,7 @@
736744
// 场景列表容器 - 动态加载
737745
let SCENES = [];
738746

739-
const WALLPAPERS = [
747+
let WALLPAPERS = [
740748
'assets/killian-prevost-caillou-final.jpg',
741749
'assets/jacek-pilarski-citadel.webp',
742750
'assets/yucong-tang-yucong-tang-2e.webp',
@@ -765,7 +773,8 @@
765773
wallIndex: 0,
766774
bgFlip: false,
767775
isScrolling: false,
768-
viewMode: 'scene'
776+
viewMode: 'scene',
777+
currentBgUrl: ''
769778
};
770779

771780
const els = {
@@ -859,6 +868,19 @@
859868
*/
860869

861870
async function init() {
871+
// Try to load wallpapers from sessionStorage first
872+
try {
873+
const storedWallpapers = sessionStorage.getItem('preview_wallpapers');
874+
if (storedWallpapers) {
875+
const parsed = JSON.parse(storedWallpapers);
876+
if (Array.isArray(parsed) && parsed.length > 0) {
877+
WALLPAPERS = parsed;
878+
}
879+
}
880+
} catch (e) {
881+
console.error("Failed to load wallpapers from session", e);
882+
}
883+
862884
// Load Scenes
863885
try {
864886
await loadScenes();
@@ -873,7 +895,21 @@
873895
// 如果 URL 提供了壁纸 src
874896
if (params.src) {
875897
// 检查壁纸是否已存在于列表中
876-
const existingIndex = WALLPAPERS.indexOf(params.src);
898+
// Normalize URLs for comparison (handle absolute/relative)
899+
const normalize = (url) => {
900+
try {
901+
return new URL(url, window.location.href).href;
902+
} catch(e) { return url; }
903+
};
904+
const targetSrc = normalize(params.src);
905+
906+
let existingIndex = WALLPAPERS.findIndex(w => normalize(w) === targetSrc);
907+
908+
// Fallback: simple string inclusion if normalization fails to match
909+
if (existingIndex === -1) {
910+
existingIndex = WALLPAPERS.findIndex(w => w.includes(params.src) || params.src.includes(w));
911+
}
912+
877913
if (existingIndex !== -1) {
878914
initialWallIndex = existingIndex;
879915
} else {
@@ -937,8 +973,25 @@
937973
// 5. 初始对齐
938974
// 延时执行以确保布局渲染完毕
939975
setTimeout(() => {
940-
scrollToCard(els.sceneTrack, 0);
941-
scrollToCard(els.wallTrack, initialWallIndex);
976+
// 临时禁用 CSS 平滑滚动
977+
els.sceneTrack.classList.add('no-smooth');
978+
els.wallTrack.classList.add('no-smooth');
979+
980+
// 强制瞬间跳转
981+
scrollToCard(els.sceneTrack, 0, 'auto');
982+
scrollToCard(els.wallTrack, initialWallIndex, 'auto');
983+
984+
// 恢复平滑滚动 (下一帧恢复,确保跳转已执行)
985+
requestAnimationFrame(() => {
986+
setTimeout(() => {
987+
els.sceneTrack.classList.remove('no-smooth');
988+
els.wallTrack.classList.remove('no-smooth');
989+
990+
// Show tracks after position is stabilized
991+
els.sceneTrack.classList.add('ready');
992+
els.wallTrack.classList.add('ready');
993+
}, 50);
994+
});
942995
}, 100);
943996

944997
// 6. Render iOS UI
@@ -1103,14 +1156,14 @@
11031156
}
11041157

11051158
// 辅助:平滑滚动到指定卡片
1106-
function scrollToCard(container, index) {
1159+
function scrollToCard(container, index, behavior = 'smooth') {
11071160
const card = container.children[index];
11081161
if (!card) return;
11091162
// 计算让卡片居中的滚动位置
11101163
const scrollPos = card.offsetTop - (container.clientHeight / 2) + (card.offsetHeight / 2);
11111164
container.scrollTo({
11121165
top: scrollPos,
1113-
behavior: 'smooth'
1166+
behavior: behavior
11141167
});
11151168
}
11161169

@@ -1244,8 +1297,11 @@
12441297
});
12451298
};
12461299

1247-
// Trigger load
1300+
// 触发 load
12481301
els.base.src = s.src;
1302+
1303+
// 更新背景 (如果是 Scene 模式,背景随场景变)
1304+
updateAmbientBackground();
12491305
}
12501306

12511307
function updateWallpaper(idx) {
@@ -1260,12 +1316,33 @@
12601316
// 3. Raw Image
12611317
if(els.rawImg) els.rawImg.src = url;
12621318

1319+
// 更新背景 (如果是 UI/Raw 模式,背景随壁纸变)
1320+
updateAmbientBackground();
1321+
}
1322+
1323+
// 统一背景更新逻辑
1324+
function updateAmbientBackground() {
1325+
let bgUrl = '';
1326+
1327+
if (state.viewMode === 'scene') {
1328+
// Scene 模式:背景使用场景图
1329+
if (SCENES[state.sceneIndex]) {
1330+
bgUrl = SCENES[state.sceneIndex].src;
1331+
}
1332+
} else {
1333+
// 其他模式:背景使用壁纸
1334+
bgUrl = WALLPAPERS[state.wallIndex];
1335+
}
1336+
1337+
if (!bgUrl || state.currentBgUrl === bgUrl) return;
1338+
state.currentBgUrl = bgUrl;
1339+
12631340
// 环境光背景切换 (Cross-fade)
12641341
state.bgFlip = !state.bgFlip;
12651342
const activeBg = state.bgFlip ? els.bg2 : els.bg1;
12661343
const inactiveBg = state.bgFlip ? els.bg1 : els.bg2;
12671344

1268-
activeBg.style.backgroundImage = `url(${url})`;
1345+
activeBg.style.backgroundImage = `url(${bgUrl})`;
12691346
activeBg.classList.add('active');
12701347
inactiveBg.classList.remove('active');
12711348
}
@@ -1290,6 +1367,9 @@
12901367
}
12911368
});
12921369

1370+
// 切换模式时,检查是否需要更新背景
1371+
updateAmbientBackground();
1372+
12931373
// Ensure current wallpaper is loaded for the new mode
12941374
// (Usually handled by updateWallpaper, but strictly enforcing here for safety)
12951375
const url = WALLPAPERS[state.wallIndex];

0 commit comments

Comments
 (0)