IPB

Willkommen, Gast ( Anmelden | Registrierung )

 
Reply to this topicStart new topic
> [JS] Jquery Word wrapper
Wertung 4 V
jan
Beitrag Aug 31 2009, 11:54 AM
Beitrag #1


Administrator
***

Gruppe: Admin
Beiträge: 552
Mitglied seit: 1-June 05
Wohnort: germany, hassia, bickenbach
Mitglieds-Nr.: 1



Von: http://davidwalsh.name/word-wrap-mootools-php
Check out PHP Version: http://www.rt-designs.de/forums/PHP-Word-wrapper-t554.html


CODE
//class
var WordWrapper = new Class({
    
    Implements: [Options],
    
    options: {
        elements: 'h1',
        minWords: 3
    },
    
    initialize: function(options) {
        this.elements = $$(options.elements);
        this.elements.each(function(el) {
            this.apply(el);
        },this);
    },
    
    apply: function(element) {
        var original = element.get('html'), arr = original.split(' ');
        if(arr.length >= this.options.minWords) {
            //join the last and second to last
            arr[arr.length - 2] += ' ' + arr[arr.length - 1];
            arr.pop();
            element.set('html',arr.join(' '));
        }
    }    
});


The MooTools plugin offers just a few options: the elements to apply this too and the minimum number of words the element must have before the substitution takes place. The usage is just as easy as you’d expect:

CODE
//usage
window.addEvent('domready',function() {
    var ww = new WordWrapper({
        elements: 'h2'
    });
});

//sample results
//"This Is The First Attempt" becomes: This Is The First Attempt
//"Leave Me" is not long enough to modify
//"Que?" is not long enough to modify
//"I Am, I Am SuperMan, And I Can Do Anything" becomes: I Am, I Am SuperMan, And I Can Do Anything



--------------------
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 Besucher lesen dieses Thema (Gäste: 1 | Anonyme Besucher: 0)
0 Mitglieder:

 

- Vereinfachte Darstellung Aktuelles Datum: 23rd January 2020 - 19:23 PM