在开发中,我们经常遇到需要适配屏幕宽度的应用,比如图片自动缩放到适合尺寸。
<script> window.onload = function () { ReImgSize(); } </script> <script type="text/javascript" defer> function ReImgSize() { var clientWidth = document.body.clientWidth;// window.innerWidth; // //首页主体背景 var indexH = 747 * clientWidth / 640; $(".default img").css("width", clientWidth * changeTwoDecimal(600 / 640)); } function getElementsByClassName(classname) { var children = document.all; var elements = new Array(); for (var i = 0; i < children.length; i++) { var child = children[i]; var classNames = child.className.split(' '); for (var j = 0; j < classNames.length; j++) { if (classNames[j] == classname) { elements.push(child); break; } } } return elements; } function changeTwoDecimal(x) { var f_x = parseFloat(x); if (isNaN(f_x)) { alert('function:changeTwoDecimal->parameter error'); return false; } var f_x = Math.round(x * 100) / 100; return f_x; } </script>