2017-09-20 11 views
0

ブラウザウィンドウのサイズが変更され、うまく機能していると思われるコードがいくつか見つかりました。ここでJavascriptボディからdiv /要素へのフォントのスケーリング

コードです:

document.body.setScaledFont = function (f) { 
     var s = this.offsetWidth, 
      fs = s * f; 
     this.style.fontSize = fs + '%'; 
     return this 
    }; 

    document.body.setScaledFont(0.35); 
    window.onresize = function() { 
     document.body.setScaledFont(0.35); 
    } 

私の質問は:

指定されたdiv要素が代わりにスケーリングされたときにテキストがスケールするので、どのように私はこのコードを変更することができますか?

@Deanoの推奨回答コードが更新されました。このコードは、ブラウザのサイズが変更された場合にのみスケーリングされます。

<!doctype html> 

<html lang="en"> 
<head> 
    <meta charset="utf-8"> 

    <title></title> 

    <style type="text/css"> 



    #window { 
     border:1px dashed #ccc; 
     overflow: hidden; 
     resize: both; 
     text-align: center; 
    } 

    </style> 

</head> 

<body> 

<div id="window">Scale the window</div> 




    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
    <script> 

    $(document).ready(function() { 

    $('#window').mouseup(function() { 

     document.getElementById('window').setScaledFont = function (f) { 
      var s = this.offsetWidth, 
      fs = s * f; 
      this.style.fontSize = fs + '%'; 
      return this 
     }; 

      document.getElementById('window').setScaledFont(0.35); 
      window.onresize = function() { 
      document.getElementById('window').setScaledFont(0.35); 
     } 

     }); 

    }); 



    </script> 

</body> 
</html> 
+0

のようなlibaryを使用する必要がありますどのようなあなたはこれまでに試してみましたか?あなたのコードを示してください。 – Soviut

答えて

1

document.bodyあなたが特定の要素をターゲットにする場合Idまたはclassを使用し、体全体を対象としています。質問の要件が変更されたのでJSfiddle

更新

document.getElementById('window').setScaledFont = function (f) { 
 
    var s = this.offsetWidth, 
 
     fs = s * f; 
 
    this.style.fontSize = fs + '%'; 
 
    return this 
 
    }; 
 

 
    document.getElementById('window').setScaledFont(0.35); 
 
    window.onresize = function() { 
 
    document.getElementById('window').setScaledFont(0.35); 
 
    }
div { 
 
    font-size:2em; 
 
}
<div id="window">Scale the window</div>

: -/

あなたはmimetic.js DEMO

+0

divのサイズが変更されたとき、私はそれを拡大縮小することができませんでした。 – max2020

+0

私はJSfiddleリンクを追加しました:) – Deano

+0

はい私は見ましたが、divのサイズを変更しませんでした...私の編集した質問の完全なコードを参照してください私は何を意味知っている – max2020

関連する問題