示例
实用示例,实时演示。
挑一个示例全屏打开,运行效果和源码并排呈现。
挑一个示例全屏打开,运行效果和源码并排呈现。
一个通过 CSS 过渡变成叉号的菜单图标,还有一颗点击时用 SMIL 动画弹一下的心。
import 'nuclo' let open = false let liked = false const line = (y: number, transform: () => string) => lineSvg({ x1: 4, y1: y, x2: 20, y2: y, style: () => ({ transition: 'transform 0.3s', transformOrigin: '12px 12px', transform: transform(), }), }) // Menu → cross: each line's CSS transform is a function of `open`. const MenuIcon = () => svgSvg( { viewBox: '0 0 24 24', width: '64', height: '64', fill: 'none', stroke: '#222', 'stroke-width': 2, 'stroke-linecap': 'round', style: { cursor: 'pointer' }, onClick: () => { open = !open; update() }, }, line(6, () => (open ? 'translateY(6px) rotate(45deg)' : '')), line(12, () => (open ? 'scaleX(0)' : '')), line(18, () => (open ? 'translateY(-6px) rotate(-45deg)' : '')), ) // Heart: the fill is a function of `liked`. A SMIL scale that // begins on click pops it, with no state of its own. const HeartIcon = () => svgSvg( { viewBox: '0 0 24 24', width: '64', height: '64', stroke: '#ff3f00', 'stroke-width': 2, 'stroke-linejoin': 'round', style: { cursor: 'pointer' }, onClick: () => { liked = !liked; update() }, }, gSvg( { transform: 'translate(12 12)' }, animateTransformSvg({ attributeName: 'transform', type: 'scale', additive: 'sum', values: '1;1.3;1', dur: '0.35s', begin: 'click', }), pathSvg({ transform: 'translate(-12 -12)', d: 'M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7Z', style: { transition: 'fill 0.2s' }, fill: () => (liked ? '#ff3f00' : 'none'), }), ), ) // Both icons side by side, and a text that reads the same two variables. export const IconToggles = () => div( div( { style: { display: 'flex', gap: '28px', justifyContent: 'center' } }, MenuIcon(), HeartIcon(), ), div( { style: { textAlign: 'center', fontSize: '12px', color: '#666' } }, () => `menu ${open ? 'open' : 'closed'} · ${liked ? 'liked' : 'not liked'}`, ), )