I welcome you on the pages of our website. In today's article, we will talk about the ability to collapse (hide) the text on the site, and when clicking on the link to show the full text.
Very often to save space on the site, or just for a more presentable view, you only need to show the user a portion of the text, that is, minimize the expand text. I use this especially often when developing an online store in categories, that is, if you need to write a category collapse a large text and deploy it with a click, show a complete description, then this article is exactly what you need.
After reading this article, you will already know how to hide part of the text on the site and click on the link to show the full text. I think you also faced the problem when a lot of text should be hidden but at the same time give the user the opportunity to read more, this article will help you understand the main principle of how to hide the text in readmore.
The script hiding the big text, very useful function and sometimes even necessary, on this we today and we will consider the option of creating a script that will collapse the text if it exceeds the specified height in the settings. That is, if your text will have a block height greater than you set in the settings, then a link will appear when clicking on which the full text of the description will unfold, in case your text does not reach the specified altitude, this link will not appear.
And so, the script "hidden text under the spoiler" and an example of its work you can download and see below under this article, and in the article itself we will look at some of its functions and settings.
The function of the simplest script call looks like this:
1 |
$('article').readmore(); |
To call the script to collapse, expand the text with its additional settings as follows:
1 2 3 4 |
$('article').readmore({ speed: 75, maxHeight: 500 }); |
This script has additional settings that you can apply to it, consider them:
speed: 100 (The speed of the unfolding of the text is indicated in milliseconds)
maxHeight: 200 (The maximum block height is indicated in pixels)
heightMargin: 16 (specify in pixels, allows avoiding the breaking of blocks, if they are not much larger than the height - maxHeight)
moreLink: '<a href="#">Expand text</a>'
lessLink: '<a href="#">Collapse text</a>'
embedCSS: true (allows you to add dynamic CSS styles, if the styles you connect in your styles file set to false)
sectionCSS: 'display: block; width: 100%;' (This value sets the block style)
startOpen: false (by default the block is hidden, if true - the text will be immediately expanded, but it will be possible to hide it)
expandedClass: 'readmore-js-expanded' (class css which is added to the expanded block.)
collapsedClass: 'readmore-js-collapsed' (class css which is added to the collapsed block)
beforeToggle: function() {} ( function that is called after clicking the "Expand text" or "Minimize text" button, but before the block collapses or unfolds.)
afterToggle: function() {} ( function that is called after the block is expanded or collapsed.)
If the element has the maximum height specified in the css styles, in this case they will be used, the maxHeight value will be ignored.
Callback function:
Script callback functions, beforeToggle() ΠΈ afterToggle()Β have the same arguments: trigger, element ΠΈ more.
- trigger: “Expand text” or “Minimize text” buttons
- element: block that is currently being collapsed or unfolded.
- more: boolean, true – means that the block is unfolding.
Example callback function:
1 2 3 4 5 6 7 8 9 10 11 |
$('article').readmore({ afterToggle: function(trigger, element, more) { if(! more) { // "Collapse" button was pressed $('html, body').animate({ scrollTop: element.offset().top },{ duration: 100 }); } } }); |
The disabling of some functional of this script is used approximately like this:
1 |
$('article').readmore('destroy'); |
Or, specify the desired element with which the script should not work.
1 |
$('article:first').readmore('destroy'); |
This script by default adds its css styles to the page:
1 2 3 4 5 6 7 |
.readmore-js-toggle, .readmore-js-section { display: block; width: 100%; } .readmore-js-section { overflow: hidden; } |
Using the script settings, the first rule can be changed:
1 2 3 |
$('article').readmore({ sectionCSS: 'display: inline-block; width: 50%;' }); |
In order to use your styles in the css file in the settings you need to specify the value false:
1 2 3 |
$('article').readmore({ embedCSS: false }); |
To see a live example or download a script, you can visit our website:
No Comment
You can post first response comment.