悬停功能干扰了 href 标签
Posted
技术标签:
【中文标题】悬停功能干扰了 href 标签【英文标题】:Hover function interfering with a href tag 【发布时间】:2017-05-12 07:33:38 【问题描述】:我在这里有这段代码,我想要完成的是可点击的图标,但似乎我拥有的悬停功能不允许我点击图标。我已经尝试过 z-index 但这似乎不起作用。
body
background-image: url("background.png");
background-size: 100%;
background-repeat: no-repeat;
background-color: rgb(19,68,97)
h1
font-family: 'Libre Baskerville', serif;
font-size: 50px;
padding: 0px 0px 0px 0px;
display: inline;
p
font-family: 'Quicksand', sans-serif;
font-size: 15px;
padding: 0px 0px 0px 0px;
display: inline;
.boxAnimation
width: 520px;
height: 300px;
position: relative;
background: rgba(255,255,255,0.3);
display: inline-block;
margin: 0 10px;
cursor: pointer;
color: #fff;
box-shadow: inset 0 0 0 3px rgba(192,192,192,1);
-webkit-transition: background 0.4s 0.5s;
transition: background 0.4s 0.5s;
z-index: 0;
margin: center;
svg
position: absolute;
top: 0;
left: 0;
z-index: 0;
svg line
stroke-width: 6;
stroke: #fff;
fill: none;
stroke-dasharray: 250;
-webkit-transition: -webkit-transform .6s ease-out;
transition: transform .6s ease-out;
z-index: 0;
div:hover
background: rgba(255,255,255,0);
-webkit-transition-delay: 0s;
transition-delay: 0s;
div:hover svg line.top
-webkit-transform: translateX(-400px);
transform: translateX(-400px);
div:hover svg line.bottom
-webkit-transform: translateX(400px);
transform: translateX(400px);
div:hover svg line.left
-webkit-transform: translateY(400px);
transform: translateY(400px);
div:hover svg line.right
-webkit-transform: translateY(-400px);
transform: translateY(-400px);
.icons
z-index: 5;
<link href="https://fonts.googleapis.com/css?family=Quicksand" rel="stylesheet">
<script src="https://use.fontawesome.com/e0037e252a.js"></script>
<body>
<center>
<div class="boxAnimation"> <svg xmlns="http://www.w3.org/2000/svg" >
<line class="top" x1="0" y1="0" x2="1560" y2="0"/>
<line class="left" x1="0" y1="360" x2="0" y2="-720"/>
<line class="bottom" x1="520" y1="360" x2="-1040" y2="360"/>
<line class="right" x1="520" y1="0" x2="520" y2="1080"/>
</svg>
<h1>Donia Amer</h1>
<br>
<p>< insert title ></p>
<br>
<br>
<div class="icons"> <a href="www.google.com"> <i class="fa fa-linkedin-square fa-inverse" aria-hidden="true" style="font-size:45px;"></i></a> <i class="fa fa-github-square" aria-hidden="true" style="font-size:45px;"></i> <i class="fa fa-twitter-square" aria-hidden="true" style="font-size:45px;"></i> <i class="fa fa-envelope-square" aria-hidden="true" style="font-size:45px;"></i> </div>
</div>
</center>
</body>
【问题讨论】:
设置你的svg
z-index: -1
而不是 0;
【参考方案1】:
将position:relative
添加到.icons
z-index 属性指定元素的堆栈顺序。具有较大堆栈顺序的元素始终位于具有较低堆栈顺序的元素之前。
注意:z-index 仅适用于定位元素(位置:绝对、位置:相对或位置:固定)。
这里是例子
body
background-image: url("background.png");
background-size: 100%;
background-repeat: no-repeat;
background-color: rgb(19,68,97)
h1
font-family: 'Libre Baskerville', serif;
font-size: 50px;
padding: 0px 0px 0px 0px;
display: inline;
p
font-family: 'Quicksand', sans-serif;
font-size: 15px;
padding: 0px 0px 0px 0px;
display: inline;
.boxAnimation
width: 520px;
height: 300px;
position: relative;
background: rgba(255,255,255,0.3);
display: inline-block;
margin: 0 10px;
cursor: pointer;
color: #fff;
box-shadow: inset 0 0 0 3px rgba(192,192,192,1);
-webkit-transition: background 0.4s 0.5s;
transition: background 0.4s 0.5s;
z-index: 0;
margin: center;
svg
position: absolute;
top: 0;
left: 0;
z-index: 0;
svg line
stroke-width: 6;
stroke: #fff;
fill: none;
stroke-dasharray: 250;
-webkit-transition: -webkit-transform .6s ease-out;
transition: transform .6s ease-out;
z-index: 0;
div:hover
background: rgba(255,255,255,0);
-webkit-transition-delay: 0s;
transition-delay: 0s;
div:hover svg line.top
-webkit-transform: translateX(-400px);
transform: translateX(-400px);
div:hover svg line.bottom
-webkit-transform: translateX(400px);
transform: translateX(400px);
div:hover svg line.left
-webkit-transform: translateY(400px);
transform: translateY(400px);
div:hover svg line.right
-webkit-transform: translateY(-400px);
transform: translateY(-400px);
.icons
z-index: 5;
position:relative;
<link href="https://fonts.googleapis.com/css?family=Quicksand" rel="stylesheet">
<script src="https://use.fontawesome.com/e0037e252a.js"></script>
<body>
<center>
<div class="boxAnimation"> <svg xmlns="http://www.w3.org/2000/svg" >
<line class="top" x1="0" y1="0" x2="1560" y2="0"/>
<line class="left" x1="0" y1="360" x2="0" y2="-720"/>
<line class="bottom" x1="520" y1="360" x2="-1040" y2="360"/>
<line class="right" x1="520" y1="0" x2="520" y2="1080"/>
</svg>
<h1>Donia Amer</h1>
<br>
<p>< Developer + Algorithm Enthusiast ></p>
<br>
<br>
<div class="icons"> <a href="www.google.com"> <i class="fa fa-linkedin-square fa-inverse" aria-hidden="true" style="font-size:45px;"></i></a> <i class="fa fa-github-square" aria-hidden="true" style="font-size:45px;"></i> <i class="fa fa-twitter-square" aria-hidden="true" style="font-size:45px;"></i> <i class="fa fa-envelope-square" aria-hidden="true" style="font-size:45px;"></i> </div>
</div>
</center>
</body>
【讨论】:
非常感谢!你介意向我解释一下为什么会这样吗? @DoniaAmer 我可以知道你为什么不接受我的回答吗?【参考方案2】:诀窍是在图标选择器中添加新属性 position:relative
提醒
z-index 仅适用于定位元素
CSS
.icons
z-index: 5;
position:relative;
DEMO
【讨论】:
【参考方案3】:只需将svg的z-index属性设置为-value
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<title>Donia Amer</title>
<link href="https://fonts.googleapis.com/css?family=Quicksand" rel="stylesheet">
<script src="https://use.fontawesome.com/e0037e252a.js"></script>
<style>
body
background-image:url("background.png");
background-size: 100%;
background-repeat: no-repeat;
background-color: rgb(19,68,97)
h1
font-family: 'Libre Baskerville', serif;
font-size: 50px;
padding: 0px 0px 0px 0px;
display: inline;
p
font-family: 'Quicksand', sans-serif;
font-size: 15px;
padding: 0px 0px 0px 0px;
display: inline;
.boxAnimation
width: 520px;
height: 300px;
position: relative;
background:rgba(255,255,255,0.3);
display: inline-block;
margin: 0 10px;
cursor: pointer;
color: #fff;
box-shadow: inset 0 0 0 3px rgba(192,192,192,1);
-webkit-transition: background 0.4s 0.5s;
transition: background 0.4s 0.5s;
z-index: 0;
margin: center;
svg
position: absolute;
top: 0;
left: 0;
z-index: -1;
svg line
stroke-width: 6;
stroke: #fff;
fill: none;
stroke-dasharray: 250;
-webkit-transition: -webkit-transform .6s ease-out;
transition: transform .6s ease-out;
z-index: 0;
div:hover
background: rgba(255,255,255,0);
-webkit-transition-delay: 0s;
transition-delay: 0s;
div:hover svg line.top
-webkit-transform: translateX(-400px);
transform: translateX(-400px);
div:hover svg line.bottom
-webkit-transform: translateX(400px);
transform: translateX(400px);
div:hover svg line.left
-webkit-transform: translateY(400px);
transform: translateY(400px);
div:hover svg line.right
-webkit-transform: translateY(-400px);
transform: translateY(-400px);
.icons
z-index: 5;
</style>
</head>
<body>
<br><br><br><br><br><br><br><br><br><br><br><br>
<center>
<div class="boxAnimation">
<svg xmlns="http://www.w3.org/2000/svg" >
<line class="top" x1="0" y1="0" x2="1560" y2="0"/>
<line class="left" x1="0" y1="360" x2="0" y2="-720"/>
<line class="bottom" x1="520" y1="360" x2="-1040" y2="360"/>
<line class="right" x1="520" y1="0" x2="520" y2="1080"/>
</svg>
<h1>Donia Amer</h1><br>
<p>< Developer + Algorithm Enthusiast ></p>
<br>
<br>
<div class="icons">
<a href="https://***.com/questions/ask">
<i class="fa fa-linkedin-square fa-inverse" aria-hidden="true" style="font-size:45px;"></i></a>
<i class="fa fa-github-square" aria-hidden="true" style="font-size:45px;"></i>
<i class="fa fa-twitter-square" aria-hidden="true" style="font-size:45px;"></i>
<i class="fa fa-envelope-square" aria-hidden="true" style="font-size:45px;"></i>
</div>
</div>
</center>
</body>
</html>
【讨论】:
以上是关于悬停功能干扰了 href 标签的主要内容,如果未能解决你的问题,请参考以下文章