/* 孕期专题页共用渲染(2026-11-01) * 用户:「其他妈妈也在搜」剩下的问题都做成专题页,UI 和文字参考「叶酸、DHA、铁、钙要不要补」(topic/buji)。 * 每个专题目录只放 index.html(内联 window.TOPIC 数据)+ img/hero.webp,版式全在这里和 article.css。 * 区块 type:cards 卡片矩阵 / timeline 按孕周横向条 / vs 两栏对照 / list 清单 / steps 步骤 / doc 找医生 / check 自查。 * 自成一页:不引 app.js;返回回 ../../;工具跳转走 ../_shared/tools.js(同源路径 + search + hash)。 */ (function () { /* 11-09 小程序「复制链接」直达:加载 js/host-nav.js(容器页返回走 navigateBack、页内链接开容器页) */ try { if (!window.MBHost && document.currentScript) { var hs = document.createElement('script'); hs.src = new URL('../../js/host-nav.js?v=26110907', document.currentScript.src).href; document.head.appendChild(hs); } } catch (e) {} 'use strict'; var D = window.TOPIC; if (!D) return; var $ = function (s, r) { return (r || document).querySelector(s); }; var $$ = function (s, r) { return [].slice.call((r || document).querySelectorAll(s)); }; function esc(s) { return String(s == null ? '' : s).replace(/[&<>"]/g, function (c) { return { '&': '&', '<': '<', '>': '>', '"': '"' }[c]; }); } /* 只放行 :答案里用它标重点,其余一律转义 */ function em(s) { return esc(s).replace(/<(\/?)em>/g, '<$1em>'); } function hp(n) { var h = (location.hash.slice(1) || '') + '&' + (location.search.slice(1) || ''); var m = h.match(new RegExp('(?:^|&)' + n + '=([^&]*)')); return m ? decodeURIComponent(m[1]) : ''; } function ic(id) { return ''; } /* 主色 */ var C = D.color || ['#2E7CF6', '46,124,246', '#EAF2FF']; var root = document.documentElement.style; root.setProperty('--c', C[0]); root.setProperty('--cr', C[1]); root.setProperty('--cb', C[2]); var SPRITE = ''; function title() { var out = ''; (D.title || [D.q]).forEach(function (p, i) { if (p === '\n') out += '
'; else out += i === 2 ? '' + esc(p) + '' : esc(p); }); return out; } function sh(s, i) { return '
' + (i < 9 ? '0' : '') + (i + 1) + '

' + esc(s.title) + '

' + (s.en ? '' + esc(s.en) + '' : '') + '
' + (s.key ? '

' + esc(s.key) + '

' : ''); } var R = { cards: function (s) { var h = '
'; (s.items || []).forEach(function (x) { h += '
' + esc(x.name) + '' + (x.verdict ? '' + esc(x.verdict) + '' : '') + '
' + (x.label ? '' + esc(x.label) + '' : '') + '' + esc(x.amt) + (x.unit ? '' + esc(x.unit) + '' : '') + '' + (x.when ? '
' + ic('clock') + '' + esc(x.when) + '
' : '') + '
'; }); h += '
'; if (s.why) h += '
' + esc(s.why.q) + ic('down') + '

' + esc(s.why.a) + '

'; return h; }, timeline: function (s, i) { var h = '
'; (s.cols || []).forEach(function (c, k) { h += '' + esc(c) + ''; }); h += '
'; (s.rows || []).forEach(function (r) { h += '
' + esc(r.name) + '
'; (r.bars || []).forEach(function (b) { h += '' + esc(b.t) + ''; }); h += '
'; }); h += '
'; (s.legend || [['', '重点'], ['lt', '日常'], ['dash', '听医生']]).forEach(function (l) { h += '' + esc(l[1]) + ''; }); return h + '
'; }, vs: function (s) { var hd = s.heads || ['', '']; var h = '
' + esc(hd[0]) + '' + esc(hd[1]) + '
'; (s.rows || []).forEach(function (r) { h += '
' + esc(r.name) + '' + esc(r.a) + '' + esc(r.b) + '
'; }); h += '
'; if (s.note) h += '

' + esc(s.note) + '

'; return h; }, list: function (s) { var h = ''; }, steps: function (s) { var h = '
    '; (s.items || []).forEach(function (x) { h += '
  1. ' + esc(x.b) + '' + (x.s ? '' + esc(x.s) + '' : '') + '
  2. '; }); return h + '
'; }, doc: function (s) { var h = '
' + ic('doc') + '
' + esc(s.b) + '' + esc(s.s) + '
'; (s.tags || []).forEach(function (t) { h += '' + esc(t) + ''; }); return h + '
'; }, check: function (s, i) { var h = '
'; (s.qs || []).forEach(function (q, k) { h += '
Q' + (k + 1) + '' + esc(q.q) + '
'; (q.opts || []).forEach(function (o, j) { h += ''; }); h += '
'; }); return h + '

' + esc(s.hint || ((s.qs || []).length + ' 题选完自动出结果,只在本机计算')) + '

' + '

给你的建议

    仅供参考,具体以产检医生意见为准。

    '; } }; var h = SPRITE + '
    孕期专题
    ' + '
    ' + esc(D.eyebrow) + '

    ' + title() + '

    ' + (D.sub ? '

    ' + esc(D.sub) + '

    ' : '') + '
    ' + '
    ' + esc(D.heroAlt || '') + '
    ' + '
    一句话答案

    ' + em(D.answer) + '

    '; if (D.three && D.three.length) { h += '

    记住这 ' + D.three.length + ' 点

      '; D.three.forEach(function (x, i) { h += '
    1. ' + (i + 1) + '
      ' + esc(x.b) + '' + esc(x.s) + '
    2. '; }); h += '
    '; } (D.sections || []).forEach(function (s, i) { if (!R[s.type]) return; h += '
    ' + sh(s, i) + R[s.type](s, i) + '
    '; }); h += '
    '; if (D.sources && D.sources.length) { h += '
    参考来源' + ic('down') + '
      '; D.sources.forEach(function (x) { h += '
    • ' + esc(x) + '
    • '; }); h += '
    '; } h += '

    本页为科普参考,不能代替医生诊断。

    ' + '
    兜爸心选孕育知识整理
    '; /* 11-02 品牌落款(样式在 article.css) */ document.body.insertAdjacentHTML('afterbegin', h); document.title = D.q || document.title; var sc = $('#scroll'); $('#back').addEventListener('click', function () { if (window.MBHost && MBHost.back(function () { location.href = '../../' + location.search + location.hash; })) return; /* 11-09 容器页:回小程序上一页 */ if (document.referrer && document.referrer.indexOf(location.host) >= 0 && history.length > 1) history.back(); else location.href = '../../' + location.search + location.hash; }); sc.addEventListener('scroll', function () { $('#bar').classList.toggle('on', sc.scrollTop > 8); }, { passive: true }); function goTo(el) { var top = el.getBoundingClientRect().top - sc.getBoundingClientRect().top + sc.scrollTop - 60; sc.scrollTo({ top: top, behavior: 'smooth' }); } /* 推荐工具 */ function renderTools(wk) { if (!window.TopicTools || !D.tools) return; var el = $('#tools'); el.className = 'fade in'; TopicTools.render(el, D.tools.items || [], { title: D.tools.title || '用得上的工具', color: C[0], bg: C[2], wk: wk || 0 }); } renderTools(0); /* 自查:每题选一个,全部选完按 a→t→k 排出最多 3 条 */ var TAG = { a: '问医生', t: '加一点', k: '保持' }, ORD = { a: 0, t: 1, k: 2 }; $$('[data-check]').forEach(function (box) { var s = D.sections[+box.dataset.check], ans = {}; $$('.q', box).forEach(function (qb) { $$('.opt', qb).forEach(function (o) { o.addEventListener('click', function () { $$('.opt', qb).forEach(function (x) { x.classList.toggle('on', x === o); }); ans[qb.dataset.q] = +o.dataset.v; var res = $('.res', box), first = !res.classList.contains('on'); if (Object.keys(ans).length < s.qs.length) return; var L = s.qs.map(function (q, k) { return q.opts[ans[k]].adv; }).filter(Boolean); L.sort(function (x, y) { return (ORD[x.tone] || 0) - (ORD[y.tone] || 0); }); var ul = $('ul', res); ul.innerHTML = ''; L.slice(0, 3).forEach(function (it) { var li = document.createElement('li'); li.className = it.tone; li.innerHTML = '
    '; li.querySelector('i').textContent = TAG[it.tone] || ''; li.querySelector('b').textContent = it.b; li.querySelector('span').textContent = it.s || ''; ul.appendChild(li); }); $('.hint', box).style.display = 'none'; res.classList.add('on'); if (first) setTimeout(function () { goTo(res); }, 60); }); }); }); }); /* 按孕周点亮 timeline 那一列 */ function markWeek(w, post) { (D.sections || []).forEach(function (s, i) { if (s.type !== 'timeline' || !s.weeks) return; var col = -1; s.weeks.forEach(function (r, k) { if (r && !post && w >= r[0] && w < r[1]) col = k; }); if (post) s.cols.forEach(function (c, k) { if (/产后|哺乳|月子|出生/.test(c)) col = k; }); if (col < 0) return; var sec = $('#s' + (i + 1)), hd = $('.gr.hd [data-c="' + col + '"]', sec), gt = $('[data-gt]', sec), box = $('.col-hl', sec); function place() { var a = hd.getBoundingClientRect(), g = gt.getBoundingClientRect(); box.style.left = (a.left - g.left - 1) + 'px'; box.style.width = (a.width + 2) + 'px'; box.classList.add('on'); } place(); window.addEventListener('resize', place); hd.style.color = C[0]; if (s.tips && s.tips[col]) { var me = $('[data-me]', sec); me.textContent = (post ? '宝宝出生了' : '你在孕 ' + w + ' 周') + ':' + s.tips[col]; me.classList.add('on'); } }); } var openid = hp('openid'); if (openid) { fetch('/api/mp/public/mama-box/pregnancy?openid=' + encodeURIComponent(openid)) .then(function (r) { return r.ok ? r.json() : null; }) .then(function (j) { var p = j && j.code === 200 && j.result; if (!p) return; if (p.postpartum) markWeek(0, true); else if (p.week != null && !p.empty) { markWeek(p.week, false); renderTools(p.week); } }).catch(function () {}); } /* 进场 fade-up */ $$('.fade').forEach(function (el) { $$('.fi', el).forEach(function (c, i) { c.style.transitionDelay = (i * 60 + 80) + 'ms'; }); }); if ('IntersectionObserver' in window) { var io = new IntersectionObserver(function (es) { es.forEach(function (e) { if (e.isIntersecting) { e.target.classList.add('in'); io.unobserve(e.target); } }); }, { root: sc, threshold: .08 }); $$('.fade').forEach(function (el) { io.observe(el); }); } else $$('.fade').forEach(function (el) { el.classList.add('in'); }); /* 禁止下拉回弹 */ var y0 = 0; sc.addEventListener('touchstart', function (e) { y0 = e.touches[0].clientY; }, { passive: true }); sc.addEventListener('touchmove', function (e) { var dy = e.touches[0].clientY - y0; if ((sc.scrollTop <= 0 && dy > 0) || (sc.scrollTop + sc.clientHeight >= sc.scrollHeight - 1 && dy < 0)) e.preventDefault(); }, { passive: false }); document.addEventListener('touchmove', function (e) { if (!sc.contains(e.target)) e.preventDefault(); }, { passive: false }); })();