Tỷ lệ hình ảnh javascript khi cuộn

Có nhiều thuộc tính JavaScript cho phép chúng tôi đọc thông tin về chiều rộng, chiều cao của phần tử và các tính năng hình học khác

Chúng ta thường cần chúng khi di chuyển hoặc định vị các phần tử trong JavaScript

yếu tố mẫu

Là một phần tử mẫu để chứng minh các thuộc tính, chúng tôi sẽ sử dụng phần tử được cung cấp bên dưới

...Text...
#example { width: 300px; height: 200px; border: 25px solid #E8C48F; padding: 20px; overflow: auto; }

Nó có đường viền, phần đệm và cuộn. Bộ đầy đủ các tính năng. Không có lề vì chúng không phải là một phần của chính phần tử và không có thuộc tính đặc biệt nào cho chúng

Phần tử trông như thế này

Bạn có thể mở tài liệu trong sandbox

Nhớ thanh cuộn

Hình trên minh họa trường hợp phức tạp nhất khi phần tử có thanh cuộn. Một số trình duyệt [không phải tất cả] dành không gian cho nó bằng cách lấy nó từ nội dung [được gắn nhãn là “chiều rộng nội dung” ở trên]

Vì vậy, nếu không có thanh cuộn, chiều rộng nội dung sẽ là


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
7, nhưng nếu thanh cuộn rộng

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
8 [chiều rộng có thể khác nhau giữa các thiết bị và trình duyệt] thì chỉ còn lại

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
9 và chúng tôi nên tính đến điều đó. Đó là lý do tại sao các ví dụ từ chương này giả định rằng có một thanh cuộn. Không có nó, một số tính toán đơn giản hơn

Khu vực


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
0 có thể chứa đầy văn bản

Thông thường, phần đệm được hiển thị trống trên hình minh họa của chúng tôi, nhưng nếu có nhiều văn bản trong phần tử và nó tràn ra ngoài, thì trình duyệt sẽ hiển thị văn bản "tràn" ở mức


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
0, đó là điều bình thường

hình học

Đây là bức tranh tổng thể với các thuộc tính hình học

Giá trị của các thuộc tính này là các số kỹ thuật, nhưng những số này là "pixel", vì vậy đây là các phép đo pixel

Hãy bắt đầu khám phá các thuộc tính bắt đầu từ bên ngoài của phần tử

offsetParent, offsetLeft/Top

Những thuộc tính này hiếm khi cần thiết, nhưng chúng vẫn là những thuộc tính hình học “bên ngoài nhất”, vì vậy chúng ta sẽ bắt đầu với chúng


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
1 là tổ tiên gần nhất mà trình duyệt sử dụng để tính toán tọa độ trong quá trình kết xuất

Đó là tổ tiên gần nhất là một trong những điều sau đây

  1. Định vị CSS [
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    2 là
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    3,
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    4,
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    5 hoặc
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    6] hoặc
  2. 
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    7,
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    8, hoặc
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    9, hoặc
  3. 
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    00

Thuộc tính


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
01 cung cấp tọa độ x/y so với góc trên bên trái của

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
1

Trong ví dụ bên dưới,


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
03 bên trong có

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
04 là

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
1 và

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
01 dịch chuyển từ góc trên bên trái của nó [

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
07]


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180

Có một số trường hợp khi


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
1 là

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
09

  1. Đối với các yếu tố không được hiển thị [
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    10 hoặc không có trong tài liệu]
  2. Cho
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    00 và
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    12
  3. Đối với các phần tử có
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    13

offsetChiều rộng/Chiều cao

Bây giờ hãy chuyển sang phần tử chính nó

Hai thuộc tính này là những thuộc tính đơn giản nhất. Chúng cung cấp chiều rộng/chiều cao “bên ngoài” của phần tử. Hay nói cách khác, kích thước đầy đủ của nó bao gồm cả viền

