9 lines
1.9 KiB
JavaScript
9 lines
1.9 KiB
JavaScript
|
/**
|
||
|
shave - Shave is a javascript plugin that truncates multi-line text within a html element based on set max height
|
||
|
@version v2.5.6
|
||
|
@link https://github.com/dollarshaveclub/shave#readme
|
||
|
@author Jeff Wainwright <yowainwright@gmail.com> (jeffry.in)
|
||
|
@license MIT
|
||
|
**/
|
||
|
!function (e) { "function" == typeof define && define.amd ? define(e) : e() }(function () { "use strict"; if ("undefined" != typeof window) { var e = window.$ || window.jQuery || window.Zepto; e && (e.fn.shave = function (e, t) { return function (e, t) { var n = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : {}; if (void 0 === t || isNaN(t)) throw Error("maxHeight is required"); var i = "string" == typeof e ? document.querySelectorAll(e) : e; if (i) { var a = n.character || "…", o = n.classname || "js-shave", r = "boolean" != typeof n.spaces || n.spaces, s = '<span class="js-shave-char">'.concat(a, "</span>"); "length" in i || (i = [i]); for (var c = 0; c < i.length; c += 1) { var h = i[c], d = h.style, l = h.querySelector(".".concat(o)), f = void 0 === h.textContent ? "innerText" : "textContent"; l && (h.removeChild(h.querySelector(".js-shave-char")), h[f] = h[f]); var v = h[f], g = r ? v.split(" ") : v; if (!(g.length < 2)) { var u = d.height; d.height = "auto"; var p = d.maxHeight; if (d.maxHeight = "none", h.offsetHeight <= t) d.height = u, d.maxHeight = p; else { for (var m = g.length - 1, y = 0, j = void 0; y < m;)j = y + m + 1 >> 1, h[f] = r ? g.slice(0, j).join(" ") : g.slice(0, j), h.insertAdjacentHTML("beforeend", s), h.offsetHeight > t ? m = j - 1 : y = j; h[f] = r ? g.slice(0, m).join(" ") : g.slice(0, m), h.insertAdjacentHTML("beforeend", s); var w = r ? " ".concat(g.slice(m).join(" ")) : g.slice(m), x = document.createTextNode(w), H = document.createElement("span"); H.classList.add(o), H.style.display = "none", H.appendChild(x), h.insertAdjacentElement("beforeend", H), d.height = u, d.maxHeight = p } } } } }(this, e, t), this }) } });
|