頁可見區域寬:document.body.clientWidth
網頁可見區域高:document.body.clientHeight
網頁可見區域寬:document.body.offsetWidth (包括邊線的寬)
網頁可見區域高:document.body.offsetHeight (包括邊線的寬)
網頁正文全文寬:document.body.scrollWidth
網頁正文全文高:document.body.scrollHeight
網頁被卷去的高:document.body.scrollTop
網頁被卷去的左:document.body.scrollLeft
網頁正文部分上:window.screenTop
網頁正文部分左:window.screenLeft
屏幕分辨率的高:window.screen.height
屏幕分辨率的寬:window.screen.width
屏幕可用工作區高度:window.screen.availHeight
屏幕可用工作區寬度:window.screen.availWidth
HTML精確定位:scrollLeft,scrollWidth,clientWidth,offsetWidth
scrollHeight: 獲取對象的滾動高度。
scrollLeft:設置或獲取位于對象左邊界和窗口中目前可見內容的最左端之間的距離
scrollTop:設置或獲取位于對象最頂端和窗口中可見內容的最頂端之間的距離
scrollWidth:獲取對象的滾動寬度
offsetHeight:獲取對象相對于版面或由父坐標 offsetParent 屬性指定的父坐標的高度
offsetLeft:獲取對象相對于版面或由 offsetParent 屬性指定的父坐標的計算左側位置
offsetTop:獲取對象相對于版面或由 offsetTop 屬性指定的父坐標的計算頂端位置
event.clientX 相對文檔的水平座標
event.clientY 相對文檔的垂直座標
event.offsetX 相對容器的水平坐標
event.offsetY 相對容器的垂直坐標
document.documentElement.scrollTop 垂直方向滾動的值
event.clientX+document.documentElement.scrollTop 相對文檔的水平座標+垂直方向滾動的量
IE,FireFox 差異如下:
IE6.0、FF1.06+:
clientWidth = width + padding
clientHeight = height + padding
offsetWidth = width + padding + border
offsetHeight = height + padding + border
IE5.0/5.5:
clientWidth = width - border
clientHeight = height - border
offsetWidth = width
offsetHeight = height
(需要提一下:CSS中的margin屬性,與clientWidth、offsetWidth、clientHeight、offsetHeight均無關)
網頁可見區域寬: document.body.clientWidth
網頁可見區域高: document.body.clientHeight
網頁可見區域寬: document.body.offsetWidth (包括邊線的寬)
網頁可見區域高: document.body.offsetHeight (包括邊線的高)
網頁正文全文寬: document.body.scrollWidth
網頁正文全文高: document.body.scrollHeight
網頁被卷去的高: document.body.scrollTop
網頁被卷去的左: document.body.scrollLeft
網頁正文部分上: window.screenTop
網頁正文部分左: window.screenLeft
屏幕分辨率的高: window.screen.height
屏幕分辨率的寬: window.screen.width
屏幕可用工作區高度: window.screen.availHeight
屏幕可用工作區寬度: window.screen.availWidth
-------------------
技術要點
本節代碼主要使用了Document對象關于窗口的一些屬性,這些屬性的主要功能和用法如下。
要得到窗口的尺寸,對于不同的瀏覽器,需要使用不同的屬性和方法:若要檢測窗口的真實尺寸,在Netscape下需要使用Window的屬性;在IE下需要 深入Document內部對body進行檢測;在DOM環境下,若要得到窗口的尺寸,需要注意根元素的尺寸,而不是元素。
Window對象的innerWidth屬性包含當前窗口的內部寬度。Window對象的innerHeight屬性包含當前窗口的內部高度。
Document對象的body屬性對應HTML文檔的標簽。Document對象的documentElement屬性則表示HTML文檔的根節點。
document.body.clientHeight表示HTML文檔所在窗口的當前高度。document.body. clientWidth表示HTML文檔所在窗口的當前寬度。
實現代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>請調整瀏覽器窗口</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<h2 align="center">請調整瀏覽器窗口大小</h2><hr>
<form action="#" method="get" name="form1" id="form1">
<!--顯示瀏覽器窗口的實際尺寸-->
瀏覽器窗口 的 實際高度: <input type="text" name="availHeight" size="4"><br>
瀏覽器窗口 的 實際寬度: <input type="text" name="availWidth" size="4"><br>
</form>
<script type="text/javascript">
<!--
var winWidth = 0;
var winHeight = 0;
function findDimensions() //函數:獲取尺寸
{
//獲取窗口寬度
if (window.innerWidth)
winWidth = window.innerWidth;
else if ((document.body) && (document.body.clientWidth))
winWidth = document.body.clientWidth;
//獲取窗口高度
if (window.innerHeight)
winHeight = window.innerHeight;
else if ((document.body) && (document.body.clientHeight))
winHeight = document.body.clientHeight;
//通過深入Document內部對body進行檢測,獲取窗口大小
if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth)
{
winHeight = document.documentElement.clientHeight;
winWidth = document.documentElement.clientWidth;
}
//結果輸出至兩個文本框
document.form1.availHeight.value= winHeight;
document.form1.availWidth.value= winWidth;
}
findDimensions();
//調用函數,獲取數值
window.onresize=findDimensions;
//-->
</script>
</body>
</html>
源程序解讀
(1)程序首先建立一個表單,包含兩個文本框,用于顯示窗口當前的寬度和高度,并且,其數值會隨窗口大小的改變而變化。
(2)在隨后的JavaScript代碼中,首先定義了兩個變量winWidth和winHeight,用于保存窗口的高度值和寬度值。
(3)然后,在函數findDimensions ( )中,使用window.innerHeight和window.innerWidth得到窗口的高度和寬度,并將二者保存在前述兩個變量中。
(4)再通過深入Document內部對body進行檢測,獲取窗口大小,并存儲在前述兩個變量中。
(5)在函數的最后,通過按名稱訪問表單元素,結果輸出至兩個文本框。
(6)在JavaScript代碼的最后,通過調用findDimensions ( )函數,完成整個操作。
看下效果圖
1、給目標div增加鼠標按下事件,記錄下div對象的位置(left,top)和鼠標點擊的位置(x,y)
2、監聽鼠標移動事件,在移動過程中計算出鼠標的移動位置(nx,xy),然后實時計算出鼠標從點擊到現在的偏移量,然后再將div的位置修改,這樣就實現了div位置的變動
ps:目標div的屬性position要設置absolute或者relative
3、div可以移動了,但什么時候停止移動呢?當然是鼠標彈起的時候,所以我們監聽鼠標彈起的事件,并結束移動
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>div-drag-每天一個知識點</title>
<style>
.dragable {
width: 200px;
height: 200px;
border: 1px solid darkorchid;
position: relative;
}
</style>
</head>
<body>
<div class="dragable"></div>
</body>
<script>
var dragDiv = document.querySelector(".dragable");
dragDiv.addEventListener("mousedown", function(e) {
//獲取div初始位置對象
var divRect = dragDiv.getBoundingClientRect();
//獲取鼠標點擊的位置
var downX = e.clientX;
var downY = e.clientY;
//開關打開
var startMove = true;
//設置樣式為移動光標
dragDiv.style.cursor = "move";
window.onmousemove = function(e) {
if (!startMove) {
return;
}
let newLeft = e.clientX - downX + divRect.left;
let newTop = e.clientY - downY + divRect.top;
dragDiv.style.left = newLeft + "px";
dragDiv.style.top = newTop + "px";
return false;
};
window.onmouseup = function(e) {
//結束移動
startMove = false;
dragDiv.style.cursor = "default";
return false;
};
e.stopPropagation();
});
</script>
</html>
代碼手敲可運行
當然這個實現還有很多可擴展或者不足,比如怎么限制div移動的范圍,能力有限,在以后的日子里會嘗試編寫js組件,實現更完善的功能。
頭條創作挑戰賽# HTMLStyleElement 對象是 Excel VBA 中用來操作 HTML 樣式的元素對象。它表示 HTML 頁面中的樣式標簽(<style>)。
下面是 6 個代碼實例,用來說明 HTMLStyleElement 對象的用法:
1、創建一個新的 <style> 元素對象,并將其添加到 HTML 文檔中:
Dim style As Object
Set style = ThisWorkbook.Sheets("Sheet1").OLEObjects.Add(ClassType:="HTML Style")
2、設置 <style> 元素的 innerHTML 屬性,即添加樣式規則:
style.Object.innerHTML = "body { background-color: lightblue; }"
3、獲取 <style> 元素的 innerHTML 屬性,即獲取樣式規則:
Dim styleRules As String
styleRules = style.Object.innerHTML
4、將 <style> 元素插入到 HTML 文檔中的某個節點之前:
Dim targetNode As Object
Set targetNode = ThisWorkbook.Sheets("Sheet1").OLEObjects("targetNode")
targetNode.ParentNode.insertBefore style.Object, targetNode
5、從 HTML 文檔中移除 <style> 元素:
style.Delete
6、通過指定 <style> 元素的 id 屬性,獲取具有特定 id 的 <style> 元素對象:
Dim styleId As String
styleId = "myStyle"
Dim style As Object
Set style = ThisWorkbook.Sheets("Sheet1").OLEObjects(styleId).Object
通過以上的代碼實例,你可以了解如何創建、設置、獲取、移除和查找 HTMLStyleElement 對象。注意,在上述代碼中,ThisWorkbook.Sheets("Sheet1") 可以根據你的具體情況進行修改,以指定你要操作的工作表。
當我感到壓力或需要放松時,我通常會使用 Excel 來記錄和跟蹤我的情緒和心理狀態。我創建了一個簡單的示例,使用 HTMLStyleElement 對象在 Excel 中應用不同的樣式主題來反映我的情緒。
Sub ApplyEmotionalStyle()
' 創建新的 HTMLStyleElement 對象
Dim style As Object
Set style = ThisWorkbook.Sheets("Sheet1").OLEObjects.Add(ClassType:="HTML Style")
' 根據情緒類型設置樣式規則
Dim mood As String
mood = InputBox("請輸入你的情緒類型(例如:happy、sad、excited、calm):")
Select Case mood
Case "happy"
style.Object.innerHTML = "body { background-color: yellow; }"
Case "sad"
style.Object.innerHTML = "body { background-color: blue; }"
Case "excited"
style.Object.innerHTML = "body { background-color: red; }"
Case "calm"
style.Object.innerHTML = "body { background-color: green; }"
Case Else
MsgBox "無效的情緒類型"
Exit Sub
End Select
' 將樣式應用到整個工作表
ThisWorkbook.Sheets("Sheet1").Cells.Font.Name = "Arial"
ThisWorkbook.Sheets("Sheet1").Cells.Font.Size = 14
ThisWorkbook.Sheets("Sheet1").Cells.Font.Color = RGB(255, 255, 255)
ThisWorkbook.Sheets("Sheet1").Cells.Interior.Pattern = xlNone
ThisWorkbook.Sheets("Sheet1").Cells.Interior.ColorIndex = xlColorIndexNone
ThisWorkbook.Sheets("Sheet1").Cells.Interior.ThemeColor = xlThemeColorLight1
' 將 <style> 元素插入 HTML 文檔中的某個節點之前
Dim targetNode As Object
Set targetNode = ThisWorkbook.Sheets("Sheet1").Range("A1")
targetNode.ParentNode.insertBefore style.Object, targetNode
' 彈出對話框展示情緒樣式的效果
MsgBox "樣式已應用到工作表中的單元格范圍"
End Sub
當運行上述代碼時,它會提示你輸入情緒類型,然后根據輸入的情緒類型應用相應的樣式。例如,如果輸入 “happy”,則會將工作表的背景顏色設置為黃色。你可以根據需要添加更多的情緒類型和對應的樣式規則。【請注意,該代碼假定在工作表 “Sheet1” 中有一個 Range 對象(A1) 作為目標節點,你也可以根據需要修改它】
在使用 HTMLStyleElement 對象時,需要注意以下幾點:
1、確保在添加 <style> 元素之前,要創建一個 OLEObject 控件并將其類型設置為 “HTML Style”,然后再通過 Object 屬性來訪問 HTMLStyleElement 對象。
Dim style As Object
Set style = ThisWorkbook.Sheets("Sheet1").OLEObjects.Add(ClassType:="HTML Style")
2、使用 .innerHTML 屬性來設置或獲取 <style> 元素的樣式規則。樣式規則必須遵循 CSS 的語法規則。
style.Object.innerHTML = "body { background-color: lightblue; }"
3、在插入 <style> 元素之前,需要找到要將其插入到的目標節點。通過 ParentNode 屬性和 insertBefore 方法來實現。
Dim targetNode As Object
Set targetNode = ThisWorkbook.Sheets("Sheet1").Range("A1")
targetNode.ParentNode.insertBefore style.Object, targetNode
4、如果不再需要 <style> 元素,可以使用 Delete 方法將其從文檔中刪除。
style.Delete
5、當在 Excel 中使用 HTMLStyleElement 對象時,要確保選擇適當的目標范圍(比如工作表、單元格等),以便樣式被正確應用。
6、需要遵循 CSS 的語法規則,確保正確設置樣式規則。例如,必須使用正確的 CSS 選擇器(如 “body”、“div”)和屬性(如 “background-color”、“font-size”)。
7、HTMLStyleElement 對象只能應用于支持 HTML 渲染的環境中,例如在 WebBrowser 控件中或使用 VBA 內置的 Web 瀏覽器。
8、在應用樣式之前,應該清楚目標節點的父節點是否支持樣式。例如,在 <style> 元素之前的節點必須是支持樣式的元素。
【通過遵循以上注意事項,你能夠正確地使用 HTMLStyleElement 對象來操作樣式規則并在 Excel 中應用相應的樣式。】
*請認真填寫需求信息,我們會在24小時內與您取得聯系。