show more/Less text with just HTML and JavaScript

前端 未结 8 2122
心在旅途
心在旅途 2020-12-10 04:45

I am needing to create a show more/less text function, but with just JavaScript and HTML.. I can\'t use any additional libraries such as jQuery and it can\'t be done with CS

8条回答
  •  生来不讨喜
    2020-12-10 05:00

    This is my pure HTML & Javascript solution:

    var setHeight = function (element, height) {
        if (!element) {;
            return false;
        }
        else {
            var elementHeight = parseInt(window.getComputedStyle(element, null).height, 10),
            toggleButton = document.createElement('a'),
            text = document.createTextNode('...Show more'),
            parent = element.parentNode;
    
            toggleButton.src = '#';
            toggleButton.className = 'show-more';
            toggleButton.style.float = 'right';
            toggleButton.style.paddingRight = '15px';
            toggleButton.appendChild(text);
    
            parent.insertBefore(toggleButton, element.nextSibling);
    
            element.setAttribute('data-fullheight', elementHeight);
            element.style.height = height;
            return toggleButton;
        }
    }
    
    var toggleHeight = function (element, height) {
        if (!element) {
            return false;
        }
        else {
            var full = element.getAttribute('data-fullheight'),
            currentElementHeight = parseInt(element.style.height, 10);
    
            element.style.height = full == currentElementHeight ? height : full + 'px';
        }
    }
    
    var toggleText = function (element) {
        if (!element) {
            return false;
        }
        else {
            var text = element.firstChild.nodeValue;
            element.firstChild.nodeValue = text == '...Show more' ? '...Show less' : '...Show more';
        }
    }
    
    
    var applyToggle = function(elementHeight){
        'use strict';
        return function(){
            toggleHeight(this.previousElementSibling, elementHeight);
            toggleText(this);
        }
    }
    
    
    var modifyDomElements = function(className, elementHeight){
        var elements = document.getElementsByClassName(className);
        var toggleButtonsArray = [];
    
    
        for (var index = 0, arrayLength = elements.length; index < arrayLength; index++) {
            var currentElement = elements[index];
            var toggleButton = setHeight(currentElement, elementHeight);
            toggleButtonsArray.push(toggleButton);
        }
    
        for (var index=0, arrayLength=toggleButtonsArray.length; index

    You can then call modifyDomElements function to apply text shortening on all the elements that have shorten-text class name. For that you would need to specify the class name and the height that you would want your elements to be shortened to:

    modifyDomElements('shorten-text','50px'); 
    

    Lastly, in your your html, just set the class name on the element you would want your text to get shorten:

    Your long text goes here...

提交回复
热议问题