Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Randomization #4

Open
ChristopherWerby opened this issue Aug 29, 2015 · 1 comment
Open

Feature Request: Randomization #4

ChristopherWerby opened this issue Aug 29, 2015 · 1 comment

Comments

@ChristopherWerby
Copy link

It would be great to add a random option (default false).

I found a likely jQuery randomization function on Stack Overflow
ref: http://stackoverflow.com/questions/14555415/how-to-randomly-sort-list-items

$.fn.randomize = function(selector){
    var $elems = selector ? $(this).find(selector) : $(this).children(),
        $parents = $elems.parent();

    $parents.each(function(){
        $(this).children(selector).sort(function(){
            return Math.round(Math.random()) - 0.5;
        }).detach().appendTo(this);
    });

    return this;
};

Thanks for the excellent plugin!

@ChristopherWerby
Copy link
Author

In case it is of use to others, I added randomization by manipulating the DOM prior to calling NewsBox from the in-page script. I pulled out the relevant list items, randomized them, and put them back and then called NewsBox. This was a lot easier than hacking the NewsBox plugin to add randomization.

My particular implementation, with code by Yair Even Or (ref), looks like this:

        <script type="text/javascript">
            $(
                function () 
                {
                    //Randomize comments before bootstrap News
                    var ul = document.querySelector("ul.comments")

                    //clone the list
                    temp = ul.cloneNode(true); 

                    //shuffle the cloned list for better performance
                    for (var i = temp.children.length + 1; i--; )
                        temp.appendChild( temp.children[Math.random() * i |0] );

                    //copy shuffled list back to 'ul'
                    ul.parentNode.replaceChild(temp, ul); 

                    $("ul.comments").bootstrapNews
                    (
                        {
                            newsPerPage:            3
                            ,autoplay:              true
                            ,pauseOnHover:          true
                            ,navigation:            true
                            ,direction:             'down'
                            ,newsTickerInterval:    2500
                            ,onToDo: function () 
                            {
                                //console.log(this);
                            }
                        }
                    );
                }
            );
        </script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant