[英]How fill IMG src with JavaScript?
我在PHP
具有此功能,該功能來自我正在工作的項目:
foreach ($reports["included_reports"] as $index => $report_name ) {
if (! in_array( $report_name, $reports["excluded_reports"])) {
$optional_reports .=
"<div class=\"large-12 columns\">
<div class=\"panel\">
<a href=\"./graph_all_periods.php?$graph_args&g=". $report_name ."&height=???&width=???\">
<img BORDER=0 style=\"padding:2px;\" $additional_cluster_img_html_args title=\"$cluster_url\" SRC=\"./graph.php?$graph_args&g=". $report_name ."&height=???&width=???\" >
</a>
</div>
</div>";
}
}
我想調整圖像大小以填充div,但是我不能使用width:100%
,因為我必須告知PHP width
和height
。 但是首先執行PHP,我也不知道用戶監視器的分辨率。 在Google中搜索時,我發現我只能通過JavaScipt知道用戶監視器的分辨率 。 所以我想知道是否有可能做一些我可以知道用戶監視器並告知img
標簽中的width
和heigh
參數的事情。
確保圖像始終填充div並將其設置為背景圖像並使用CSS background-size對其進行拉伸的最佳方法
在您的js中,對於用戶監視器,請嘗試window.screen.width
和window.screen.height
。 對於圖像尺寸,您可以在CSS中使用100%,然后通過img.clientWidth
在px中獲得js的高度和寬度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.