javascript - 如何使用 Snap.svg 和 Angular 5 在悬停时缩放不同的 svg 图层

标签 javascript html angular svg

我是 SVG 的 super 菜鸟,所以请耐心等待(并随时纠正我做错的地方)。我的应用程序是用 Angular 5 构建的。我正在使用一个复杂的 SVG,它大约有 23 层。这是因为它是一个包含代表各种输入的框的图表,并且每个框都必须在悬停单击时缩放,以向用户表明他/她正在选择相应的框。

在尝试使用简单的 CSS 变换(缩放、平移)实现缩放后,我意识到 svg 矩阵和缩放需要我进行更多研究和理解,因此决定尝试 Snap.svg,我读到它非常擅长简化 svgs 的工作。

现在我的 Angular 5 组件如下所示:

import { Component, OnInit } from '@angular/core';
declare const Snap: any;


@Component({
   ...
export class SvgComponent implements OnInit {

constructor(){
}

ngOnInit(){

}

onHover(event) {
    const layer = Snap(event.target.id);
    layer.hover(function() {
       this.animate({ transform: 's1.5,1.5' }, 500);
    }, function() {
       this.animate({ transform: 's1,1' }, 500);
    });
}

HTML 看起来像这样(其中一层):

<svg:g style="display:inline" class="svg-layer" id="layer1" (hover)="onHover($event)">
  <svg:g id="g4746" style="display:inline" transform="matrix(0.26458333,0,0,0.26458333,-13.055467,-56.894235)">
    <svg:path
      id="path3892"
      d="m 396.86667,292.53333 c -4.59211,0 -9.16503,0.1345 -13.7,0.36667 -4.53498,0.23217 -9.06106,0.57462 -13.53334,1.03333 -4.03511,0.41387 -8.02024,0.93941 -12,1.53334 -6.98255,23.42835 -10.71331,47.22459 -12.26666,71.16666 3.73406,-1.00536 7.41654,-2.14453 11.23333,-2.93333 6.49856,-1.34304 13.12148,-2.34492 19.83333,-3.03333 6.71185,-0.68842 13.54166,-1.06667 20.43334,-1.06667 6.89168,0 13.68815,0.37825 20.4,1.06667 6.71185,0.68841 13.33477,1.69029 19.83333,3.03333 3.82739,0.791 7.52247,1.92458 11.26667,2.93333 -1.55336,-23.94207 -5.28412,-47.73831 -12.26667,-71.16666 -3.99047,-0.59604 -7.98719,-1.11834 -12.03333,-1.53334 -4.47228,-0.45871 -8.99836,-0.80116 -13.53334,-1.03333 -4.53497,-0.23217 -9.07456,-0.36667 -13.66666,-0.36667 z"
      style="color:#000000;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:medium;line-height:normal;font-family:'Bitstream Vera Sans';-inkscape-font-specification:'Bitstream Vera Sans';text-indent:0;text-align:start;text-decoration:none;text-decoration-line:none;letter-spacing:normal;word-spacing:normal;text-transform:none;writing-mode:lr-tb;direction:ltr;text-anchor:start;display:inline;overflow:visible;visibility:visible;fill:#ffffb1;fill-opacity:1;stroke:#000000;stroke-width:2.13333344;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker:none;enable-background:accumulate" />
    <svg:text
     id="text3897"
     y="333.54636"
     x="396.53268"
     style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:12.80000019px;line-height:0%;font-family:Tahoma;-inkscape-font-specification:Tahoma;text-align:center;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1.06666672"
     xml:space="preserve"><tspan
       id="tspan3311"
       x="396.53268"
       y="333.54636"
       style="font-size:14.9333334px;line-height:1.25;stroke-width:1.06666672">sereniteit</tspan></svg:text>
  </svg:g>

所以我得出以下结论:

  1. 实现悬停函数OnInit(我首先执行的)不会 动画但第一层(所以layer1)。有点道理, 因为它只被触发一次。
  2. (hover) 或 (mouseover) 或任何其他事件绑定(bind)似乎不 放置在 svg 标记内时起作用。作为引用,我阅读并 实现了本文中的一些想法: 奥 git _a 这非常好,但是那里明确提到了该事件 绑定(bind)在 Angular 2 中可以正常工作。也许 Angular 2 中发生了一些变化 同时,现在事件绑定(bind)不再起作用。

任何想法都非常受欢迎,因为我现在很困难。

稍后编辑: 所以 Angular 2+ 确实支持事件绑定(bind)(只要你给它正确的事件名称,显然 - “悬停”不存在)。悬停对应的事件名称是 (mouseenter) 和 (mouseleave)。如果您将它们附加到 svg 标签,它们就会起作用。

最佳答案

您应该能够仅使用 CSS 完成您想要的操作。

以下内容适用于现代浏览器版本。如果您需要支持较旧的浏览器,那么您可能需要做更多的工作。

.svg-layer {
  transform-box: fill-box;
  transform-origin: 50% 50%;
  transition: 0.2s transform;
  transform: scale(1,1);
}

.svg-layer:hover {
  transform: scale(1.5,1.5);
}
<svg width="300" height="300" viewBox="60 10 60 60">
  <g style="display:inline" class="svg-layer" id="layer1">
  <g id="g4746" style="display:inline" transform="matrix(0.26458333,0,0,0.26458333,-13.055467,-56.894235)">
    <path
      id="path3892"
      d="m 396.86667,292.53333 c -4.59211,0 -9.16503,0.1345 -13.7,0.36667 -4.53498,0.23217 -9.06106,0.57462 -13.53334,1.03333 -4.03511,0.41387 -8.02024,0.93941 -12,1.53334 -6.98255,23.42835 -10.71331,47.22459 -12.26666,71.16666 3.73406,-1.00536 7.41654,-2.14453 11.23333,-2.93333 6.49856,-1.34304 13.12148,-2.34492 19.83333,-3.03333 6.71185,-0.68842 13.54166,-1.06667 20.43334,-1.06667 6.89168,0 13.68815,0.37825 20.4,1.06667 6.71185,0.68841 13.33477,1.69029 19.83333,3.03333 3.82739,0.791 7.52247,1.92458 11.26667,2.93333 -1.55336,-23.94207 -5.28412,-47.73831 -12.26667,-71.16666 -3.99047,-0.59604 -7.98719,-1.11834 -12.03333,-1.53334 -4.47228,-0.45871 -8.99836,-0.80116 -13.53334,-1.03333 -4.53497,-0.23217 -9.07456,-0.36667 -13.66666,-0.36667 z"
      style="color:#000000;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:medium;line-height:normal;font-family:'Bitstream Vera Sans';-inkscape-font-specification:'Bitstream Vera Sans';text-indent:0;text-align:start;text-decoration:none;text-decoration-line:none;letter-spacing:normal;word-spacing:normal;text-transform:none;writing-mode:lr-tb;direction:ltr;text-anchor:start;display:inline;overflow:visible;visibility:visible;fill:#ffffb1;fill-opacity:1;stroke:#000000;stroke-width:2.13333344;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker:none;enable-background:accumulate" />
    <text
     id="text3897"
     y="333.54636"
     x="396.53268"
     style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:12.80000019px;line-height:0%;font-family:Tahoma;-inkscape-font-specification:Tahoma;text-align:center;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1.06666672"
     xml:space="preserve"><tspan
       id="tspan3311"
       x="396.53268"
       y="333.54636"
       style="font-size:14.9333334px;line-height:1.25;stroke-width:1.06666672">sereniteit</tspan></text>
  </g>
  </g>
</svg>

关于javascript - 如何使用 Snap.svg 和 Angular 5 在悬停时缩放不同的 svg 图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51267982/

相关文章:

javascript - AngularJS 中的子菜单(展开/折叠树)

angular - 如何使用 RxJS 处理 Angular 服务中的多个 api 调用?

javascript - 突出显示 Angular * ngFor中的选定元素

javascript - 自定义悬停标签,如 Thinglink

javascript - 使用Vue.js获取用户的IP地址

php - 以复选框形式显示表格内容

html - 如何水平滚动我的 HTML 表格?

javascript - 在 Angular 中将 Controller 的功能添加到某些 View

javascript - 负载名称中的 Node 红色字符串

angular - 模型驱动表单提交后如何清空表单? ( Angular 2)