代码之家  ›  专栏  ›  技术社区  ›  Sean

如何让elementFromPoint()返回SVG g元素?[副本]

  •  0
  • Sean  · 技术社区  · 4 年前

    我可以让SVG动画悬停的方式,我想为Chrome,但我面临着一个Firefox和Safari的问题。

    显然 pointer-events <g></g> 在该浏览器上的行为与在其他现代浏览器上的行为不同,至少在尝试将值设置为 bounding-box .

    我在做什么

    g {
      pointer-events: bounding-box;
    }
    

    但只有当实际的 <path> 元素,而不是整个组 <g> 就像我需要的那样。

    Can I use 没有说任何关于这个,它提到SVG也支持这个属性。

    下面是一个示例代码,您可以查看我的一个SVG的样子。

    <?xml version="1.0" encoding="UTF-8"?>
    <svg width="40px" height="40px" viewBox="0 0 40 40" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
        <style>
            g {
                pointer-events: bounding-box;     
                //not working on FF
            }
            #mobile:hover .flip {
                transform-origin:55% 50%;
                -moz-transform-origin:55% 50%;
                animation: flip_left 1.6s forwards;
            }
            @keyframes flip_left {
              0% {transform: perspective(2000px) rotateY(90deg) skewY(-1deg)}
              30% {transform:perspective(2000px) rotateY(-25deg) skewY(-0.8deg)}
              50% {transform:perspective(2000px) rotateY(20deg) skewY(0.8deg)}
              70% {transform:perspective(2000px) rotateY(-10deg) skewY(-0.8deg)}
              100% {transform:perspective(2000px) rotateY(0deg)}
            }
        </style>
        <!-- Generator: Sketch 51.2 (57519) - http://www.bohemiancoding.com/sketch -->
        <title>Mobile solutions</title>
        <desc>Created with Sketch.</desc>
        <defs></defs>
        <g id="mobile" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
            <g id="MS_HP_Usecase_Based_Page-Desktop-2A" transform="translate(-766.000000, -418.000000)" stroke="#00A0DF" stroke-width="1.25">
                <g id="Asset-5" transform="translate(766.000000, 418.000000)">
                    <g class="flip">
                    <rect id="Rectangle-path" stroke-linecap="round" stroke-linejoin="round" x="12.35" y="7.41" width="15.32" height="25.33" rx="2.03"></rect>
                    <circle id="Oval" stroke-linecap="round" stroke-linejoin="round" cx="20.01" cy="28.72" r="1.58"></circle>
                    <path d="M18.43,10.72 L21.48,10.72" id="Shape" stroke-linecap="round" stroke-linejoin="round"></path>
                </g>
                    <circle id="Oval" cx="19.67" cy="19.67" r="19.04"></circle>
                </g>
            </g>
        </g>
    </svg>
    

    我想找到一个解决办法,因为我想让这个动画跨浏览器工作。我想最终使它工作的IE11和边缘太。

    谢谢,

    0 回复  |  直到 6 年前
        1
  •  1
  •   Uli    6 年前

    所以呢 pointer-events: bounding-box 似乎大多数浏览器都不支持。

    我在问题的评论部分实现了@ccprog建议的变通方法。

    我加了一个 <rect fill="none"> 元素,即与svg本身相同的维度。我加了一个 :hover ~ 使用 flip 在里面上课。

    参见CSS:

    #mobile-hover {
       visibility: visible;
       pointer-events: visible;
    }
    #mobile-hover:hover ~ .group .flip {
      -moz-transform-origin:55% 50%;
      -webkit-transform-origin: 55% 50%;
      transform-origin:55% 50%;
      -webkit-animation: flip_left 1.6s forwards;
      animation: flip_left 1.6s forwards;
    }
    

    我发现我不得不补充 pointer-events: visible rect 元素来检测 :悬停 . 我补充道 visibility: visible 作为一项要求 指针事件:可见 去工作。

    下面是全新的SVG代码:

    <?xml version="1.0" encoding="UTF-8"?>
    <svg width="40px" height="40px" viewBox="0 0 40 40" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="mobile-icon">
        <style>
            #mobile-hover {
            visibility: visible;
            pointer-events: visible;
            }
            #mobile-hover:hover ~ .group .flip {
                -moz-transform-origin:55% 50%;
                -webkit-transform-origin: 55% 50%;
                transform-origin:55% 50%;
                -webkit-animation: flip_left 1.6s forwards;
                animation: flip_left 1.6s forwards;
            }
            @keyframes flip_left {
              0% {transform: perspective(2000px) rotateY(90deg) skewY(-1deg)}
              30% {transform:perspective(2000px) rotateY(-25deg) skewY(-0.8deg)}
              50% {transform:perspective(2000px) rotateY(20deg) skewY(0.8deg)}
              70% {transform:perspective(2000px) rotateY(-10deg) skewY(-0.8deg)}
              100% {transform:perspective(2000px) rotateY(0deg)}
            }
        </style>
        <!-- Generator: Sketch 51.2 (57519) - http://www.bohemiancoding.com/sketch -->
        <title>Mobile solutions</title>
        <desc>Created with Sketch.</desc>
        <defs></defs>
        <g id="mobile" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" >
        <rect fill="none" width="40" height="40" id="mobile-hover">
        </rect>
            <g id="MS_HP_Usecase_Based_Page-Desktop-2A" transform="translate(-766.000000, -418.000000)" stroke="#00A0DF" stroke-width="1.25" class="group">
                <g id="Asset-5" transform="translate(766.000000, 418.000000)">
                    <g class="flip">
                    <rect id="Rectangle-path" stroke-linecap="round" stroke-linejoin="round" x="12.35" y="7.41" width="15.32" height="25.33" rx="2.03"></rect>
                    <circle id="Oval" stroke-linecap="round" stroke-linejoin="round" cx="20.01" cy="28.72" r="1.58"></circle>
                    <path d="M18.43,10.72 L21.48,10.72" id="Shape" stroke-linecap="round" stroke-linejoin="round"></path>
                </g>
                    <circle id="Oval" cx="19.67" cy="19.67" r="19.04"></circle>
                </g>
            </g>
    
        </g>
    </svg>
    

    在Chrome、Safari和Firefox上工作,接下来我将尝试测试IE11和Edge。