responsive design - Changing .text() depending on the window size with jQuery -
i've got simple function finds , shows first sentence of every paragraph:
function firstsentence(){ $('p').each(function(){ var p = $(this).text(); var s = p.split('.'); var f = s[0]; $(this).text(f + '...'); }); }
and use when window size less 600:
$(window).resize(function(){ if ($(window).width() < 600) { firstsentence(); } else { // normal please! } });
however, when user resizes window above 600, content not change back.
what best way content reverted, if user resizes larger window ( > 600
)?
before else, need store original text in each paragraph. e.g. this:
var originaltexts = $('p').map(function () { return $(this).text(); });
and then, "back normal code":
$('p').each(function (key) { $(this).text(originaltexts[key]); });
Comments
Post a Comment