Idea:
1. Get the distance of the object from the top and left side;
2. Get the element object;
3. Get the scrolling distance of the scroll bar when the scroll bar scrolls;
4. The function is executed when the scroll bar is scrolling: the distance of the object from the top/left becomes the original distance from the top/left + the number of pixels scrolled by the scroll bar.
html code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="left">
<img src="images/z1.jpg" alt=""/>
</div>
<div id="right">
<img src="images/z2.jpg" alt=""/>
</div>
</body>
</html> css code:
*{
margin: 0;
padding: 0;
}
body{
width: 2000px;
height: 2000px;
}
.left{
position: absolute;
width: 110px;
height: 110px;
top: 100px;
left: 50px;
}
.right{
position: absolute;
width: 110px;
height: 110px;
top: 100px;
left: 1360px;
} js code:
var leftT;//The distance between the left p and the top var leftL;//The distance between the left p and the left var rightT;//The distance between the right p and the top var rightL;//The distance between the right p and the left var objLeft;//P document object on the left var objRight;//P document object on the right function place(){
objLeft=document.getElementById("left");
objRight=document.getElementById("right");
leftT=document.defaultView.getComputedStyle(objLeft,null).top;
leftL=document.defaultView.getComputedStyle(objLeft,null).left;
rightT=document.defaultView.getComputedStyle(objRight,null).top;
rightL=document.defaultView.getComputedStyle(objRight,null).left;
}
//Get the number of pixels scrolled by the scroll bar function move(){
var scrollT=document.documentElement.scrollTop;
var scrollL=document.documentElement.scrollLeft;
//Set the pixels from the left p to the top objLeft.style.top=parseInt(leftT)+scrollT+"px";
objLeft.style.left=parseInt(leftL)+scrollL+"px";
objRight.style.top=parseInt(rightT)+scrollT+"px";
objRight.style.left=parseInt(rightL)+scrollL+"px";
}
window.onload=place;
window.onscroll=move; Related recommendations: [JavaScript video tutorial]
The above is to use js to fix the picture at a certain position on the screen! For more details, please pay attention to other related articles on the php Chinese website!