SVGLineElement: y2 property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Thuộc tính chỉ đọc y2 của giao diện SVGLineElement mô tả giá trị tọa độ trục y của điểm cuối của đường dưới dạng SVGAnimatedLength. Nó phản ánh thuộc tính hình học y2 của phần tử <line>.

Giá trị thuộc tính là một <length>, <percentage>, hoặc <number>. Giá trị số của SVGAnimatedLength.baseVal là vị trí cuối đó dọc theo trục y trong đơn vị hệ tọa độ người dùng.

Giá trị

Một SVGAnimatedLength.

Ví dụ

Cho SVG sau:

html
<svg viewBox="0 0 300 200" xmlns="http://www.w3.org/2000/svg">
  <line x1="20" y1="30" x2="40" y2="50" stroke="blue" stroke-width="2" />
  <line x1="15%" y1="5%" x2="30%" y2="60%" stroke="red" stroke-width="4" />
</svg>

Chúng ta có thể truy cập các giá trị tính toán của thuộc tính y2:

js
const lines = document.querySelectorAll("line");
const y2Pos0 = lines[0].y2;
const y2Pos1 = lines[1].y2;
console.dir(y2Pos0.baseVal.value); // output: 50 (giá trị của `y2`)
console.dir(y2Pos1.baseVal.value); // output: 120 (60% của 200)

Thông số kỹ thuật

Specification
Scalable Vector Graphics (SVG) 2
# __svg__SVGLineElement__y2

Tương thích trình duyệt

Xem thêm