節課介紹CSS的浮動。
lt;head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<style>
div{
position:absolute;
}
</style>
</head>
<body>
<div id="divimg">
<img src="http://www.jungjaehyung.com/uploadfile/2024/1012/20241012041128400.png" width="150" height="150">
</div>
<script language="javascript" type="text/javascript">
//獲取圖片div對象
var img=document.getElementById("divimg");
//設置div左上角 起始點坐標
var x=0,y=0;
//設置圖片的行進速度
var xSpeed=5,ySpeed=5;
//設置圖片的最大浮動高度和寬度
var w=document.body.clientWidth-150,h=document.body.clientHeight-150;
function floatimg(){
//比較圖片是否到達邊界
//如果到達邊界以后,我控制圖片改變方向
if(x>w||x<0) xSpeed=-xSpeed;
if(y>h||y<0) ySpeed=-ySpeed;
//如果沒有到達邊界,設置圖片的左上角坐標,
//設置坐標值 起始坐標+速度
x+=xSpeed;
y+=ySpeed;
img.style.top=y+"px";
img.style.left=x+"px";
setTimeout("floatimg()",50);
}
floatimg();
</script>
</body>
HTML 頁面布局中,浮動元素被用于控制元素的相鄰位置,以水平或垂直方向上排版。了解如何正確使用 float 屬性將有助于您對頁面進行精細控制。
水平浮動
水平浮動用于將元素排版并行。當元素被設置為 float:left 或 float:right 時,它們就會在文檔流中垂直方向上緊縮,直到遇到相鄰的非浮動元素或直到頁面底部。
垂直浮動
垂直浮動用于將元素垂直排列。當元素被設置為 float:top 或 float:bottom 時,它們就會在文檔流中水平方向上緊縮,直到遇到相鄰的非浮動元素或直到頁面右部。
清除浮動
當一個元素的浮動效果覆蓋了其相鄰元素時,就會發生交叉疊加。 為了避免這種情況,可以在浮動元素的相鄰元素中添加 clear:both 屬性。
示例
html
<div>
<div style="float: left">
左邊元素
</div>
<div style="float: right">
右邊元素
</div>
<div style="clear: both"></div>
</div>
結論
浮動元素是頁面布局中強大的工具,可以水平或垂直方向上排列元素。通過正確使用 float 屬性,您可以根據要求對頁面進行精細控制。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。