Skip to main content
Code Review

Return to Question

edited tags; edited title
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Responsive text by JavaScript (plugin)

added 110 characters in body; edited title
Source Link

Responsive text by JavaScript (plugin)

I'm working on a JavaScript plugin that makes font-sizes flexible (for a responsive layout). The code works well so far, but I'm sure that it's capable of improvement (especially if you look at the global variable).

(function () {
 var extend = function (obj, ext) {
 for (var key in ext) {
 if (ext.hasOwnProperty(key)) {
 obj[key] = ext[key];
 }
 }
 return obj;
 };
 window.txt = function (el, k, c) {
 var s = extend({
 n: Number.NEGATIVE_INFINITY,
 x: Number.POSITIVE_INFINITY
 }, c);
 var fit = function () {
 var a = k;
 var i = function () {
 el.style.fontSize = Math.max(Math.min(el.clientWidth / a, parseFloat(s.x)), parseFloat(s.n)) + "px";
 };
 i();
 window.addEventListener("resize", i);
 window.addEventListener("orientationchange", i);
 };
 if (el.length) {
 for (var i = 0; i < el.length; i++) {
 fit(el[i]);
 }
 } else {
 fit(el);
 }
 return el;
 };
})();

DEMO

Meaning of the used variables:

  • n = minFontSize
  • x = maxFontSize
  • s = settings
  • c = config
  • el = element
  • i → running the function
  • a or k = fontRatio/compressor

window.txt(document.getElementById("title"), 10);

executes the JavaScript Plugin on an element.

Responsive text by JavaScript

I'm working on a JavaScript plugin that makes font-sizes flexible (for a responsive layout). The code works well so far, but I'm sure that it's capable of improvement (especially if you look at the global variable).

(function () {
 var extend = function (obj, ext) {
 for (var key in ext) {
 if (ext.hasOwnProperty(key)) {
 obj[key] = ext[key];
 }
 }
 return obj;
 };
 window.txt = function (el, k, c) {
 var s = extend({
 n: Number.NEGATIVE_INFINITY,
 x: Number.POSITIVE_INFINITY
 }, c);
 var fit = function () {
 var a = k;
 var i = function () {
 el.style.fontSize = Math.max(Math.min(el.clientWidth / a, parseFloat(s.x)), parseFloat(s.n)) + "px";
 };
 i();
 window.addEventListener("resize", i);
 window.addEventListener("orientationchange", i);
 };
 if (el.length) {
 for (var i = 0; i < el.length; i++) {
 fit(el[i]);
 }
 } else {
 fit(el);
 }
 return el;
 };
})();

DEMO

Meaning of the used variables:

  • n = minFontSize
  • x = maxFontSize
  • s = settings
  • c = config
  • el = element
  • i → running the function
  • a or k = fontRatio/compressor

Responsive text by JavaScript (plugin)

I'm working on a JavaScript plugin that makes font-sizes flexible (for a responsive layout). The code works well so far, but I'm sure that it's capable of improvement (especially if you look at the global variable).

(function () {
 var extend = function (obj, ext) {
 for (var key in ext) {
 if (ext.hasOwnProperty(key)) {
 obj[key] = ext[key];
 }
 }
 return obj;
 };
 window.txt = function (el, k, c) {
 var s = extend({
 n: Number.NEGATIVE_INFINITY,
 x: Number.POSITIVE_INFINITY
 }, c);
 var fit = function () {
 var a = k;
 var i = function () {
 el.style.fontSize = Math.max(Math.min(el.clientWidth / a, parseFloat(s.x)), parseFloat(s.n)) + "px";
 };
 i();
 window.addEventListener("resize", i);
 window.addEventListener("orientationchange", i);
 };
 if (el.length) {
 for (var i = 0; i < el.length; i++) {
 fit(el[i]);
 }
 } else {
 fit(el);
 }
 return el;
 };
})();

DEMO

Meaning of the used variables:

  • n = minFontSize
  • x = maxFontSize
  • s = settings
  • c = config
  • el = element
  • i → running the function
  • a or k = fontRatio/compressor

window.txt(document.getElementById("title"), 10);

executes the JavaScript Plugin on an element.

