悬停时使用javascript或css在背景图像中淡入

悬停时使用javascript或css在背景图像中淡入,javascript,html,css,web,fade,Javascript,Html,Css,Web,Fade,到目前为止,我已经能够使光标悬停在div上时,主体中的背景图像出现。我需要添加一个淡入动画到这个。我一直在这里寻找解决方案,但一直无法解决。我在javascript方面没有任何经验 enter code here <script> changeBgImage = () => { document.body.style.backgroundImage = "url('../Images/Background/wraithback.jpg')&

到目前为止,我已经能够使光标悬停在div上时,主体中的背景图像出现。我需要添加一个淡入动画到这个。我一直在这里寻找解决方案,但一直无法解决。我在javascript方面没有任何经验

enter code here
<script>


    changeBgImage = () => {
        document.body.style.backgroundImage = "url('../Images/Background/wraithback.jpg')";
        console.log("working")




    }
    ogBgImage = () => {
        document.body.style.backgroundImage = "url('../Images/Background/black.jpg')";
        console.log("working")
    }


</script>

<style>
    body {
        background-image: url('../Images/Background/black.jpg');
    }
</style>
<body>
<div class="gwraith"><a href="../Legends/wraith.html ">
                    <img src="../Images/Legends_pics/wraithchibi.png" width="130vw" class="wraith"
                        onmouseover="changeBgImage();" onmouseout="ogBgImage();">
                </a>
</body>
在此处输入代码
changeBgImage=()=>{
document.body.style.backgroundImage=“url('../Images/Background/wraithback.jpg')”;
控制台日志(“工作”)
}
ogBgImage=()=>{
document.body.style.backgroundImage=“url('../Images/Background/black.jpg')”;
控制台日志(“工作”)
}
身体{
背景图像:url('../Images/background/black.jpg');
}

将转换规则添加到
正文
标记中。同样可以在css中完成,而不需要javascript

enter code here
<script>


    changeBgImage = () => {
        document.body.style.backgroundImage = "url('../Images/Background/wraithback.jpg')";
        console.log("working")




    }
    ogBgImage = () => {
        document.body.style.backgroundImage = "url('../Images/Background/black.jpg')";
        console.log("working")
    }


</script>

<style>
    body {
        background-image: url('../Images/Background/black.jpg');
    }
</style>
<body>
<div class="gwraith"><a href="../Legends/wraith.html ">
                    <img src="../Images/Legends_pics/wraithchibi.png" width="130vw" class="wraith"
                        onmouseover="changeBgImage();" onmouseout="ogBgImage();">
                </a>
</body>
函数changeBgImage(){
document.body.style.backgroundImage=“url('https://s1.1zoom.ru/big0/284/Summer_Pond_Fence_Trees_496376.jpg')";
}
函数ogBgImage(){
document.body.style.backgroundImage=“url('https://pristor.ru/wp-content/uploads/2017/02/leto12.jpg')";
}
正文{
背景图像:url('https://pristor.ru/wp-content/uploads/2017/02/leto12.jpg');
背景重复:无重复;
背景尺寸:封面;
过渡:所有0.7s线性;
}

我没能用身体做这件事。但是可以拉伸基础div并更改其不透明度

const-appDiv=document.getElementById(“app”);
appDiv.addEventListener(“鼠标悬停”,showBodyBackground);
appDiv.addEventListener(“鼠标出口”,隐藏背景);
函数showBodyBackground(){
document.getElementById(“bg”).classList.add(“隐藏”);
}
函数hideBodyBackground(){
document.getElementById(“bg”).classList.remove(“隐藏”);
}
。可见{
背景:url('https://www.bouwendnederland.nl/media/6502/rijnhaven-impressie-602-x-402.jpg');
过渡:不透明度1.5s线性;
不透明度:1;
}
.隐藏{
不透明度:0;
}
.伸展{
位置:绝对位置;
宽度:100%;
身高:100%;
}
#应用程序{
宽度:100px;
高度:50px;
背景:浅蓝色;
文本对齐:居中;
保证金:0自动;
位置:相对位置;
}

让我停下来!

我们不能只淡出body,或者任何可能取代它的wrapper div,因为那样会淡出所有内容。我们也不能直接淡入背景图像,因为CSS没有这种能力。但是我们可以把两个背景图像放在身体的两个伪元素中,在前后,然后可以设置动画使其淡入淡出。代码希望在鼠标悬停时淡入一个背景,在鼠标悬停时淡出

使用了两个背景图像,一个称为黑色。这里的代码会随着其他图像的淡入而淡出,但如果需要,可以轻松删除

将鼠标悬停在齿轮图像上以在第二个图像中淡出,将鼠标悬停在齿轮上以淡出


changeBgImage=()=>{
document.body.classList.toggle('showbefore');
document.body.classList.toggle('showafter');
控制台日志(“工作”)
}
ogBgImage=()=>{
document.body.classList.toggle('showbefore');
document.body.classList.toggle('showafter');
控制台日志(“工作”)
}
身体{
位置:相对位置;
高度:100vh;/*我添加这个只是为了覆盖整个窗口,您可能不需要它*/
}
身体:之前,身体:之后{
不透明度:0;
内容:'';
显示:块;
位置:绝对位置;
左:0;
排名:0;
宽度:100%;
身高:100%;
z指数:-1;
背景尺寸:cover;/*我添加这个只是为了获得整个窗口的背景-你可能想要也可能不想要*/
背景重复:不重复不重复;
背景位置:中心;
动画持续时间:2s;/*更改为您想要的状态*/
动画填充模式:正向;
}
身体:以前{
背景图像:线性渐变(黑色,黑色);/*将此更改为url(“您的背景图像”)*/
动画名称:已显示;
}
正文:之后{
背景图像:url('https://ahweb.org.uk/christmas 卡片2020 front.jpg');
动画名称:unshawn;
}
body.showbefore:before,body.showafter:after{
动画名称:show;
}
body.showafter:before,body.showbefore:after{
动画名称:取消显示;
} 
@关键帧取消显示{
从{
不透明度:0;
}
到{
不透明度:0;
}
}
@显示的关键帧{
从{
不透明度:1;
}
到{
不透明度:1;
}
}
@取消显示关键帧{
从{
不透明度:1;
}
到{
不透明度:0;
}
}
@关键帧显示{
从{
不透明度:0;
}
到{
不透明度:1;
}
}

我想,作者不想更改背景图像。是否可以将背景图像平滑地设置为“无”?是的,使用不透明规则如果主体具有不透明性:0,它将隐藏内部的所有内容,不是吗?当然,因此,最好在(儿童)标签内执行此操作。这正是我需要的,谢谢你的回答。这是一个关于它外观的视频:看起来很棒!我投票赞成这个答案。这种方法的优点是它不会改变HTML,我们也不需要“对抗”由我的答案中的一个额外div引起的卷轴等副作用。很高兴它起作用了-谢谢,让人高兴知道一些有用的东西!嘿,我试着做你的代码,它有点工作,现在当我悬停在链接的背景图像出现,然后淡入黑色。我想要的是,当我将鼠标悬停在链接上时,图像淡入淡出。我添加了一个视频,以便更好地理解。好的,谢谢你的澄清,所以你希望背景图像(称为黑色)保持不变(一点也不淡入淡出),而另一个图像淡入淡出?我来看看。嗯,允许删除“black.jpg”,因为淡入的图像已经有了黑色背景