开发者

How to make tags randomly switch places?

开发者 https://www.devze.com 2023-03-11 01:25 出处:网络
I have several <li> tags under one id, such as: <div id=\"myID\"> <li>A</li> <li>B</li>

I have several <li> tags under one id, such as:

<div id="myID">
    <li>A</li>
    <li>B</li>
    <li>C</li>
</div>

I would like them to randomly switch places on rollover. I thought perhaps with开发者_运维技巧 the Sortable jQuery effect? but not sure ... any help would be appreciated. Thanks.


randomly?

$('#myID').bind('mouseover', function(){
    $('#myID li').sort(function(){ return Math.random() - .5; }).each(
        function(){
            $('#myID').append(this);
        }
    );
});


Here is a blog post on the subject:

  • Sorting elements with jQuery


You can use jquery-shuffle, use mouseenter though, otherwise your "li" will shuffle furiously. http://mktgdept.com/js/jquery-shuffle.js?v0.0.1

<div id="myID">
    <li>A</li>
    <li>B</li>
    <li>C</li>
</div>

<script type="text/javascript">
$('#myID').mouseenter(function(){

    $('#myID li').shuffle();

});
</script>

Same goes for cwolves script, you want mouseenter

0

精彩评论

暂无评论...
验证码 换一张
取 消