You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现电话按钮通过Transition扩展宽度且文本不换行?

解决电话按钮过渡时文本换行的问题

核心解决方案

给显示电话号码的<span id="tel">添加以下CSS样式,强制文本保持单行,避免过渡过程中换行:

#tel {
  white-space: nowrap;
  overflow-x: hidden;
}

样式作用说明

  • white-space: nowrap:强制文本始终在同一行显示,不会因容器宽度不足自动换行。
  • overflow-x: hidden:当容器宽度还没过渡到足够容纳文本时,隐藏超出容器的部分,而非换行展示。

代码优化建议(可选)

把JS中直接操作内联样式的逻辑改成通过CSS类切换,更便于维护和扩展:

  1. 新增控制状态的CSS类:
.tel-parent-expanded {
  width: 190px !important;
}
.increase-width-expanded {
  width: 100% !important;
}
.tel-visible {
  display: block !important;
}
  1. 简化JS事件逻辑:
const increaseWidthBtn = document.getElementById("increase-width");
const telParent = document.querySelector('#tel-parent');
const tel = document.querySelector('#tel');

increaseWidthBtn.addEventListener('mouseover', function() {
  telParent.classList.add('tel-parent-expanded');
  this.classList.add('increase-width-expanded');
  tel.classList.add('tel-visible');
});

increaseWidthBtn.addEventListener('mouseout', function() {
  telParent.classList.remove('tel-parent-expanded');
  this.classList.remove('increase-width-expanded');
  tel.classList.remove('tel-visible');
});

这样修改后,样式逻辑集中在CSS中,JS仅负责状态切换,代码结构更清晰。

内容的提问来源于stack exchange,提问作者rozhan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 14:20:26