Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

วิธีตั้งค่าตำแหน่งซ้าย (Left Position) ขององค์ประกอบที่มี Position ด้วย JavaScript

ใช้คุณสมบัติ left เพื่อกำหนดตำแหน่งแนวนอนขององค์ประกอบที่มี position: absolute, relative, fixed หรือ sticky

ตัวอย่าง

<!DOCTYPE html>
<html>
  <head>
    <style>
      #myID {
        position: absolute;
      }
    </style>
  </head>
  <body>
    <h1>Heading 1</h1>
    <p>This is Demo Text.</p>
    <button type="button" id="myID" onclick="display()">Change Left Position</button>
    <script>
      function display() {
        document.getElementById("myID").style.left = "150px";
      }
    </script>
  </body>
</html>