原生js实现摩天轮菜单栏-弧形菜单栏-圆形菜单栏

你猜 阅读:186 2022-06-06 14:58:02 评论:0

长话短说,废话少说

  • 看效果

摩天轮菜单.gif

  • 上代码
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8">  
<title>菜鸟教程(runoob.com)</title> 
<style> 
 
#div1 
{ 
  position: relative; 
  height: 200px; 
  width: 200px; 
  border: 1px solid black; 
  border-radius:50%; 
  transition: transform 1s 
} 
 
#div2 
{  width:50px; 
  height:30px; 
  position: absolute; 
  //top:15px; 
  left:calc(50% - 25px); 
  border: 1px solid black; 
  background-color: red; 
  transform: rotate(0deg); 
  transform-origin:25px 100px; 
} 
   
#div3 
{  width:50px; 
  height:30px; 
  position: absolute; 
  //top:15px; 
  left:calc(50% - 25px); 
  //border: 1px solid black; 
  //background-color: red; 
  transform: rotate(45deg); 
  transform-origin:25px 100px; 
} 
#div3 .content { 
    width:100%; 
    height:100%; 
    border: 1px solid black; 
    background-color: gray; 
    transform: rotate(-45deg);                     
    transition: transform 1s 
  } 
   
#div4 
{  width:50px; 
  height:30px; 
  position: absolute; 
  //top:15px; 
  left:calc(50% - 25px); 
  transform: rotate(90deg); 
  transform-origin:25px 100px; 
} 
#div4 .content { 
    width:100%; 
    height:100%; 
    border: 1px solid black; 
    background-color: pink; 
    transform: rotate(-90deg); 
    transition: transform 1s 
  } 
   
#div5 
{  width:50px; 
  height:30px; 
  position: absolute; 
  //top:15px; 
  left:calc(50% - 25px); 
  border: 1px solid black; 
  background-color: red; 
  transform: rotate(135deg); 
  transform-origin:25px 100px; 
} 
   
#div6 
{  width:50px; 
  height:30px; 
  position: absolute; 
  //top:15px; 
  left:calc(50% - 25px); 
  border: 1px solid black; 
  background-color: red; 
  transform: rotate(180deg); 
  transform-origin:25px 100px; 
} 
 
#div7 
{  width:50px; 
  height:30px; 
  position: absolute; 
  //top:15px; 
  left:calc(50% - 25px); 
  border: 1px solid black; 
  background-color: red; 
  transform: rotate(225deg); 
  transform-origin:25px 100px; 
} 
</style> 
</head> 
 
<body> 
 
<div id="div1"> 
  <div id="div2"></div> 
  <div id="div3"> 
    <div class='content' id='test'></div> 
  </div> 
  <div id="div4"> 
    <div class='content' id='test1'></div> 
  </div> 
  <div id="div4"></div> 
  <div id="div5"></div> 
  <div id="div6"></div> 
  <div id="div7"></div> 
</div> 
   
<hr/> 
<button id='rotateButton'>旋转</button> 
 
<script> 
  let count = 0 
  const btn = document.getElementById('rotateButton') 
  const circle = document.getElementById('div1') 
  const test = document.getElementById('test') 
  const test1 = document.getElementById('test1') 
  btn.addEventListener('click',function(){ 
    count = count + 45 
    let testCount = -count-45 
    let testCount1 = -count-90 
    circle.style.transform = 'rotate('+count+'deg)' 
    test.style.transform = 'rotate('+testCount+'deg)' 
    test1.style.transform = 'rotate('+testCount1+'deg)' 
  }) 
</script> 
</body> 
</html>
  • 以上是个小demo,相信聪明的你把它跑起来之后稍加调试就能领会其中奥妙了

摩天轮菜单demo.gif

  • 讲思路
  • 摆位置。把静态的位置排列放好
  • 公转。需要旋转的时候整体去旋转,此时各个item位置自然会发生偏移
  • 自传。如上图,不作处理的话item的朝向永远是向着容器圆心的,所以需要item自转
  • 在需要旋转时公转和自转同时进行
  • 加个transition过渡
  • 齐活

客官留步,难道你不喜欢老夫的封面吗?还不收藏加关注?关注过百放一波福利哟


标签:前端
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

搜索
排行榜
关注我们

一个IT知识分享的公众号