คุณสมบัติ maxHeight สไตล์ DOM ส่งคืนและแก้ไขความสูงสูงสุดขององค์ประกอบในเอกสาร HTML
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
-
คืนค่า maxHeight
object.style.maxHeight
-
แก้ไขความสูงสูงสุด
object.style.maxHeight = “value”
คุณค่า
ในที่นี้ ค่าสามารถเป็น −
| ค่า | คำอธิบาย |
|---|---|
| ไม่มี | ไม่จำกัดความสูงขององค์ประกอบ |
| สืบทอด | รับค่าคุณสมบัตินี้จากองค์ประกอบหลัก |
| ค่าเริ่มต้น | ตั้งค่าคุณสมบัตินี้เป็นค่าเริ่มต้น |
| ความยาว | ตั้งค่าเป็นหน่วยความยาว |
| percentage(%) | ตั้งค่าเป็นเปอร์เซ็นต์ความสูงขององค์ประกอบหลัก |
ตัวอย่าง
ให้เรามาดูตัวอย่างคุณสมบัติสไตล์ maxHeight −
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #000;
background-image: linear-gradient(to top, #a18cd1 0%, #fbc2eb 100%);
height: 100vh;
}
p {
border: 2px solid #fff;
width: 200px;
overflow: auto;
}
.btn {
background: coral;
border: none;
height: 2rem;
border-radius: 2px;
width: 40%;
display: block;
color: #fff;
outline: none;
cursor: pointer;
margin-top: 4rem;
}
</style>
</head>
<body>
<h1>DOM Style maxHeight Property Example</h1>
<p>
This is paragraph 1 with some dummy text.
This is paragraph 1 with some dummy text.
This is paragraph 1 with some dummy text.
This is paragraph 1 with some dummy text.
This is paragraph 1 with some dummy text.
This is paragraph 1 with some dummy text.
</p>
<button onclick="add()" class="btn">Set maxHeight</button>
<script>
function add() {
document.querySelector('p').style.maxHeight = "100px";
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

คลิกที่ “ตั้งค่าความสูงสูงสุด ” เพื่อกำหนดความสูงสูงสุดขององค์ประกอบย่อหน้า
