Created
April 23, 2014 12:56
-
-
Save Bartunek/11214224 to your computer and use it in GitHub Desktop.
Simple read more jQuery plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Read more plugin | |
* Can be anywhere in js, after jQuery is loaded | |
*/ | |
(function($){ | |
$.fn.read_more = function (opts) { | |
opts = opts || {}; | |
var textContent = opts.text || 'Číst více', | |
hideContent = opts.text || 'Skrýt', | |
linkClass = opts.class || 'read-more-link'; | |
return this.each(function(index, el) { | |
var $el = $(el), | |
$parent = $el.parent(), | |
$link = $('<a href="#read-more">' + textContent + '</a>'); | |
textContent = $el.data('text') ? $el.data('text') : textContent; | |
hideContent = $el.data('hide-text') ? $el.data('hide-text') : hideContent; | |
$link.addClass(linkClass); | |
$el.hide().addClass('is-hidden'); | |
$parent.append($link); | |
$link.on('click', function(event) { | |
event.preventDefault(); | |
if ($el.hasClass('is-hidden')) { | |
$el .show() | |
.removeClass('is-hidden'); | |
$link.text(hideContent); | |
} else { | |
$el .hide() | |
.addClass('is-hidden'); | |
$link.text(textContent); | |
} | |
}); | |
}); | |
}; | |
})(jQuery); | |
/** | |
* Initialization should be inside document ready handler | |
* and should look as follows: | |
*/ | |
$(document).ready(function() { | |
// With default settings | |
$('.js-readmore').read_more(); | |
// Or with custom texts: | |
$('.js-readmore').read_more({ | |
textContent: 'Read more', | |
hideContent: 'Hide', | |
linkClass: 'hidden-content-link' | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment