A responsive text auto-expend plug-in by using jQuery/Bootstrap

The responsive auto-expend plug-in

As more and more users are coming from mobile or smart devices to your websites, it is becoming necessary to make your site mobile-friendly not only for SEO purpose but for even better user experience.

If your website has long paragraphs of text, it may add good experience if textual information is expanded by clicking “more” or some other related text while using mobile whereas the same information is presented in long paragraphs in bigger screens of desktop or laptops.

In this tutorial, a jQuery plug-in is used that you may deploy in WordPress or simple website where depending on the screen size the text information is presented completely or compacts in small devices. The plug-in is available to download from the Github website (credit: inaveedahmed).

A demo of using AutoExpend with Bootstrap framework

In this example, a responsive navigation bar is created by using the Bootstrap framework. This is followed by the long block of text paragraphs. In bigger desktop screen, the textual information is presented normally without the more button to expand it. If you are looking at it from the smart phone or smaller desktop screen, the paragraphs’ text will collapse. Click on “Show More” link to expand the text:

jQuery collpase text

See online demo and code

For setting up this plugin, you need to include the jQuery and plug-in’s JS files. You may either use the CDN link or host at your own:

    <script type=”text/javascript” src=”https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js”></script>

<script type=”text/javascript” src=”https://rawgit.com/inaveedahmed/narround/master/ina.autoexpand.js”></script>

The Bootstrap’s files are included as demos is using its classes for the navigation bar. You do not necessarily require it.

The next step is to imitate the plug-in in the jQuery code:

        $(document).ready(function(f){

            $('.content').collaps();

        });

 

The .content class is used in the main div containing textual information that you want to collapse / expand.

 

 <div class="row">

            <div class="col-lg-12 content">

                <p>The jQuery is a powerful JavaScript library. It has plenty of methods and plug-ins to expand the functionality of your website.</p>

                <span id="more-10"></span>

                <p>The jQuery is a powerful JavaScript library. It has plenty of methods and plug-ins to expand the functionality of your website.The jQuery is a powerful JavaScript library. </p>

A demo of displaying other text than “Show more”

You may want using some other text than the default “Show More”. For example, “More information, “More info”, “More” and so on.

In order to change this text to expand or collapse the information, you need to change this in the plug-in JS file.

For that, open the “ina.autoexpand.js” file and find this text: “Show More”. Simply replace this to the one you want to display. See the following demo where I changed “Show More” to “More Info”.

jQuery auto expand show-more

See online demo and code

You can see, the markup remains the same as used in the first example. Only, the reference to the JS file of plug-in is changed in the <head> section:

    <script type="text/javascript" src="js/autoexpand/ina.autoexpand.js"></script>

 

In the JS file, I changed the following:

