【问题标题】:How to style svg progress bar with gradients如何使用渐变样式设置 svg 进度条
【发布时间】:2018-02-12 07:57:25
【问题描述】:

我正在使用progressbar.js 插件,它可以帮助我使用svg 创建漂亮的进度条。这是小提琴:

https://jsfiddle.net/vaxobasilidze/xqge4cew/1/

#container1 中,我使用十六进制颜色来设置svg 的样式,但在#container 中,我需要使用渐变设置样式。如您所见,它不起作用。有没有办法使用渐变来设置 svg 样式?

// progressbar.js@1.0.0 version is used
// Docs: http://progressbarjs.readthedocs.org/en/1.0.0/

var bar = new ProgressBar.SemiCircle(container, {
  strokeWidth: 6,
  easing: 'easeInOut',
  duration: 1400,
  color: 'linear-gradient(to bottom, #1e5799 0%,#2989d8 50%,#7db9e8 100%)',
  trailColor: '#eee',
  trailWidth: 1,
  svgStyle: null
});

bar.animate(1.0);  // Number from 0.0 to 1.0

var bar1 = new ProgressBar.SemiCircle(container1, {
  strokeWidth: 6,
  easing: 'easeInOut',
  duration: 1400,
  color: '#eee',
  trailColor: '#eee',
  trailWidth: 1,
  svgStyle: null
});

bar1.animate(1.0);
#container {
  margin: 20px;
  width: 200px;
  height: 100px;
}

#container1 {
  margin: 20px;
  width: 200px;
  height: 100px;
}
<script src="https://rawgit.com/kimmobrunfeldt/progressbar.js/1.0.0/dist/progressbar.js"></script>
<div id="container"></div>
<div id="container1"></div>

【问题讨论】:

    标签: javascript html css svg progress-bar


    【解决方案1】:

    您必须插入渐变颜色,然后像这样将其附加到 svg

    // progressbar.js@1.0.0 version is used
    // Docs: http://progressbarjs.readthedocs.org/en/1.0.0/
    
    var bar = new ProgressBar.SemiCircle(container, {
      strokeWidth: 6,
      easing: 'easeInOut',
      duration: 1400,
      color: 'url(#gradient)',
      trailColor: '#eee',
      trailWidth: 1,
      svgStyle: null
    });
    bar.animate(1.0);  // Number from 0.0 to 1.0
    
    let linearGradient = `
      <defs>
        <linearGradient id="gradient" x1="0%" y1="0%" x2="100%" y2="0%" gradientUnits="userSpaceOnUse">
          <stop offset="20%" stop-color="tomato"/>
          <stop offset="50%" stop-color="purple"/>
        </linearGradient>
      </defs>
    `
    
    bar.svg.insertAdjacentHTML('afterBegin', linearGradient);
    
    var bar1 = new ProgressBar.SemiCircle(container1, {
      strokeWidth: 6,
      easing: 'easeInOut',
      duration: 1400,
      color: '#eee',
      trailColor: '#eee',
      trailWidth: 1,
      svgStyle: null
    });
    
    bar1.animate(1.0);
    #container {
      margin: 20px;
      width: 200px;
      height: 100px;
    }
    
    #container1 {
      margin: 20px;
      width: 200px;
      height: 100px;
    }
    <script src="https://rawgit.com/kimmobrunfeldt/progressbar.js/1.0.0/dist/progressbar.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="container"></div>
    <div id="container1"></div>

    【讨论】:

      【解决方案2】:

      您不能将渐变用作颜色,因为它稍后会放置在笔划内。要将渐变与 SVG 一起使用,您需要先定义它,然后将其应用为颜色。所以你可以尝试这样的事情:

      // progressbar.js@1.0.0 version is used
      // Docs: http://progressbarjs.readthedocs.org/en/1.0.0/
      
      var Gradient = '<defs><linearGradient id="gradient" x1="0%" y1="0%" x2="100%" y2="0%" gradientUnits="userSpaceOnUse"><stop offset="0%" stop-color="#1e5799"/><stop offset="50%" stop-color="#2989d8"/><stop offset="100%" stop-color="#7db9e8"/></linearGradient></defs>';
      
      var bar = new ProgressBar.SemiCircle(container, {
        strokeWidth: 6,
        easing: 'easeInOut',
        duration: 1400,
        color: 'url(#gradient)',
        trailColor: '#eee',
        trailWidth: 1,
        svgStyle: null
      });
      bar.svg.insertAdjacentHTML('afterbegin', Gradient);
      
      bar.animate(1.0);  // Number from 0.0 to 1.0
      
      var bar1 = new ProgressBar.SemiCircle(container1, {
        strokeWidth: 6,
        easing: 'easeInOut',
        duration: 1400,
        color: '#eee',
        trailColor: '#eee',
        trailWidth: 1,
        svgStyle: null
      });
      
      bar1.animate(1.0);
      #container {
        margin: 20px;
        width: 200px;
        height: 100px;
      }
      
      #container1 {
        margin: 20px;
        width: 200px;
        height: 100px;
      }
      <script src="https://rawgit.com/kimmobrunfeldt/progressbar.js/1.0.0/dist/progressbar.js"></script>
      <div id="container"></div>
      <div id="container1"></div>

      【讨论】:

      • 不错的一个。谢谢!
      • +1 ...所以这个渐变跨越了半圆...我只是想知道,你将如何配置渐变以跨越圆的更大部分,这样它就不会重复.例如在this codepen 中,渐变重复了两次
      • @Danield 你的意思是什么? :) 它不在你的笔中......它在整个圈子上从右到左
      • @Danield 啊好的,顺便说一下,半圆的情况并非如此,因为方向是从右到左......你正在寻找的是一个锥形渐变,它还没有到处都支持也不认为它有一个svg元素......但我们总是可以尝试通过使用这样的两个渐变来模拟它; jsfiddle.net/rq2ve5sx/1
      • @Danield 为了让整个事情变得更好更准确,您可以像这样调整动画:jsfiddle.net/rq2ve5sx/3 ;)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 2016-07-30
      • 2014-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多