javascript - div onclick 不触发(css 动画箭头,可能的 div 大小/覆盖问题)

标签 javascript html css

我正在使用带有以下代码的动画箭头:

function startDownload() {
    alert("Hi");
}
.arrow {
    cursor: pointer;
    height: 120px;
    position: relative;
    transition: transform 0.1s;
    width: 80px;
    /*display: inline-block;*/
}

.arrow-top, .arrow-bottom {
    background-color: #666;
    height: 4px;
    left: -5px;
    position: absolute;
    top: 50%;
    width: 100%;
}

    .arrow-top:after, .arrow-bottom:after {
        background-color: #fff;
        content: '';
        height: 100%;
        position: absolute;
        top: 0;
        transition: all 0.15s;
    }

.arrow-top {
    transform: rotate(45deg);
    transform-origin: bottom right;
}

    .arrow-top:after {
        left: 100%;
        right: 0;
        transition-delay: 0s;
    }

.arrow-bottom {
    transform: rotate(-45deg);
    transform-origin: top right;
}

    .arrow-bottom:after {
        left: 0;
        right: 100%;
        transition-delay: 0.15s;
    }

.arrow:hover .arrow-top:after {
    left: 0;
    transition-delay: 0.15s;
}

.arrow:hover .arrow-bottom:after {
    right: 0;
    transition-delay: 0s;
}

.arrow:active {
    transform: translateX(-50%) translateY(-50%) scale(0.9);
}
<style type="text/css">
    body {
        background-color: black;
        /*background-color: #2B2A3F;*/
    }
</style>

<div class="arrow" id="start-arrow" onclick="startDownload()" style="z-index: 10;">
    <div class="arrow-top" style="border:1px solid black; z-index: 9;"></div>
    <div class="arrow-bottom" style="border:1px solid black; z-index: 9;"></div>
</div>

问题是当我点击两条箭头线时,onclick() 不起作用。仅当我单击两条线的周围区域时,它才有效,该区域被 id 为 start-arrow 的父 div 的边框包围。

所需的行为是让 onclickstart-arrow div 包围的整个区域中工作。

我尝试使用 z-index 使 start-arrow div 位于顶部,但它不起作用。我尝试弄乱 display 以及 CSS 中元素的 position 但也没有运气。不过我应该提一下,我正在寻找一种不包括更改元素的 position 属性的解决方案。

无论我在 start-arrow div 区域中的哪个位置单击,我怎样才能使 onclick 触发?

编辑:它在 Stack Overflow 中似乎工作得更好,为什么?但是,如果单击每行边框的顶部,它并不总是有效。我在 Firefox 中打开我的(完全相同的代码)(它在我的 asp.net 中也不起作用)。

最佳答案

为什么我们不简单地将元素包装到另一个父元素中并在其上绑定(bind)事件?我能够使用父元素('parent-id')解决它。

function startDownload() {
    alert("Hi");
}
.arrow {
    cursor: pointer;
    height: 120px;
    position: relative;
    transition: transform 0.1s;
    width: 80px;
    /*display: inline-block;*/
}

.arrow-top, .arrow-bottom {
    background-color: #666;
    height: 4px;
    left: -5px;
    position: absolute;
    top: 50%;
    width: 100%;
}

    .arrow-top:after, .arrow-bottom:after {
        background-color: #fff;
        content: '';
        height: 100%;
        position: absolute;
        top: 0;
        transition: all 0.15s;
    }

.arrow-top {
    transform: rotate(45deg);
    transform-origin: bottom right;
}

    .arrow-top:after {
        left: 100%;
        right: 0;
        transition-delay: 0s;
    }

.arrow-bottom {
    transform: rotate(-45deg);
    transform-origin: top right;
}

    .arrow-bottom:after {
        left: 0;
        right: 100%;
        transition-delay: 0.15s;
    }

.arrow:hover .arrow-top:after {
    left: 0;
    transition-delay: 0.15s;
}

.arrow:hover .arrow-bottom:after {
    right: 0;
    transition-delay: 0s;
}

.arrow:active {
    transform: translateX(-50%) translateY(-50%) scale(0.9);
}
<style type="text/css">
    body {
        background-color: black;
        /*background-color: #2B2A3F;*/
    }
</style>

<div id="parent-id" onclick="startDownload()">
  <div class="arrow" id="start-arrow"  style="z- 
   index: 10;">
      <div class="arrow-top" style="border:1px solid black; z-index: 9;"></div>
      <div class="arrow-bottom" style="border:1px solid black; z-index: 9;"></div>
   </div>
 </div>

关于javascript - div onclick 不触发(css 动画箭头,可能的 div 大小/覆盖问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55609812/

相关文章:

javascript - 如何在 Mobile Safari 中为 <video> 获取 Javascript 中的位置更改事件?

javascript - 如何在 JS/React-Native 中下载完整的 html 页面?

javascript - Angular4/5 依赖注入(inject)文档不起作用

javascript - Processing.js - sleep 、等待、超时、暂停、延迟?

html - 如何更改所选选项中的字体颜色?

html - Chrome/Safari 最近是否更改了 mix-blend-mode 的处理方式

php - 用 PHP 显示 MYSQL。想要将选定的行移动到另一个 sql 表并从现有表中删除

html - 背景图片 - 限制比例

css - CSS 的目录生成器 - 注释和文档相关问题

html - 对齐 div 标签内的文本