英文:
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 -->
<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>
<!-- 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(".wrapper").addEventListener("click", function (e) {
const elem = e.target.closest(".svg");
if (!elem) return;
console.log(elem.dataset.id);
});
<!-- language: lang-html -->
<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"/></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"/></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"/></svg>
</button>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论