Creating a CSS 'path' on hover

前端 未结 2 1728
旧时难觅i
旧时难觅i 2021-01-30 01:55

I\'m trying to generate a \'nice\' CSS menu using (mainly) CSS, but with a tiny bit of jQuery as well:

My overall idea is:

+--------------------         


        
2条回答
  •  梦谈多话
    2021-01-30 02:33

    Using a different approach you'll get a slightly different effect. You can play with the times of the setTimeout and the transition to modify the behavior.

    See the fiddle

    + function() {
      var to;
      $(".wrap").on('mouseenter', function() {
        var circles = $(this).children();
        var degree = (2 * Math.PI) / circles.length; //calc delta angle
        var transforms = [];
    
        // Calculate the position for each circle
        circles.each(function(index) {
            var x = 100 * Math.cos(-0.5 * Math.PI + degree * (-1 * index - 0.5));
            var y = 100 * Math.sin(-0.5 * Math.PI + degree * (-1 * index - 0.5));
    
          transforms.push('translate(' + x + 'px,' + y + 'px)');
        });
    
        // Function to moves all the circles
        // We'll pop a circle each time and than call this function recursively
        function moveCircles() {
          var transform = transforms.shift();
          circles.css('transform', transform);
    
          circles.splice(0, 1);
          if (circles.length) to = setTimeout(moveCircles, 400);
        }
    
        moveCircles();
      });
    
      $(".wrap").on('mouseleave', function() {
        var circles = $(this).children().css('transform', '');
        clearTimeout(to);
      });
    }();
       .wrap {
         height: 300px;
         width: 300px;
         background: red;
         position: relative;
         transform-origin: center center;
         transition: all 0.8s;
       }
       .circle {
         transition: all 0.8s;
         position: absolute;
         height: 50px;
         width: 50px;
         text-align: center;
         line-height: 50px;
         top: calc(50% - 25px);
         left: calc(50% - 25px);
         background: tomato;
         border-radius: 50%;
       }
    
    
    1
    2
    3
    4
    5
    6

提交回复
热议问题