css实现扇形展开的圆形菜单

废话不多说,直接上代码

效果

在这里插入图片描述

在线体验

😄点击体验😄

源码

html

<div class="fab-menu">
  <div class="trigger">
    +
  </div>
  <div class="menu-list">
    <div class="menu-item">
      <div class="icon-text">
        <div>社区</div>
      </div>
    </div>
    <div class="menu-item">
      <div class="icon-text" >
        <div>社群</div>
      </div>
    </div>
    <div class="menu-item">
      <div class="icon-text">
        <div>组局</div>
      </div>
    </div>
    <div class="menu-item">
      <div class="icon-text">
        <div></div>
      </div>
    </div>
  </div>
</div>

css

.fab-menu {
  position: fixed;
  left: 200px;
  top: 150px;
}

.fab-menu .trigger {
  width: 60px;
  height: 60px;
  position: absolute;
  z-index: 1;
  left: -30px;
  top: -30px;
  text-align: center;
  line-height: 55px;
  transition: all 0.5s;
  background-color: #ff0000;
  border-radius: 50%;
  font-size: 40px;
  cursor: pointer;
  color: #fff;
}

.fab-menu .trigger.open {
  transform: rotate(45deg);
}

.fab-menu .menu-list {
  position: relative;
  height: 125px;
  width: 125px;
  transform: rotate(45deg);
  transform-origin: 0 0;
}


.fab-menu .menu-list .menu-item {
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0;
  border-radius: 0 0 100% 0;
  transform-origin: 0 0;
  overflow: hidden;
  transition: all 0.5s;
  transform: rotate(-90deg);
  box-shadow: 0rpx 0px 25px 0px rgba(0, 0, 0, 0.1);
  opacity: 0;
  font-size: 29rpx;
}

.fab-menu .menu-list .menu-item .icon-text {
  transform-origin: 0 0;
  position: absolute;
  text-align: center;
}

.fab-menu .menu-list .menu-item:nth-child(1) {
  background-color: #cdff55;
}

.fab-menu .menu-list .menu-item:nth-child(1) .icon-text {
  transform: rotate(-45deg);
  top: 47px;
  left: 15px;
}

.fab-menu .menu-list .menu-item:nth-child(2) {
  background-color: #fbff66;
}



.fab-menu .menu-list .menu-item:nth-child(2) .icon-text {
  transform: rotate(-133deg);
  top: 80px;
  left: 45px;
}

.fab-menu .menu-list .menu-item:nth-child(3) {
  background-color: #e6ff49;
}

.fab-menu .menu-list .menu-item:nth-child(3) .icon-text {
  transform: rotate(-225deg);
  top: 55px;
  left: 80px;
}

.fab-menu .menu-list .menu-item:nth-child(4) {
  background-color: #fbff66;
}

.fab-menu .menu-list .menu-item:nth-child(4) .icon-text {
  transform: rotate(45deg);
  top: 35px;
  left: 30px;
}


/* 定义菜单展开后的状态 */
.fab-menu .menu-list.open .menu-item {
  opacity: 1;
}

.fab-menu .menu-list.open .menu-item:nth-child(1) {
  transform: rotate(0deg);
}

.fab-menu .menu-list.open .menu-item:nth-child(2) {
  transform: rotate(90deg);
}

.fab-menu .menu-list.open .menu-item:nth-child(3) {
  transform: rotate(180deg);
}

.fab-menu .menu-list.open .menu-item:nth-child(4) {
  transform: rotate(270deg);
}

js


var t = document.querySelector('.trigger')
var m = document.querySelector('.menu-list')

// add or delete class name 'open'
t.addEventListener('click', () => {
  t.classList.toggle('open');
  m.classList.toggle('open');
})
实现点击扇形展开效果菜单,需要使用CSS中的transform和transition属性。以下是一种实现方式: HTML结构: ```html <div class="menu"> <div class="item item1"></div> <div class="item item2"></div> <div class="item item3"></div> <div class="item item4"></div> <div class="item item5"></div> <div class="center"></div> </div> ``` CSS样式: ```css .menu { position: relative; width: 200px; height: 200px; } .item { position: absolute; width: 50px; height: 50px; background-color: #ccc; border-radius: 50%; transition: transform 0.5s ease; } .item1 { top: -25px; left: 75px; } .item2 { top: 35px; left: 35px; } .item3 { top: 75px; left: -25px; } .item4 { top: 35px; left: -85px; } .item5 { top: -25px; left: -45px; } .center { position: absolute; top: 50%; left: 50%; width: 20px; height: 20px; background-color: #333; border-radius: 50%; transform: translate(-50%, -50%); cursor: pointer; } ``` JavaScript代码: ```javascript var menu = document.querySelector('.menu'); var items = document.querySelectorAll('.item'); var center = document.querySelector('.center'); var isOpen = false; center.addEventListener('click', function() { if (isOpen) { // 关闭菜单 menu.classList.remove('open'); items.forEach(function(item, index) { item.style.transform = 'rotate(0deg)'; }); } else { // 打开菜单 menu.classList.add('open'); items.forEach(function(item, index) { item.style.transform = 'rotate(' + (index * 72) + 'deg)'; }); } isOpen = !isOpen; }); ``` 在上面的代码中,我们通过JavaScript监听中心圆的点击事件,并根据菜单是否打开来决定是展开还是收起菜单。当菜单打开时,我们给菜单容器添加open类名,并将每个菜单项按照一定角度旋转,从而形成扇形展开效果。当菜单关闭时,我们将open类名移除,并将每个菜单项旋转回初始状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值