Đối với phần tử mẫu của chúng tôi

  • 
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    14 – chiều rộng bên ngoài, có thể được tính bằng chiều rộng CSS bên trong [
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    7] cộng với phần đệm [
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    16] và đường viền [
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    17]
  • 
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    18 – chiều cao bên ngoài

Các thuộc tính hình học bằng 0/null đối với các phần tử không được hiển thị

Các thuộc tính hình học chỉ được tính cho các phần tử được hiển thị

Nếu một phần tử [hoặc bất kỳ tổ tiên nào của nó] có


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
10 hoặc không có trong tài liệu, thì tất cả các thuộc tính hình học bằng 0 [hoặc

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
09 cho

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
1]

Ví dụ:


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
1 là

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
09 và

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
44,

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
45 là

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
46 khi chúng tôi tạo một phần tử nhưng chưa chèn phần tử đó vào tài liệu hoặc phần tử đó [hoặc tổ tiên của phần tử đó] có

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
10

Chúng ta có thể sử dụng điều này để kiểm tra xem một phần tử có bị ẩn hay không, như thế này


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
0

Xin lưu ý rằng


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
48 như vậy trả về

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
49 cho các thành phần trên màn hình nhưng không có kích thước

clientTop/Left

Bên trong phần tử chúng ta có các đường viền

Để đo lường chúng, có các thuộc tính


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
10 và

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
11

trong ví dụ của chúng tôi

  • 
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    12 – chiều rộng đường viền bên trái
  • 
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    13 – chiều rộng đường viền trên cùng

…Nhưng chính xác hơn – những thuộc tính này không phải là chiều rộng/chiều cao của đường viền, mà là tọa độ tương đối của mặt trong so với mặt ngoài

Có gì khác biệt?

Nó hiển thị khi tài liệu ở chế độ từ phải sang trái [hệ điều hành bằng ngôn ngữ Ả Rập hoặc tiếng Do Thái]. Khi đó, thanh cuộn không ở bên phải mà ở bên trái, sau đó,


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
11 cũng bao gồm chiều rộng của thanh cuộn

Trong trường hợp đó,


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
11 sẽ không phải là

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
16, nhưng với chiều rộng của thanh cuộn là

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
17

Đây là ví dụ trong tiếng Do Thái

khách hàngChiều rộng/Chiều cao

Các thuộc tính này cung cấp kích thước của khu vực bên trong đường viền phần tử

Chúng bao gồm chiều rộng nội dung cùng với phần đệm, nhưng không có thanh cuộn

Ở hình trên, trước tiên hãy xem xét


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
18

Không có thanh cuộn ngang, vì vậy nó chính xác là tổng của những gì bên trong đường viền. Chiều cao CSS ________ 419 cộng với phần đệm trên và dưới [________ 216] tổng cộng _________ 571

Bây giờ


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
72 – ở đây chiều rộng nội dung không phải là

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
7, mà là

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
74, vì

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
8 bị thanh cuộn chiếm giữ. Vì vậy, tổng là ________ 574 cộng với phần đệm bên trái và bên phải, tổng số là _________ 577

Nếu không có phần đệm thì


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
78 chính xác là vùng nội dung, bên trong đường viền và thanh cuộn [nếu có]

Vì vậy, khi không có phần đệm, chúng tôi có thể sử dụng


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
79 để lấy kích thước vùng nội dung

cuộnChiều rộng/Chiều cao

Các thuộc tính này giống như


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
79, nhưng chúng cũng bao gồm các phần được cuộn ra [ẩn]

Trên hình trên

  • 
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    81 – là toàn bộ chiều cao bên trong của khu vực nội dung bao gồm các phần được cuộn ra
  • 
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    82 – là toàn bộ chiều rộng bên trong, ở đây chúng ta không có cuộn ngang nên nó bằng
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    72

Chúng ta có thể sử dụng các thuộc tính này để mở rộng phần tử theo chiều rộng/chiều cao đầy đủ của nó

Như thế này


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
1

Nhấp vào nút để mở rộng phần tử

văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản văn bản

yếu tố. Phong cách. chiều cao =


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
84

cuộnTrái/cuộnTop

