RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 811094
Accepted
Meetromb
Meetromb
Asked:2020-04-08 11:08:41 +0000 UTC2020-04-08 11:08:41 +0000 UTC 2020-04-08 11:08:41 +0000 UTC

围绕圆定位元素

  • 772

再会。有一个任务:

围绕圆圈排列元素,使它们相互影响,即 如果我们将鼠标悬停在一个元素上,其他元素就会移到一边。

所需结构:

<div class="container">
   <div class="item">
   <div class="item">
   <div class="item">
   <div class="item">
   <div class="item">
</div>

默认状态悬停时

javascript
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Meetromb
    2020-04-08T11:08:41Z2020-04-08T11:08:41Z

    互联网上有什么?

    1. 按度定位元素(不是我们的情况) - https://codepen.io/HugoGiraudel/pen/vEJXGm
    2. 均匀分布在整个圆周上。该选项很有趣,但不依赖于元素的大小 - https://codepen.io/pat_hg/pen/JYEZMe?limit=all&page=3&q=circle
    3. 属性shape-outside,仅适用于文本(根本不适用) - https://css-tricks.com/almanac/properties/s/shape-outside/

    如何实施?

    1. 我们确定我们的元素将占据的扇区的角落。我在和弦上做了计算,和弦也需要计算,因为 它只是радиус * 2行不通 - 元素之间会有差异。
    2. 将角与元素的中心相加
    3. 计算圆上的一个点并将坐标分配给一个元素

    现在我们需要计算与元素交互时的角度:

    1. 悬停时,您可以更改任何内容,但对我们来说主要作用是size。我们将新的大小写入缓冲区或元素的属性并重新计算位置

    2. 好的,改变元素的角度是根据旧公式计算的,它们相应变大,元素向右移动。

    3. 但是如果你想让元素在两边移动,那么你需要将之前的(初始)大小值写入缓冲区,然后计算差异并将每个元素的角度减少到改变元素角度的一半。

    带注释的工作代码:

    var container = $('.container');
    var radius = $('.container').height() / 2;
    var item = $('.item');
    
    var leftAngle = 0;
    
    //start angle
    var angle = -Math.PI / 2; // 90 deg
    
    //angle steps between elements
    var step = Math.PI / 12; //15 deg
    
    var multiplier = 1;
    
    function calcPositions() {
      //calculate left rotation angle 
      item.each(function() {
        //difference in sizes if item hovered
        var sizesDiff = $(this).attr('data-width') - $(this).attr('data-before');
      
        //if sizes are different, calc angle to rotate items to left
        if(sizesDiff != 0 && leftAngle == 0) {
          leftAngle = angleByChord($(this).attr('data-width') / 2, radius) / 4;
        }
      });
      
      //rotate items to left
      if(leftAngle != 0) {
        angle -= leftAngle;
      }
      
      leftAngle = 0;
      
      item.each(function(index) {
        
        //if first, no rotation
        if(index == 0) {
          multiplier = 0;
        } else {
          multiplier = 1;
        }
        
        //get container circle section angle
        var thisItemAngleSection = angleByChord($(this).attr('data-width') / 2, radius);
        
        //this aitem angle rotation
        angle += ((thisItemAngleSection / 2) + step) * multiplier;
        
        //calculate positions
        var posX = radius + radius * Math.cos(angle);
        var posY = radius + radius * Math.sin(angle);
        
        //set half of angle to next element
        multiplier = 1
        angle += ((thisItemAngleSection / 2) + step) * multiplier;
        
        $(this).css({
          left: posX + 'px',
          top: posY + 'px'
        });
      });
      
      //reset
      angle = -Math.PI / 2;
    }
    
    //get angle of item 
    function angleByChord(this_radius, radius) {
      var a = (2*radius*radius - this_radius*this_radius) / (2 * radius);
      
      var chord = Math.sqrt(radius*radius - a*a) * 2;
      
      return Math.asin(chord / (2 * radius)) * 2;
    }
    
    /*  actions  */
    $('.start').on('click', function() {
      calcPositions();
    });
    
    $('.item').hover(function() {
      $(this).attr('data-before', $(this).width());
      $(this).addClass('big');
      
      //update final widths (after transitions)
      $(this).attr('data-width', getFinalWidthOfElement($(this)));
      
      //recalsulate positions
      calcPositions();
    }, function() {
      $(this).removeClass('big');
      
      //return widths
      $(this).attr('data-width', 30);//getFinalWidthOfElement($(this)));
      $(this).attr('data-before', 30);//$(this).width());
      
      calcPositions();
      
      leftAngle = 0;
    });
    
    function getFinalWidthOfElement(element) {
    
      //clone current elem
      var clone = element.clone();
      
      //remove transitions to get final sizes
      clone.css('transition', 'none');
    
      //hide and change classes
      clone.hide();
      clone.removeClass('item');
    
      element.parent().append(clone);
      
      //our final sizes of item
      var endHeight = clone.height();
    
      //remove because we're already get sizes
      clone.remove();
    
      return endHeight;
    }
    .container {
      position: relative;
      margin-top: 50px;
      margin-left: 100px;
      height: 200px;
      width: 200px;
      border-radius: 50%;
      border: 1px solid #000;
    }
    
    .item {
      position: absolute;
      height: 30px;
      width: 30px;
      top: 100px;
      left: 100px;
      border-radius: 50%;
      background: red;
      line-height: 30px;
      color: #fff;
      font-family: sans-serif;
      text-align: center;
      transform: translateX(-50%) translateY(-50%);
      cursor: pointer;
      transition: all 0.3s ease-in-out;
    }
    
    .big {
      background: orange;
      height: 60px;
      width: 60px;
      line-height: 60px;
    }
    
    .start {
      display: inline-block;
      background: #cccc;
      padding: 10px 25px;
      border: 1px solid #000;
      font-family: sans-serif;
      cursor: pointer;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div class="start">Start</div>
    
    <div class="container">
      <div class="item" data-width="30" data-before="30">1</div>
      <div class="item" data-width="30" data-before="30">2</div>
      <div class="item" data-width="30" data-before="30">3</div>
      <div class="item" data-width="30" data-before="30">4</div>
      <div class="item" data-width="30" data-before="30">5</div>
    </div>

    如需改进:https ://codepen.io/meetromb/pen/LdXgNd

    • 4
  2. user33274
    2020-04-08T14:38:15Z2020-04-08T14:38:15Z

    当然,这根本不是菜单,只是碰巧是这样做的,但你可以修改它,它会是你需要的:

    *{
      margin:0;
      padding:0;
      box-sizing:border-box;
      transition:all .5s .3s cubic-bezier(0.08, 0.81, 0.58, 0.01);
    }
    
    .items{
      width:300px;
      height:300px;
      margin:80px auto;
      position:relative;
      border-radius: 50%;
      background:#fbfbfb;
    }
    .cord,
    .item{
      position: absolute;
      top:50%;
      left:50%;
      width:2px;
      height:2px;
      background:#ccc;
      margin:-1px;
      overflow:hidden;
    }
    .items:hover .cord{
      width:100px;
      height:100px;
      border-radius:50%;
      position: absolute;
      left:calc(100% - 50px);
      top:calc(50% - 50px);
    }
    
    .items:hover .item1{
      width:100px;
      height:100px;
      border-radius:50%;
      position: absolute;
      left:calc(85% - 50px);
      top:calc(15% - 50px); 
      transition-delay: 0.1s;
      animation-delay:0.1s;
    }
    .items:hover .item2{
      width:100px;
      height:100px;
      border-radius:50%;
      position: absolute;
      left:90px;
      top:-40px;
      transition-delay: 0.2s;
      animation-delay:0.2s;
    }
    .items:hover .item3{
      width:100px;
      height:100px;
      border-radius:50%;
      position: absolute;
      left:-20px;
      top:5px;
      transition-delay: 0.3s;
      animation-delay:0.3s;
    }
    
    .items:hover .item4{
      width:100px;
      height:100px;
      border-radius:50%;
      position: absolute;
      left:-45px;
      top:115px;
      transition-delay: 0.4s;
      animation-delay:0.4s;
    }
    
    .items:hover .item5{
      width:100px;
      height:100px;
      border-radius:50%;
      position: absolute;
      left:25px;
      top:215px;
      transition-delay: 0.5s;
      animation-delay:0.5s;
    }
    .item{
      visibility: hidden;
      opacity:0;
    }
    .items:hover .cords,
    .items:hover .item{
      visibility: visible;
      opacity:1;
      animation:scale .5s  .3s cubic-bezier(0, 1, 0.92, 0.11);
    }
    
    @keyframes scale{
      100%{
    transform:scale(.3)rotate(360deg);  
    background:#fbfbfb;
      }
    }
    <div class="items">
      <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="cord"></div>
    </div>

    • 0

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5