当你用鼠标光标悬停链接时,我想让我的背景图像模糊,让我们说5px.有没有简单的方法来实现这一目标?我有点纠结于课程和id在这里......
#pic {
background: url(http://www.metalinjection.net/wp-content/uploads/2014/07/space-metal.jpg);
background-attachment: fixed;
background-repeat: no-repeat;
background-size: cover;
height: 500px;
/*blur using this function*/
filter: blur(0);
-webkit-filter: blur(0);
-moz-filter: blur(0);
-o-filter: blur(0);
-ms-filter: blur(0);
}
.banner_link {
font-family: 'Raleway';
letter-spacing: 0.2em;
font-size: 13px;
color: #ffffff;
text-align: center;
line-height: 16px;
padding-top: 45px;
text-transform: uppercase;
}
.banner_link a:after {
content: '';
display: block;
margin: auto;
height: 1px;
width: 90px;
background: #ffffff;
transition: width .2s ease, background-color .5s ease;
}
.banner_link a:hover:after {
width: 0px;
background: transparent;
}
.banner_link a:hover #pic {
filter: blur(5px);
-webkit-filter: blur(5px);
-moz-filter: blur(5px);
-o-filter: blur(5px);
-ms-filter: blur(5px);
}Run Code Online (Sandbox Code Playgroud)
<div id="pic" class="banner">
<div class="banner_link"><a>Link</a>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
这个问题是,你正试图穿越了文档树与CSS.CSS中没有父选择器,因此当内部<a>元素悬停时,您只能依靠JS来切换模糊效果.
这可以使用原生JS轻松实现,但我选择使用jQuery,因为相对容易使用.
诀窍很简单:绝对定位背景图像的模糊版本,嵌套在伪元素中,比如说::before,其不透明度设置为零.当光标位于内部<a>元素上时,切换一个类,比如.blur将伪元素的不透明度设置为1.
我们不能使用JS来设置伪元素的CSS属性的原因是因为JS无法访问它.
$(function() {
$('.banner_link a').hover(function() {
$('#pic').addClass('blur');
}, function() {
$('#pic').removeClass('blur');
});
});Run Code Online (Sandbox Code Playgroud)
#pic {
background: url(http://www.metalinjection.net/wp-content/uploads/2014/07/space-metal.jpg);
background-attachment: fixed;
background-repeat: no-repeat;
background-size: cover;
height: 500px;
position: relative;
overflow: hidden;
}
#pic::before {
position: absolute;
content: '';
display: block;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: url(http://www.metalinjection.net/wp-content/uploads/2014/07/space-metal.jpg);
background-attachment: fixed;
background-repeat: no-repeat;
background-size: cover;
-webkit-filter: blur(5px);
filter: blur(5px);
opacity: 0;
transition: opacity .5s ease-in-out;
}
#pic.blur::before {
opacity: 1;
}
.banner_link {
font-family: 'Raleway';
letter-spacing: 0.2em;
font-size: 13px;
color: #ffffff;
text-align: center;
line-height: 16px;
padding-top: 45px;
position: relative;
text-transform: uppercase;
}
.banner_link a::after {
content: '';
display: block;
margin: auto;
height: 1px;
width: 90px;
background: #ffffff;
transition: width .2s ease, background-color .5s ease;
}
.banner_link a:hover:after {
width: 0px;
background: transparent;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pic" class="banner">
<div class="banner_link"><a>Link</a>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7712 次 |
| 最近记录: |