Thuộc tính


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
85 là chiều rộng/chiều cao của phần ẩn, cuộn ra của phần tử

Trên hình bên dưới, chúng ta có thể thấy


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
86 và

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
87 cho một khối có cuộn dọc

Nói cách khác,


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
87 là “số lượng được cuộn lên”


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
85 có thể được sửa đổi

Hầu hết các thuộc tính hình học ở đây là chỉ đọc, nhưng có thể thay đổi


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
85 và trình duyệt sẽ cuộn phần tử

Nếu bạn nhấp vào phần tử bên dưới, mã


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
91 sẽ thực thi. Điều đó làm cho nội dung phần tử cuộn xuống

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
92

Nhấp vào
Tôi
1
2
3
4
5
6
7
8
9

Đặt


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
87 thành

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
46 hoặc một giá trị lớn, chẳng hạn như

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
95 sẽ làm cho phần tử cuộn lên trên cùng/dưới cùng tương ứng

Không lấy chiều rộng/chiều cao từ CSS

Chúng tôi vừa trình bày các thuộc tính hình học của các phần tử DOM, có thể được sử dụng để lấy chiều rộng, chiều cao và tính toán khoảng cách

Nhưng như chúng ta đã biết từ chương Kiểu và lớp, chúng ta có thể đọc chiều cao và chiều rộng CSS bằng cách sử dụng


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
96

Vậy tại sao không đọc chiều rộng của một phần tử với


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
96, như thế này?


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
4

Tại sao chúng ta nên sử dụng các thuộc tính hình học để thay thế?

  1. Đầu tiên, CSS

    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    98 phụ thuộc vào một thuộc tính khác.
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    99 xác định chiều rộng và chiều cao CSS “là gì”. Thay đổi trong
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    99 cho mục đích CSS có thể phá vỡ JavaScript đó

  2. Thứ hai, CSS

    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    98 có thể là
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    02, chẳng hạn đối với phần tử nội tuyến

    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    1

    Từ quan điểm CSS,

    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    03 là hoàn toàn bình thường, nhưng trong JavaScript, chúng tôi cần một kích thước chính xác trong
    
      
    ...
    alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
    04 mà chúng tôi có thể sử dụng trong tính toán. Vì vậy, ở đây chiều rộng CSS là vô dụng

Và còn một lý do nữa. một thanh cuộn. Đôi khi mã hoạt động tốt mà không có thanh cuộn lại gặp lỗi vì thanh cuộn lấy không gian từ nội dung trong một số trình duyệt. Vì vậy, chiều rộng thực có sẵn cho nội dung nhỏ hơn chiều rộng CSS. Và


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
79 tính đến điều đó

…Nhưng với


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
06 thì tình hình đã khác. Một số trình duyệt [e. g. Chrome] trả về chiều rộng bên trong thực, trừ đi thanh cuộn và một số trong số chúng [e. g. Firefox] – Chiều rộng CSS [bỏ qua thanh cuộn]. Sự khác biệt giữa các trình duyệt như vậy là lý do không sử dụng

  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
96 mà dựa vào các thuộc tính hình học

Nếu trình duyệt của bạn dành không gian cho thanh cuộn [hầu hết các trình duyệt dành cho Windows đều có], thì bạn có thể kiểm tra nó bên dưới

Phần tử có văn bản có CSS ​​


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
08

Trên Desktop Windows OS, Firefox, Chrome, Edge đều dành chỗ cho thanh cuộn. Nhưng Firefox hiển thị


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
7, trong khi Chrome và Edge hiển thị ít hơn. Đó là vì Firefox trả về chiều rộng CSS và các trình duyệt khác trả về chiều rộng "thực"

Xin lưu ý rằng sự khác biệt được mô tả chỉ là về việc đọc


  
...
alert[example.offsetParent.id]; // main alert[example.offsetLeft]; // 180 [note: a number, not a string "180px"] alert[example.offsetTop]; // 180
00 từ JavaScript, về mặt trực quan mọi thứ đều chính xác

Chủ Đề