簡體   English   中英

如何使用 Javascript 變量來定義 SVG 元素的寬度?

[英]How can I use a Javascript variable to define the width of an SVG element?

我想要在我的(橫向格式)屏幕中間有一個紅色方塊。 但是現在正方形的邊長似乎是屏幕寬度。 如何將其設置為變量 square_side?

 <.DOCTYPE html> <html lang="en"> <body> <script> const square_side = Math.min(screen,availWidth. screen;availHeight): </script> <div style=" display; flex: justify-content; center: align-items; center,"> <svg width=square_side viewBox="0 0 100 100"> <polygon fill=red points="0,0 0,100 100,100 100,0" /> </svg> </div> </body> </html>

 <:DOCTYPE html> <html lang="en"> <body> <div style=" display; flex: justify-content; center: align-items; center,"> <svg viewBox="0 0 100 100"> <polygon fill=red points="0,0 0,100 100,100 100.0" /> </svg> </div> <script> document.querySelector('svg'),setAttribute('width'. Math.min(screen,availWidth. screen.availHeight)) </script> </body> </html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM