如何用CSS实现加粗标题hover变斜体并修复滚动偏移问题?
解决CSS滚动标题偏移+Hover样式问题
修改后代码(两种方案可选)
方案1:Hover时原标题直接变斜体(无滚动)
适合不需要滚动效果,仅需标题样式变化的场景:
*, *::after, *::before { box-sizing: border-box; } :root { font-size: 15px; } body { margin: 0; --color-text: #000000; --color-bg: #000000; --color-link: #000000; --color-link-hover: #000000; color: var(--color-text); background-color: var(--color-bg); -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; font-family: sofia-pro, sans-serif; } a { text-decoration: none; color: var(--color-link); outline: #cec7ff; } a:hover, a:focus { color: var(--color-link-hover); outline: none; } .menu { -webkit-touch-callout: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; padding: 10vh 0 25vh; --item-font-size: 5vw; counter-reset: menu; } .menu__item { cursor: default; position: relative; padding: 0 5vw; } .menu__item-link { display: inline-block; cursor: pointer; position: relative; -webkit-text-stroke: 0px #000; text-stroke: 0px #000; -webkit-text-fill-color: #000000; text-fill-color: transparent; color: transparent; font-weight: 900; transition: all 0.4s; } /* Hover时标题直接变斜体,不隐藏 */ .menu__item-link:hover { font-style: italic; opacity: 1; } .menu__item-img { pointer-events: none; position: absolute; height: 30vh; max-height: 400px; opacity: 0; left: 120%; top: 50%; transform: translate3d(calc(-100% - 6vw),-30%,0) translate3d(0,10px,0); transition: all 0.2s; } .menu__item-link:hover + .menu__item-img { opacity: 1; transform: translate3d(calc(-100% - 6vw),-30%,0); } /* 移除滚动相关的marquee元素 */ .marquee { display: none; } @media screen and (min-width: 53em) { .frame { text-align: left; display: grid; align-content: space-between; width: 100%; padding: 2.5rem 6vw; pointer-events: none; grid-template-columns: 75% 25%; grid-template-rows: auto; } }
方案2:保留滚动斜体标题(移除偏移+解决多标题问题)
适合需要保留滚动效果的场景:
*, *::after, *::before { box-sizing: border-box; } :root { font-size: 15px; } body { margin: 0; --color-text: #000000; --color-bg: #000000; --color-link: #000000; --color-link-hover: #000000; color: var(--color-text); background-color: var(--color-bg); -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; font-family: sofia-pro, sans-serif; } a { text-decoration: none; color: var(--color-link); outline: #cec7ff; } a:hover, a:focus { color: var(--color-link-hover); outline: none; } .menu { -webkit-touch-callout: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; padding: 10vh 0 25vh; --marquee-width: 100vw; --offset: 0vw; /* 移除偏移值 */ --move-initial: 0; /* 滚动起始位置对齐视口左侧 */ --move-final: calc(-100%); /* 滚动结束位置完全移出视口 */ --item-font-size: 5vw; counter-reset: menu; } .menu__item { cursor: default; position: relative; padding: 0 5vw; } .menu__item-link { display: inline-block; cursor: pointer; position: relative; -webkit-text-stroke: 0px #000; text-stroke: 0px #000; -webkit-text-fill-color: #000000; text-fill-color: transparent; color: transparent; transition: opacity 0.4s; } .menu__item-link:hover { transition-duration: 0.1s; opacity: 0; } .menu__item-img { pointer-events: none; position: absolute; height: 30vh; max-height: 400px; opacity: 0; left: 120%; top: 50%; transform: translate3d(calc(-100% - 6vw),-30%,0) translate3d(0,10px,0); transition: all 0.2s; } .menu__item-link:hover + .menu__item-img { opacity: 1; transform: translate3d(calc(-100% - 6vw),-30%,0); } .marquee { position: absolute; top: 0; left: 5vw; /* 对齐原标题的padding,避免偏移 */ width: var(--marquee-width); overflow: hidden; pointer-events: none; mix-blend-mode: color-burn; } .marquee__inner { width: fit-content; display: flex; position: relative; transform: translate3d(var(--move-initial), 0, 0); animation: marquee 6s linear infinite; animation-play-state: paused; opacity: 0; transition: opacity 0.1s; } .menu__item-link:hover ~ .marquee .marquee__inner { animation-play-state: running; opacity: 1; transition-duration: 0.4s; } .marquee span { text-align: left; white-space: nowrap; font-size: var(--item-font-size); padding: 0 1vw; font-weight: 900; line-height: 1.15; font-style: italic; } .menu__item-link { white-space: nowrap; font-size: var(--item-font-size); padding: 0 1vw; font-weight: 900; line-height: 1.15; } @keyframes marquee { 0% { transform: translate3d(var(--move-initial), 0, 0); } 100% { transform: translate3d(var(--move-final), 0, 0); } } @media screen and (min-width: 53em) { .frame { text-align: left; display: grid; align-content: space-between; width: 100%; padding: 2.5rem 6vw; pointer-events: none; grid-template-columns: 75% 25%; grid-template-rows: auto; } }
关键修改点说明
- 移除偏移值:将
.menu中的--offset设为0vw,同时调整滚动动画的起始/结束位置,消除额外偏移。 - 解决多标题偏移问题:
- 方案1直接移除滚动的marquee元素,让原标题hover时直接变斜体,彻底避免多标题问题。
- 方案2调整
.marquee的left值对齐原标题的padding,优化动画位置参数,确保滚动时仅显示单个斜体标题且位置准确。
- 保留图片hover效果:完全保留原代码中图片的hover显示逻辑,hover标题时图片正常弹出。
内容的提问来源于stack exchange,提问作者Buura
相关产品推荐
相关产品推荐

