问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0px;
margin: 0px;
}
body {
text-align: center;
overflow: auto;
}
img {
margin-top: 800px;
height: 230px;
width: 130px;
background-color: blueviolet;
}
.dow {
width: 300px;
height: 300px;
background-color: aqua;
}
</style>
</head>
<body>
<img src="" alt="" myImg="webp.webp.jpg" id="pic">
<div class="dow"></div>
<script>
let pic = document.getElementById('pic');
let myImgOne = pic.getAttribute('myImg');
window.onscroll = function () {
let scre = document.documentElement.clientHeight;
let h = pic.clientHeight;
let distance = pic.offsetTop;
let boundin = pic.getBoundingClientRect().top;
if (boundin < 0) {
console.log('nice');
pic.setAttribute('src', myImgOne);
}
return;
}
pic.onload = function () {
console.log('ok');
}
</script>
</body>
</html>