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

标签 javascript html css web fade

到目前为止,我已经能够做到当光标悬停在 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>

最佳答案

body 标记添加转换规则。同样的事情可以在 css 中完成,无需 javascript。

function changeBgImage() {
  document.body.style.backgroundImage = "url('https://s1.1zoom.ru/big0/284/Summer_Pond_Fence_Trees_496376.jpg')";
}

function ogBgImage() {
  document.body.style.backgroundImage = "url('https://pristor.ru/wp-content/uploads/2017/02/leto12.jpg')";
}
body {
  background-image: url('https://pristor.ru/wp-content/uploads/2017/02/leto12.jpg');
  background-repeat: no-repeat;
  background-size: cover;
  
  transition: all 0.7s linear;
  
}
<body>
  <div class="gwraith">
    <a href="../Legends/wraith.html">
    <img src="https://begin-english.ru/img/word/refresh.jpg" width="130vw" class="wraith"
                            onmouseover="changeBgImage();" onmouseout="ogBgImage();">
    </a>
  </div>
</body>

关于javascript - 在悬停时使用 javascript 或 css 淡入背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64905761/

相关文章:

javascript - 带有模块加载器的 Emscripten

html - 当 NSURLResponse 对于 textEncodingName 返回 nil 时检测 HTML 编码

java - 抓取数据。保存存档?

html - 试图使我的 Fix 导航栏的内容在左侧,一个白框,但它不起作用?

css - 如何让 [container>ul>li>a] 占据 [container] 的全高

javascript - 在 AJAX 中更新 Chart.js 无法正确呈现图表

javascript - HTML/Jquery 移除被下面的div占用的div位置。如何?

javascript - 从 Typescript 类型/接口(interface)生成 Joi 模式

用于从已发布的 URL(公共(public)网络链接)下载文件的 Python 脚本

html - 响应式设备类型定位