Tweeted twitter.com/#!/StackCodeReview/status/574180448265441280
edited body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Responsive text by JavascriptJavaScript

I'm working on a javascriptJavaScript plugin that makes font-sizes flexible (for a responsive layout). The code works well so far, but I'm sure that it's capable of improvement (especially if you look at the global variable).

(function () {
 var extend = function (obj, ext) {
 for (var key in ext) {
 if (ext.hasOwnProperty(key)) {
 obj[key] = ext[key];
 }
 }
 return obj;
 };
 window.txt = function (el, k, c) {
 var s = extend({
 n: Number.NEGATIVE_INFINITY,
 x: Number.POSITIVE_INFINITY
 }, c);
 var fit = function () {
 var a = k;
 var i = function () {
 el.style.fontSize = Math.max(Math.min(el.clientWidth / a, parseFloat(s.x)), parseFloat(s.n)) + "px";
 };
 i();
 window.addEventListener("resize", i);
 window.addEventListener("orientationchange", i);
 };
 if (el.length) {
 for (var i = 0; i < el.length; i++) {
 fit(el[i]);
 }
 } else {
 fit(el);
 }
 return el;
 };
})();

DEMO

Meaning of the used variables:

  • n = minFontSize
  • x = maxFontSize
  • s = settings
  • c = config
  • el = element
  • i → running the function
  • a or k = fontRatio/compressor

Responsive text by Javascript

I'm working on a javascript plugin that makes font-sizes flexible (for a responsive layout). The code works well so far, but I'm sure that it's capable of improvement (especially if you look at the global variable).

(function () {
 var extend = function (obj, ext) {
 for (var key in ext) {
 if (ext.hasOwnProperty(key)) {
 obj[key] = ext[key];
 }
 }
 return obj;
 };
 window.txt = function (el, k, c) {
 var s = extend({
 n: Number.NEGATIVE_INFINITY,
 x: Number.POSITIVE_INFINITY
 }, c);
 var fit = function () {
 var a = k;
 var i = function () {
 el.style.fontSize = Math.max(Math.min(el.clientWidth / a, parseFloat(s.x)), parseFloat(s.n)) + "px";
 };
 i();
 window.addEventListener("resize", i);
 window.addEventListener("orientationchange", i);
 };
 if (el.length) {
 for (var i = 0; i < el.length; i++) {
 fit(el[i]);
 }
 } else {
 fit(el);
 }
 return el;
 };
})();

DEMO

Meaning of the used variables:

  • n = minFontSize
  • x = maxFontSize
  • s = settings
  • c = config
  • el = element
  • i → running the function
  • a or k = fontRatio/compressor

Responsive text by JavaScript

I'm working on a JavaScript plugin that makes font-sizes flexible (for a responsive layout). The code works well so far, but I'm sure that it's capable of improvement (especially if you look at the global variable).

(function () {
 var extend = function (obj, ext) {
 for (var key in ext) {
 if (ext.hasOwnProperty(key)) {
 obj[key] = ext[key];
 }
 }
 return obj;
 };
 window.txt = function (el, k, c) {
 var s = extend({
 n: Number.NEGATIVE_INFINITY,
 x: Number.POSITIVE_INFINITY
 }, c);
 var fit = function () {
 var a = k;
 var i = function () {
 el.style.fontSize = Math.max(Math.min(el.clientWidth / a, parseFloat(s.x)), parseFloat(s.n)) + "px";
 };
 i();
 window.addEventListener("resize", i);
 window.addEventListener("orientationchange", i);
 };
 if (el.length) {
 for (var i = 0; i < el.length; i++) {
 fit(el[i]);
 }
 } else {
 fit(el);
 }
 return el;
 };
})();

DEMO

Meaning of the used variables:

  • n = minFontSize
  • x = maxFontSize
  • s = settings
  • c = config
  • el = element
  • i → running the function
  • a or k = fontRatio/compressor
deleted 15 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
added 212 characters in body
Source Link
Loading
edited body
Source Link
Loading
Rollback to Revision 2
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
Loading
added 97 characters in body; added 16 characters in body
Source Link
Loading
added 37 characters in body
Source Link
Loading
Source Link
Loading
default

AltStyle によって変換されたページ (->オリジナル) /