I am drawing arrows on my HTML page and I want to make it clickable so when that arrow is pressed, it acts as a button

huangapple go评论61阅读模式
英文:

I am drawing arrows on my HTML page and I want to make it clickable so when that arrow is pressed, it acts as a button

问题

我在网页顶部绘制了一个箭头,用于指示向下滚动。这很好,不过我希望可以单击该箭头,然后它会滚动到下一个 div。这些 div 已经有 ID,所以需要实现的是将该箭头变成一个按钮。我还在使用 Bootstrap,如果有帮助的话。

<!-- 开始代码段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-css -->

html {
  scroll-behavior: smooth;
}

.arrows {
  width: 60px;
  height: 72px;
  position: absolute;
  left: 50%;
  margin-left: -30px;
  bottom: 10%;
}

.arrows path {
  stroke: #ffffff;
  fill: transparent;
  stroke-width: 3px;
  animation: arrow 2s infinite;
  -webkit-animation: arrow 2s infinite;
}

@keyframes arrow {
  0% {
    opacity: 0;
  }
  40% {
    opacity: 1;
  }
  80% {
    opacity: 0;
  }
  100% {
    opacity: 0;
  }
}

@-webkit-keyframes arrow /* Safari 和 Chrome */
{
  0% {
    opacity: 0;
  }
  40% {
    opacity: 0;
  }
  80% {
    opacity: 0;
  }
  100% {
    opacity: 0;
  }
}

.arrows path.a1 {
  animation-delay: -1s;
  -webkit-animation-delay: -1s;
  /* Safari 和 Chrome */
}

.arrows path.a2 {
  animation-delay: -0.5s;
  -webkit-animation-delay: -0.5s;
  /* Safari 和 Chrome */
}

.arrows path.a3 {
  animation-delay: 0s;
  -webkit-animation-delay: 0s;
  /* Safari 和 Chrome */
}

<!-- 语言:lang-html -->

<svg class="arrows">
  <path class="a1" d="M0 0 L30 32 L60 0"></path>
  <path class="a2" d="M0 20 L30 52 L60 20"></path>
  <path class="a3" d="M0 40 L30 72 L60 40"></path>
</svg>

<!-- 结束代码段 -->

我尝试在绘制的开头使用锚点,但没有成功。

英文:

I am drawing an arrow at the top of my webpage that kind of indicates to scroll down. This is good however I would like it so that you can click that arrow and then it scrolls down to the next div. The divs already have ID's so all that needs to be implemented is making that arrow into a button. I am also using bootstrap if that helps.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

html {
  scroll-behavior: smooth;
}

.arrows {
  width: 60px;
  height: 72px;
  position: absolute;
  left: 50%;
  margin-left: -30px;
  bottom: 10%;
}

.arrows path {
  stroke: #ffffff;
  fill: transparent;
  stroke-width: 3px;
  animation: arrow 2s infinite;
  -webkit-animation: arrow 2s infinite;
}

@keyframes arrow {
  0% {
    opacity: 0;
  }
  40% {
    opacity: 1;
  }
  80% {
    opacity: 0;
  }
  100% {
    opacity: 0;
  }
}

@-webkit-keyframes arrow
/*Safari and Chrome*/

{
  0% {
    opacity: 0;
  }
  40% {
    opacity: 0;
  }
  80% {
    opacity: 0;
  }
  100% {
    opacity: 0;
  }
}

.arrows path.a1 {
  animation-delay: -1s;
  -webkit-animation-delay: -1s;
  /* Safari 和 Chrome */
}

.arrows path.a2 {
  animation-delay: -0.5s;
  -webkit-animation-delay: -0.5s;
  /* Safari 和 Chrome */
}

.arrows path.a3 {
  animation-delay: 0s;
  -webkit-animation-delay: 0s;
  /* Safari 和 Chrome */
}

<!-- language: lang-html -->

&lt;svg class=&quot;arrows&quot;&gt;
  &lt;path class=&quot;a1&quot; d=&quot;M0 0 L30 32 L60 0&quot;&gt;&lt;/path&gt;
  &lt;path class=&quot;a2&quot; d=&quot;M0 20 L30 52 L60 20&quot;&gt;&lt;/path&gt;
  &lt;path class=&quot;a3&quot; d=&quot;M0 40 L30 72 L60 40&quot;&gt;&lt;/path&gt;
&lt;/svg&gt;

<!-- end snippet -->

I tried using an anchor at the start of the drawing but it didnt work.

答案1

得分: 1

以下是代码部分的中文翻译:

document.querySelector(".wrapper").addEventListener("click", function (e) {
  const elem = e.target.closest(".svg");
  if (!elem) return;
  console.log(elem.dataset.id);
});
<div class="wrapper">
  <svg class="svg" data-id="1" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill-rule="evenodd" clip-rule="evenodd">
    <path d="M21.883 12l-7.527 6.235.644.765 9-7.521-9-7.479-.645.764 7.529 6.236h-21.884v1h21.883z"></path>
  </svg>

  <a href="#" class="svg" data-id="2">
    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill-rule="evenodd" clip-rule="evenodd">
      <path d="M21.883 12l-7.527 6.235.644.765 9-7.521-9-7.479-.645.764 7.529 6.236h-21.884v1h21.883z"></path>
    </svg>
  </a>

  <button class="svg" data-id="3">
    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill-rule="evenodd" clip-rule="evenodd">
      <path d="M21.883 12l-7.527 6.235.644.765 9-7.521-9-7.479-.645.764 7.529 6.236h-21.884v1h21.883z"></path>
    </svg>
  </button>
</div>

请注意,这是代码部分的翻译。如果您有其他问题或需要进一步的帮助,请随时提出。

英文:

Not sure what issues you are having with detecting the click. Below is svg, an anchor, and a button.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.querySelector(&quot;.wrapper&quot;).addEventListener(&quot;click&quot;, function (e) {
 const elem = e.target.closest(&quot;.svg&quot;);
 if (!elem) return;
 console.log(elem.dataset.id);
});

<!-- language: lang-html -->

&lt;div class=&quot;wrapper&quot;&gt;
&lt;svg class=&quot;svg&quot; data-id=&quot;1&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;24&quot; height=&quot;24&quot; fill-rule=&quot;evenodd&quot; clip-rule=&quot;evenodd&quot;&gt;&lt;path d=&quot;M21.883 12l-7.527 6.235.644.765 9-7.521-9-7.479-.645.764 7.529 6.236h-21.884v1h21.883z&quot;/&gt;&lt;/svg&gt;

&lt;a href=&quot;#&quot; class=&quot;svg&quot; data-id=&quot;2&quot;&gt;
  &lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;24&quot; height=&quot;24&quot; fill-rule=&quot;evenodd&quot; clip-rule=&quot;evenodd&quot;&gt;&lt;path d=&quot;M21.883 12l-7.527 6.235.644.765 9-7.521-9-7.479-.645.764 7.529 6.236h-21.884v1h21.883z&quot;/&gt;&lt;/svg&gt;
&lt;/a&gt;


&lt;button class=&quot;svg&quot; data-id=&quot;3&quot;&gt;
&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;24&quot; height=&quot;24&quot; fill-rule=&quot;evenodd&quot; clip-rule=&quot;evenodd&quot;&gt;&lt;path d=&quot;M21.883 12l-7.527 6.235.644.765 9-7.521-9-7.479-.645.764 7.529 6.236h-21.884v1h21.883z&quot;/&gt;&lt;/svg&gt;
&lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月14日 05:28:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定