javascript - 悬停时淡入背景图像
问题描述
将鼠标悬停在文本上时,我有 jquery 来更改背景图像。我想添加淡入淡出效果。
这是我现在拥有的代码:
$(document).ready(function(){
$("#anatomyNow").hover(function(){
$("#bg").css("background-image", "url(image/anatomyNow5.png)");
}, function(){
$("#bg").css("background-image", "url(image/anatomyNow5.png)");
});
});
我试图添加下面的代码,但它不起作用。
$(document).ready(function(){
$("#anatomyNow").hover(function(){
$("#bg").fadeIn();
});
});
更新:谢谢大家的回答。
我想要的效果是这样的: https ://www.christinewalthall.com/work
当您将鼠标悬停在文本上时,背景图像将发生变化。我已经设法做到了,但是图像变化太快了。我希望添加效果,使图像不会发生显着变化。
解决方案
如果我没有误解您的要求,那么这就是您想要的。
$(document).ready(function() {
$("#effect").hover(function() {
$(this).animate({
opacity: '1'
}, "slow");
}, function() {
$(this).animate({
opacity: '0.5'
}, "slow");
});
});
#effect {
padding: 0.4em;
background: #555 url("https://thumb9.shutterstock.com/display_pic_with_logo/77318/1007648908/stock-photo-sunrise-beam-in-the-beautiful-park-1007648908.jpg");
opacity: 0.5;
}
#effect {
max-width: 490px;
height: 320px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="effect" class="ui-widget-content ui-corner-all"></div>
推荐阅读
- mysql - mysql 在一个查询中计算两个表中的行数?
- scala - 如何在 Spark Scala 中按值排序
- go - 下面这个抛出指针错误的代码有什么问题
- python - 如何使用 python 在 KoboToolbox 中编辑记录
- r - 如何将4个掷骰子的样本相乘?
- angular - 如何处理角度上的多个帖子请求?
- selenium - 将存储在 Excel 中的 Xpath 值转换为 Selenium 循环
- c# - 带有 systemctl 单元的 net core 5 应用程序不显示带有 journalctl 的日志
- python - 如何使用beautifulsoup 抓取某个表并变成pandas 数据框?
- linux - netstat 缩短/截断主机的 fqdn