jquery move elements into a random order

前端 未结 5 989
难免孤独
难免孤独 2020-12-01 11:23

I am attempting to display a series of images in a random order. However, I do not want any single item to repeat until all items have been shown, so instead of selecting a

5条回答
  •  情书的邮戳
    2020-12-01 12:14

    
    
    
    
    

    Click the button to sort the array.

    Explanation: normally you have "return (a-b)" yielding a positive number for ascending sort order; or you have "return (b-a)" yielding a negative number for descending sort order.

    Here we use Math.random()-0.5 which gives in half of the cases a positive number and in half of the cases a negative number. Thus sorting of pairs is either ascending or descending, yielding a random distribution of the array elements.

提交回复
热议问题