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

如何修改圆形旋转菜单代码 让选中项最终旋转定位到90度而非0度

解决方案

仅需修改JS代码中move函数的endAngle计算逻辑即可,修改后点击选中的菜单项会旋转到90度(正右方)位置停止:

修改说明

原来的endAngle计算的是将选中项移动到初始0度(正上方)位置的目标角度,现在额外偏移Math.PI / 2(即90度对应的弧度值),就能让选中项停在正右方。额外添加的+ 2 * Math.PI和取模操作是为了避免负角度导致旋转路径异常。

修改后完整JS代码

const buttons = Array.from(document.querySelectorAll('.result-button'))
const count = buttons.length
const increase = Math.PI * 2 / buttons.length
const radius = 125
let angle = 0

buttons.forEach((button, i) => {
  button.style.top = Math.sin(-Math.PI / 2 + i * increase) * radius + 'px'
  button.style.left = Math.cos(-Math.PI / 2 + i * increase) * radius + 'px'
  button.addEventListener('click', move)
})

function move(e) {
  const n = buttons.indexOf(e.target)
  // 仅修改这一行的偏移量即可调整停止位置
  const endAngle = ((n % count) * increase - Math.PI / 2 + 2 * Math.PI) % (2 * Math.PI)
  console.log(endAngle)
  turn()

  function turn() {
    if (Math.abs(endAngle - angle) > 1 / 8) {
      const sign = endAngle > angle ? 1 : -1
      angle = angle + sign / 8
      setTimeout(turn, 20)
    } else {
      angle = endAngle
    }
    buttons.forEach((button, i) => {
      button.style.top = Math.sin(-Math.PI / 2 + i * increase - angle) * radius + 'px'
      button.style.left = Math.cos(-Math.PI / 2 + i * increase - angle) * radius + 'px'
    })
  }
}

可选调整说明

如果需要修改停止位置到其他角度,仅需调整endAngle计算中的偏移量即可,对应关系如下:

  • 正上方(原默认停止位置):偏移量为0
  • 正右方(90度,本次需求指定位置):偏移量为- Math.PI / 2
  • 正下方(180度):偏移量为- Math.PI
  • 正左方(270度):偏移量为- Math.PI * 1.5

HTML和CSS代码无需修改,和原有代码保持一致即可正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 10:36:04