随机div,只显示其中的一部分(Random divs, display only part of them)

编程入门 行业动态 更新时间:2024-10-23 02:45:10
随机div,只显示其中的一部分(Random divs, display only part of them)

我在我的网站上使用此代码来随机化div的顺序。 在我的一个子页面中,我想在稍微修改的版本中使用它 - 将会有12个div以随机顺序再次显示,但只会显示前3个,其余的将设置为“display:none;” 或使用js的类似效果。

有任何建议如何修改以下代码,以便只出现3个第一个div?

这是代码: http : //jsfiddle.net/ca7WW/

HTML:

<div class="randomize"> <div class="random_div">1</div> <div class="random_div">2</div> <div class="random_div">3</div> <div class="random_div">4</div> <div class="random_div">5</div> <div class="random_div">6</div> <div class="random_div">7</div> <div class="random_div">8</div> <div class="random_div">9</div> <div class="random_div">10</div> <div class="random_div">11</div> <div class="random_div">12</div> </div>

jQuery的:

function shuffle(array) { var currentIndex = array.length , temporaryValue , randomIndex ; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } (function ($) { $.fn.randomize = function (childElem) { return this.each(function () { var $this = $(this); var elems = shuffle($(childElem)); $this.remove(childElem); for (var i = 0; i < elems.length; i++) $this.append(elems[i]); }); } })(jQuery) jQuery(function($){ $("div.randomize").randomize("div.random_div"); });

I use this code on my website to randomize the order of divs. On one of my subpages I want to use it in slightly modified version - there will be 12 divs displayed in random order again but only the first 3 will be displayed, the rest will be set to "display:none;" or similiar effect using js.

Any suggestions how to modify the following code, so that only 3 first divs will appear?

Here is the code: http://jsfiddle.net/ca7WW/

HTML:

<div class="randomize"> <div class="random_div">1</div> <div class="random_div">2</div> <div class="random_div">3</div> <div class="random_div">4</div> <div class="random_div">5</div> <div class="random_div">6</div> <div class="random_div">7</div> <div class="random_div">8</div> <div class="random_div">9</div> <div class="random_div">10</div> <div class="random_div">11</div> <div class="random_div">12</div> </div>

jQuery:

function shuffle(array) { var currentIndex = array.length , temporaryValue , randomIndex ; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } (function ($) { $.fn.randomize = function (childElem) { return this.each(function () { var $this = $(this); var elems = shuffle($(childElem)); $this.remove(childElem); for (var i = 0; i < elems.length; i++) $this.append(elems[i]); }); } })(jQuery) jQuery(function($){ $("div.randomize").randomize("div.random_div"); });

最满意答案

使用:lt和:gt来获取选择器中的“前N”或“后N”元素。

DEMO

jQuery(function($){ $("div.randomize").randomize("div.random_div"); $(".random_div:gt(2)").hide(); // This hides anything after the 3rd element });

Use :lt and :gt to get the "first N" or the "last N" elements in your selector.

DEMO

jQuery(function($){ $("div.randomize").randomize("div.random_div"); $(".random_div:gt(2)").hide(); // This hides anything after the 3rd element });

更多推荐

本文发布于:2023-07-24 00:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239214.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:只显示   Random   div   part   display

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!