【问题标题】:How to fill a rectange from center html , css如何从中心 html , css 填充矩形
【发布时间】:2016-05-13 04:43:39
【问题描述】:

这就是我需要使用 css + html + jquery 来做的事情......我可以编写 jquery 代码来获取输入百分比......我需要帮助来绘制这个......

一个简单的填充是:

    <svg width="400" height="110">
  <rect width="100" height="100" style="fill:rgb(0,0,255);">
  Sorry, your browser does not support inline SVG.  
</svg>

【问题讨论】:

  • 感谢您的回复..这是一个椭圆填充..有很多参考资料..我需要一个方形填充..
  • 您可以使用 SVG 或使用剪裁的画布轻松完成此操作。 1. 创建一个矩形剪切区域,使所有图形只出现在剪切的矩形内, 2. 绘制一个红色矩形 3. 绘制一个较浅的红色弧,这是所需的 - X 百分比,半径远超出矩形, 4. 绘制文本.

标签: jquery html css svg html5-canvas


【解决方案1】:

CODEPEN 中查看此示例。我设法只使用 CSS 和 HTML 来构建它。

HTML

<div class="container">
  <div class="triangle"></div>
  <div class="percentage">
     92%
  </div>
  <div class="description">
     Goal Achievement <br /> Projection by End Date
  </div>
</div>

CSS

.container {
   position: relative;
   background: #E96C6A;
   width: 150px;
   height: 150px;
   color: white;
   z-index: 1;
}

.triangle {
   position: absolute;
   width: 0;
   height: 0;
   left: 40px;
   border-top: 70px solid #E8908F;
   border-left: 50px solid transparent;
   z-index: 2;
}

.percentage {
   position: relative;
   padding-top: 50px;
   padding-bottom: 5px;
   width: 100%;
   font-size: 3em;
   text-align: center;
   z-index: 3;
}

.description {
  position: relative;
  width: 100%;
  font-size: 0.8em;
  text-align: center;
}

当然,您可以通过将元素的字体和大小更改为他们想要的内容来改进它。但结构保持不变。

【讨论】:

  • 这是一个不错的尝试..但这不是我需要的..百分比值是动态的..它可以是 42.65%...所以围绕这个 html + css 构建一个 javascript 逻辑将是太乱了
  • “会太乱”是什么意思?我刚刚添加了一个按钮来动态更改百分比。它就像一个魅力。看看codepen.io/zero_point/pen/QyVGzg
  • 老兄......背景怎么样......你不认为纯红色也应该填充 100% 正方形的 42.65%......??
  • 首先,我想帮助你,所以要友善。其次,没有人改变布局或子布局的大小,这是一个糟糕的用户体验。相对于视口的宽度,您的红色背景应始终保持不变。如果你根据 % 定义它的宽度和高度,你会没事的。此外,您需要在较小的设备或视口上更改字体大小,这是另一个 UX 妥协。不管怎样,祝你好运
  • @zeropoint。 3个想法:1.您很好地重新创建了要求的设计(赞成),2.问题不清楚,因为OP显然希望较亮的区域可以更改为整个矩形的X%(但这仅在他们的评论中明确表示给你),3. user2740034 在他们对你的评论中似乎采取了粗鲁的语气。
【解决方案2】:

制作一个简单的饼图相当容易。我们只需要使用一点三角函数来计算圆弧到百分比位置的坐标。

由于饼图的周长在 SVG 的边界之外,因此不可见,我们可以使用单个弧线。当您仅对 180 度以上的角度使用一个圆弧 ('A') 路径命令时,有时可能会出现精度问题。

演示

function setPie(cx, cy, fraction)
{
  // Get reference to the <path> element that we use to make the "pie chart"
  var pie = document.getElementById("pie");
  // Pie radius (just a value we can be sure is large enough to fall outside SVG bounds)
  var radius = cx+cy;
  // Calculate end angle of pie chart (radians)
  var angle = fraction * 2 * Math.PI;
  // End coordinates of circular arc
  var endX = cx + Math.sin(angle) * radius;
  var endY = cy - Math.cos(angle) * radius;
  // Let renderer know we want to use the long direction if the arc > 180deg
  var largeArcFlag = (fraction > 0.5) ? 1 : 0;
  if (fraction < 1.0) {
    // Set the path command string with a path representing a circular sector of the right amount.
    pie.setAttribute("d", ["M", cx, cy,         // move
                           "L", cx, cy-radius,  // vertical line at fraction=0
                           "A", radius, radius, 0, largeArcFlag, 1, endX, endY,  // arc
                           "Z"].join(' '));
  } else {
    // Special case for 100%. The arc form above doesn't render properly when arc end = arc start
    // So we just make a rectangle instead
    pie.setAttribute("d", ["M", 0, 0,         // move
                           "L", 2*cx, 0, 2*cx, 2*cy, 0, 2*cy,  // lines to form a rect
                           "Z"].join(' '));
  }
    
}


// First two values are center-X and center-Y of the pie chart.
// Third value is the percentage (in the form of a fraction).

setPie(64, 66, 0.92);
svg .bg {
  fill: #e28f8d;
}

#pie {
  fill: #e16b67;
}
<svg width="128" height="132">
  <rect class="bg" width="100%" height="100%"/>
  <path id="pie" d="M0 0"/>
</svg>

【讨论】:

  • 谢谢伙计……正是我需要的……你拯救了我的一天。!! :) :) :)
  • 顺便说一句,它在 IE 中被破坏了——完整的 92 度圆显示(未剪辑到记录中)。
  • 这很奇怪。最外层的&lt;svg&gt; 元素应该隐藏溢出。不知道IE中的那个错误。修复只是添加一个额外的 CSS 规则:svg { overflow: hidden; }.
猜你喜欢
  • 1970-01-01
  • 2022-08-22
  • 1970-01-01
  • 1970-01-01
  • 2019-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-20
相关资源
最近更新 更多