loop gradient directions by clicking on a target

前端 未结 3 1482
借酒劲吻你
借酒劲吻你 2021-01-29 02:11

I need to loop all of the eight gradient directions by clicking on a target, like this:

3条回答
  •  既然无缘
    2021-01-29 02:29

    You can access the linear-gradient value with background-image.

    $('.targ').on('click', function() {
      let direction = $(this).css('background-image').split(',')[0].slice(16);
      const gradientColors = $(this).css('background-image').split(',').slice(1);
    
      if (direction == 'to top') {
        direction = 'to top right'
      } else if (direction == 'to top right') {
        direction = 'to right'
      } else if (direction == 'to right') {
        direction = 'to right bottom'
      }
    
      $(this).css('background', 'linear-gradient(' + direction + ',' + gradientColors.join(','));
    });
    .targ {
      width: 54px;
      height: 54px;
      cursor: pointer;
    }
    
    

提交回复
热议问题