简体   繁体   English

如何以过渡延迟更改svg在悬停时的位置

[英]How to change position of svg on hover with a transition delay

I'm trying to move svg icons in my navbar to the left on hovering, which I'm able to do so, but I need the element to move smoothly through something like this transition : all .5s; 我正在尝试将鼠标悬停在导航栏中的svg图标向左移动,这是可以做到的,但是我需要该元素在此transition : all .5s;平稳移动transition : all .5s; The issue is that svg tag doesn't accept transition property in css, so I tried to use the transition on the container, but that doesn't work, it just move instantly without transition effect. 问题是svg标记在CSS中不接受过渡属性,因此我尝试在容器上使用过渡,但这不起作用,它会立即移动而没有过渡效果。

HTML : HTML

  <div id="sidenav-icon-section">
            <li>
                <a href="/">
                    <img src="/assets/images/home.svg" alt="home" onload="SVGInject(this)">
                </a>
          </li>
   </div>

I use the SVGInject library to transform my svg code in the browser, which results to this : 我使用SVGInject库在浏览器中转换我的svg代码,结果是这样的:

 <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0px" y="0px" width="64px" height="64px" viewBox="0 0 64 64" enable-background="new 0 0 64 64" xml:space="preserve" data-inject-url="http://localhost:4200/assets/images/home.svg" _ngcontent-c1="">

<polygon fill="none" stroke="#000000" stroke-width="2" stroke-miterlimit="10" points="32,3 2,33 11,33 11,63 23,63 23,47 39,47   39,63 51,63 51,33 62,33 "></polygon></svg>

Also I have another svg icon that has a path tag instead of polygon 我也有另一个svg图标,它具有路径标签而不是多边形

CSS : CSS

  #sidenav-icon-section {
    top: 25%;
    position: relative;

    li {
      position: relative;
      transition: all .5s;

      &:hover svg {
        left: 7%;
      }
    }
  }

I tried applying the transition to and the "left" attribute to path and polygon elements but nothing happens at that point, they don't even move. 我尝试对路径和多边形元素应用到的过渡,并将“ left”属性应用于路径和多边形元素,但此时没有任何反应,它们甚至都不会移动。

You are trying to set the attribute left on an element which position is static . 您正在尝试设置属性left该位置的元素static Try a negative margin instead. 请改用负边距。 Also you are applying the transition to the wrong element. 另外,您将过渡应用于错误的元素。


 svg { transition: margin-left .5s } li:hover svg { margin-left: -7px; } 
 <ul id="sidenav-icon-section"> <li class="item"> <a href="/"> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0px" y="0px" width="64px" height="64px" viewBox="0 0 64 64" enable-background="new 0 0 64 64" xml:space="preserve" data-inject-url="http://localhost:4200/assets/images/home.svg" _ngcontent-c1=""> <polygon fill="none" stroke="#000000" stroke-width="2" stroke-miterlimit="10" points="32,3 2,33 11,33 11,63 23,63 23,47 39,47 39,63 51,63 51,33 62,33 "></polygon></svg> </a> </li> </ul> 

the scss equivalent: 等价于scss:

li {
  svg { transition: margin-left .5s; }
  &:hover svg {
    margin-left: -7px;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM