html - 当容器 ul 可见时绘制 svg 线条

标签 html css svg svg-animate

我创建了一个受“站点地图”启发的菜单,它使用 svgs 连接菜单中的每个元素。目前 svg 是静态的。但是我相信有可能使这些吸引进来?

我有一个更复杂的问题,我只希望在容器 ul 可见时绘制线条。

当父 li 悬停在...上时,这些当前可见

   #submenu-1 li:hover>ul {
      visibility: visible;
      opacity: 1;
      max-height: 500px;
      transition: opacity 0.5s ease-in;
    }

这是 fiddle 的链接 https://jsfiddle.net/spittman/sn3xg5Lb/113/show

有什么想法吗?

谢谢 山姆

最佳答案

你被困在哪里了?关于如何在 SVG 中绘制动画线条,堆栈溢出有很多问题和答案。

对于您的情况,我建议您重新绘制每条线,以便它们都从顶部开始并在叶点结束。此刻,它们似乎被分成几部分,朝着随机的方向前进。有些是从头到尾,有些是从头到尾)。

例如:

<svg viewBox="0 0 900 50">
  <polyline points="450,0, 450,25, 112.5,25, 112.5,50" class="st0"/>
  <polyline points="450,0, 450,25, 337.5,25, 337.5,50" class="st0"/>
  <polyline points="450,0, 450,25, 562.5,25, 562.5,50" class="st0"/>
  <polyline points="450,0, 450,25, 787.5,25, 787.5,50" class="st0"/>
</svg>

画线动画是通过将 stroke-dasharray 设置为等于线的长度来完成的。然后将 stroke-dashoffset 从该长度设置为 0 的动画。网络上有许多教程解释了它是如何工作的。我不会在这里重复细节。

#submenu-1 li ul svg polyline.st0 {
  stroke-dasharray: 388px;
  stroke-dashoffset: 388px;
}

然后在悬停时我们将 stroke-dashoffset 从 388 转换为 0。

#submenu-1 li:hover>ul svg polyline.st0 {
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 1s ease-in;
}

您需要为每个树形 SVG 重复上述步骤。对于较小的 TreeMap ,需要减小 388px 值。

完整示例:

$('#menu').mouseover(function () {
      $('#page-title').hide();      
});
$('#menu').mouseout(function () {
      $('#page-title').show();      
});
@import url("https://use.typekit.net/tud5kgo.css");

body {
  margin: 0;
  padding: 0;
  border: 0;
  background-color: black;
}

/* Main Menu*/

#submenu-1 {
  list-style: none;
  margin: 0;
  padding: 0px 0 0 0;
}

/* Level 1 – List item  */

#submenu-1 li {
  width: 900px;
  float: left;
  text-align: center;
  padding-top: 10px;
}

#submenu-1 li:hover>ul {
  visibility: visible;
  opacity: 1;
  max-height: 500px;
  transition: opacity 0.5s ease-in;
}

#submenu-1 li ul svg polyline.st0 {
  stroke-dasharray: 388px;
  stroke-dashoffset: 388px;
}

#submenu-1 li:hover>ul svg polyline.st0 {
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 1s ease-in;
}

#submenu-1 ul {
  list-style: none;
  left: 0;
  margin: 0;
  padding: 0;
  position: relative;
  width: 900px;
  float: left;
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s, opacity 0.5s linear;
  max-height: 0;
  transition: max-height 0.5s ease-out;
  z-index: 600;
  padding: 10px 0px 0px 0px;
}


a:link,
a:visited,
a:active {
  color: white;
  text-align: center;
  text-transform: uppercase;
  text-decoration: none;
  font-family: "montserrat";
  font-weight: 800;
  font-size: 25px;
  letter-spacing: 2px;
  line-height: 1;
}

a:hover {
  color: grey;
}

.st0{fill:none;stroke:#FFFFFF;stroke-width:6;stroke-miterlimit:10;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="container">
  <div id="menu">

    <ul id="submenu-1">
      <li id="m"><a href="#">what brings<br>you here?</a>

        <ul>
          <svg viewBox="0 0 900 50">
            <polyline points="450,0, 450,25, 112.5,25, 112.5,50" class="st0"/>
            <polyline points="450,0, 450,25, 337.5,25, 337.5,50" class="st0"/>
            <polyline points="450,0, 450,25, 562.5,25, 562.5,50" class="st0"/>
            <polyline points="450,0, 450,25, 787.5,25, 787.5,50" class="st0"/>
          </svg>
    
        </ul>
        
      </li>
    </ul>
  </div>
</div>

关于html - 当容器 ul 可见时绘制 svg 线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53374237/

相关文章:

javascript - jQuery JavaScript 不适用于 Wordpress,对无冲突模式语法感到困惑

html - 如何使用clojure解析html文件?

css - 动画 svg+xml base 64 css 背景在 IE 中不起作用

html - 使用 CSS 移除标签的效果

php - 没有 AJAX 的 HTML5 拖放是可能的吗?

javascript - 我应该怎么做才能使这个菜单栏在 Internet Explorer 7 及更高版本中正常工作?

html - 部署后响应式设计在 iPhone 上不起作用

与祖 parent 相关的 CSS 宽度百分比

html - 在 html5 中使用 svg 蒙版剪切照片的矩形区域

android - 在放大后使用 webview 在 Android 中显示 svg 不会缩小