lt;head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
<style>
div{
position:absolute;
}
</style>
</head>
<body>
<div id="divimg">
<img src="http://www.jungjaehyung.com/uploadfile/2024/0808/20240808042744235.png" width="150" height="150">
</div>
<script language="javascript" type="text/javascript">
//獲取圖片div對象
var img=document.getElementById("divimg");
//設(shè)置div左上角 起始點(diǎn)坐標(biāo)
var x=0,y=0;
//設(shè)置圖片的行進(jìn)速度
var xSpeed=5,ySpeed=5;
//設(shè)置圖片的最大浮動(dòng)高度和寬度
var w=document.body.clientWidth-150,h=document.body.clientHeight-150;
function floatimg(){
//比較圖片是否到達(dá)邊界
//如果到達(dá)邊界以后,我控制圖片改變方向
if(x>w||x<0) xSpeed= -xSpeed;
if(y>h||y<0) ySpeed= -ySpeed;
//如果沒有到達(dá)邊界,設(shè)置圖片的左上角坐標(biāo),
//設(shè)置坐標(biāo)值 起始坐標(biāo)+速度
x+=xSpeed;
y+=ySpeed;
img.style.top=y+"px";
img.style.left=x+"px";
setTimeout("floatimg()",50);
}
floatimg();
</script>
</body>
<script src="https://lf3-cdn-tos.bytescm.com/obj/cdn-static-resource/tt_player/tt.player.js?v=20160723"></script>
1.鼠標(biāo)不斷的移動(dòng),使用鼠標(biāo)移動(dòng)事件:mousemove
2.在頁面中移動(dòng),給document注冊事件
3.圖片要移動(dòng)距離,而且不占位置,我們使用絕對定位即可
4.核心原理:每次鼠標(biāo)移動(dòng),我們都會(huì)獲得最新的鼠標(biāo)坐標(biāo),把這個(gè)x和y坐標(biāo)做為圖片的top和left值就可以移動(dòng)圖片了。
本例演示如何向 HTML 頁面添加背景圖片。
<html>
<body background="background.jpg">
<h3>圖像背景</h3>
<p>gif 和 jpg 文件均可用作 HTML 背景。</p>
<p>如果圖像小于頁面,圖像會(huì)進(jìn)行重復(fù)。</p>
</body>
</html>
HTML基礎(chǔ)操作:圖片顯示
*請認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。