Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

คุณสมบัติ HTML DOM offsetWidth


คุณสมบัติ HTML DOM offsetWidth ส่งคืนตัวเลขที่สอดคล้องกับความกว้างขององค์ประกอบ รวมถึงการเติม เส้นขอบ และแถบเลื่อน แต่ไม่ใช่ระยะขอบ

ต่อไปนี้เป็นไวยากรณ์ -

ส่งกลับค่าตัวเลข

HTMLelement.offsetWidth

ให้เราดูตัวอย่างของ HTML DOM offsetWidth ทรัพย์สิน −

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
<title>HTML DOM offsetHeight</title>
<style>
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="button"] {
      border-radius: 10px;
   }
   #containerDiv {
      margin: 0 auto;
      height: 100px;
      width: 100px;
      overflow: auto;
   }
</style>
</head>
<body>
   <form>
      <fieldset>
         <legend>HTML-DOM-offsetHeight</legend>
         <div id="containerDiv">
            <img id="image" src="https://www.tutorialspoint.com/hibernate/images/hibernate-mini-logo.jpg">
         </div>
         <input type="button" onclick="getHeight()" value="Get height of picture">
         <input type="button" onclick="fitHeight()" value="Remove Scrollbars">
         <div id="divDisplay">
         </div>
      </fieldset>
   </form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var imgSelect = document.getElementById("image");
   var containerDiv = document.getElementById("containerDiv");
   function getHeight() {
      divDisplay.innerHTML = 'Height of above picture with padding & border: '+imgSelect.offsetHeight;
      divDisplay.innerHTML += '<br>Height of container with padding & border: '+containerDiv.offsetHeight;
   }
   function fitHeight() {
      containerDiv.style.height = imgSelect.height+'px';
      containerDiv.style.width = imgSelect.width+'px';
      containerDiv.style.overflow = 'hidden';
      getHeight();
   }
</script>
</body>
</html>

ผลลัพธ์

ก่อนคลิกปุ่มใด ๆ −

คุณสมบัติ HTML DOM offsetWidth

หลังจากคลิก 'รับความกว้างของรูปภาพ' ปุ่ม −

คุณสมบัติ HTML DOM offsetWidth

หลังจากคลิก 'ลบแถบเลื่อน' ปุ่ม −

คุณสมบัติ HTML DOM offsetWidth