var style ="."+settings.clousureClass+"{padding:10px;border-left:5px solid #ddd;margin:0 10px;display:none}."+settings.moreClass+"{display:block;font-size:100%;padding:10px;border-top:1px solid #ddd;border-bottom:1px solid #ddd;font-family:arial;color:#555;position:relative;cursor:pointer; margin:10px 0;}."+settings.moreClass+".active{border-bottom:0 solid #ddd}."+settings.moreClass+":before{content:'More Info'}."+settings.moreClass+":after{width:25px;height:25px;background-repeat:no-repeat;background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAACYxJREFUeNrMmnlM1dkVxy8/nuKugKCoo+KOGxi3mkoVxA20dUEik5iZ0LGTtOnUZBqbaZtpm5lM2th0pp0mTWNMJ52/1BYl7oqgCDYKyqIIlYKKuCuLioKK9nxuvOTN8/3exkPnJjcP3u/+7j3fs3zPufe+kHv37inXFhoaqh48eKDa2trUo0ePVEdHh+rZs6dqbGxUvXv3Vn369FFPnjxRvDt8+HA9/saNG6q9vT25tbV10uPHj9+SzwT5P0Lm6MGc8v6zXr16PQgLCyvr169fvfTaiIiIfPm+NSQkRN2/f18NGDBAORwOPTfr8inj1cCBA/WnZVkqPDxcy+PaHMrP9uLFCz0pncUvXrz4g5aWljW3bt36roAf++zZMz0O4RCqRw+NQz18+FC9fJb8/PlzDV7A3I6MjDwmgh6Jjo7+hwDpYH4A8OlPc/gDgMUHDRqE5sOqqqp+fPny5febmpomoiEsJRrXANw13gW8c5N5ouvq6jLknYz+/ft/fP369a+nTJnypQC7CWh57jOQEG+uJS6i0KBoTt25c0c1NDS8W11d/Wlzc/NwBDPC+6tB12bcSQA9njRp0u9iY2P/YL7H5VjHk2t5BCK+rsH07dtXiebjioqKtkgspAEAC3RV+FeEEY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goXLZsWYrUhe3OSv8GYDstYBkCSbanP4yPj/+1udV6HTHDGqyHDGPGjNmalpaWKOzXrjO4DZt6/eWDCSrZc6cUFBT8Tb4fBzF0h3XMIQVrS1JuEQV+IPv/f5pTUPMTEr9/VMPE0DBgJNByRRuT6+rqPq2pqdnMPp+sGox7RmcAaBzhpX8kCrvOOjx3d6Trs0VgBnMXggVwLTTCD2mqqqo+amhoWC/Pw17uCfx2OxTAGnQElWS8f/LkyZ/JZxHxiWzmzt/8FiUgi7jzW5KQLPRfCbx3a2trfyWLZUklkCr0+B0sBzXqIo7rMFnY+DRCI4C5kUJZjBEWqpCeGxUVtVVKjmoOQaBaTxc97prDXxcwFI3AoqVrUrh9IsJ9UllZOVUEnCNjponFEuTvKBG8t4wLfWnl59Lbpd8RwavFwiVixVIpLosRnjmxPkk5kET8fwEGAHgbMihsDKrVAAAAAElFTkSuQmCC);background-size:cover;position:absolute;right:16px;font-size:200%;top:50%;line-height:0;cursor:pointer;content:'';transition:all .3s ease-out;transform:translate(0,-12.5px)}."+settings.moreClass+".active:after{transform:rotate(-180deg) translate(0,12.5px)}@media screen and (min-width:767px){."+settings.moreClass+"{display:none}."+settings.clousureClass+"{padding:initial;border-left:0 solid #ddd;margin:initial;display:initial}}";

 

In that long code, you only need to change the “More Info” to one you want.

Changing other styles of “More” text

Similarly, you may change the style of “Show more” text as per need or to match the style of your website. For that, simply change or add the CSS properties for the “Show More” section as shown in the example below:

jQuery auto expand CSS

See online demo and code

You can see the light blue background color of “More info” content along with different borders for left and right. No markup is changed in this demo as well while JS file’s following section is modified:

var style ="."+settings.clousureClass+"{padding:15px;border-left:3px solid green;border-right:3px solid red;background-color:#D2E9FF;margin:0 10px;display:none}."+settings.moreClass+"{display:block;font-size:100%;padding:10px;border-top:1px solid #ddd;border-bottom:1px solid #ddd;font-family:arial;color:#555;position:relative;cursor:pointer; margin:10px 0;}."+settings.moreClass+".active{border-bottom:0 solid #ddd}."+settings.moreClass+":before{content:'More Info'}."+settings.moreClass+":after{width:25px;height:25px;background-repeat:no-repeat;background-image:url(}}";

        $("<style type='text/css'>"+ style +"</style>").appendTo("head");

 

Similarly, you may change the images for showing collapsed or expanded content.

Author - Abu Hassam

Abu Hassam is an experienced web developer. He graduated in Computer Science in 2000. Started as a software engineer and worked mostly on web-based projects.
Just like any great adventure, web development is about discovery and learning.
The web is a vast playground, and the best adventures are yet to come. Happy coding and exploring! ️