I have a jquery script that i want to convert in javascript.
Jquery:
<script type="text/javascript">
$(document).ready( function() {
addCssTransform();
$( window ).resize(function() {
addCssTransform();
});
function addCssTransform() {
var docWid = $(document).width();
var w = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth);
var actualWidth = w - (w - docWid);
var zoom = actualWidth / 1280;
var zoomRatio = zoom *100;
console.log(zoomRatio);
if(actualWidth > 1280) {
$('html').css( 'font-size', + zoomRatio+ '%' ); /* IE 9 */
}
}
});
</script>
I have tried and here is the output. But it is not working and giving error in console.
javascript:
<script type="text/javascript">
addCssTransform();
window.addEventListener('resize', function(event){
addCssTransform();
});
function addCssTransform() {
var docWid = document.body.clientWidth;
var w = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth);
var actualWidth = w - (w - docWid);
var zoom = actualWidth / 1280;
var zoomRatio = zoom *100;
console.log(zoomRatio);
if(actualWidth > 1280) {
document.getElementsByTagName("html").style.fontSize = zoomRatio + '%';
//$('html').css( 'font-size', + zoomRatio+ '%' ); /* IE 9 */
}
};
</script>
It seems there is an error in line:
document.getElementsByTagName("html").style.fontSize = zoomRatio + '%';
getElementsByTagNamereturns a nodelist? I think it is. Do as mplungjan suggests.document.getElementsByTagName("html")can be written asdocument.documentElement