具有随机位置的数组

发布于 2024-12-10 16:52:47 字数 748 浏览 2 评论 0原文

我对随机位置有疑问。我制作了一个在页面上随机设置

  • 的脚本。您可以在这里看到它:单击此处
  • 但问题是。项目重叠。我想用数组制作这个脚本。我想要一个具有固定位置的数组。始终有 8 项。而这八件物品,都有一个固定的位置。

    我怎样才能做到这一点?如何制作具有固定位置的数组?

    这是我的代码:

    var images = [];
    
    function init() {
        $('.friend-selection li > div').each(function(){
    
            var id = this.id;
            var img = $('#img_' + id);
            var randomTop = 400*Math.random(); //random top position
            var randomLeft = 500*Math.random()+1; //random left position
    
            $("#parent_" + id).css({ //apply the position to parent divs
                top     : randomTop,
                left    : randomLeft
            });
        });
    };
    
    init(); 
    

    I have a problem with random positions. I make a script that set <li> random on the page. You can see it here: Click here

    But the problem is. That the items overlap. I want make this script with a array. I would like an array with fixed positions. There are always 8 items. And these eight items, all have one fixed position.

    How can I make this? How can I make a array with fix positions?

    Here my code:

    var images = [];
    
    function init() {
        $('.friend-selection li > div').each(function(){
    
            var id = this.id;
            var img = $('#img_' + id);
            var randomTop = 400*Math.random(); //random top position
            var randomLeft = 500*Math.random()+1; //random left position
    
            $("#parent_" + id).css({ //apply the position to parent divs
                top     : randomTop,
                left    : randomLeft
            });
        });
    };
    
    init(); 
    

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(2

    浊酒尽余欢 2024-12-17 16:52:47

    我假设您有一组 8 个固定的、不重叠的位置,您想随机且唯一地使用它们:

    var images = [];
    
    // Constructor for the "Position" structure
    function Position(left, top) {
        this.left=left;
        this.top=top;
    }
    
    // sortFunction routine to help randomize array
    function rand(ar){
        return 0.5-Math.random();
    }
    
    // Array containing the 8 positions you want to use
    var positionArray = [
          new Position(0,  0) 
        , new Position(50, 50) 
        , new Position(100,100) 
        , new Position(150,150) 
        , new Position(200,200) 
        , new Position(250,250) 
        , new Position(300,300) 
        , new Position(350,350) 
    ];
    
    function init() {
        $('.friend-selection li > div').each(function(){
    
            var id = this.id;
            var img = $('#img_' + id);
            var imageIndex = parseInt(id.substring(id.length - 1)); // This is a hack because you're using "picture*" as the id
    
            $("#parent_" + id).css({ //apply the position to parent divs
                top     : positionArray[imageIndex].top,
                left    : positionArray[imageIndex].left
            });
        });
    };
    
    
    // Randomize array - http://stackoverflow.com/questions/7802661
    positionArray.sort(rand);
    
    init(); 
    

    I'm assuming you have a set of 8 fixed, non-overlapping positions that you'd like to use randomly and uniquely:

    var images = [];
    
    // Constructor for the "Position" structure
    function Position(left, top) {
        this.left=left;
        this.top=top;
    }
    
    // sortFunction routine to help randomize array
    function rand(ar){
        return 0.5-Math.random();
    }
    
    // Array containing the 8 positions you want to use
    var positionArray = [
          new Position(0,  0) 
        , new Position(50, 50) 
        , new Position(100,100) 
        , new Position(150,150) 
        , new Position(200,200) 
        , new Position(250,250) 
        , new Position(300,300) 
        , new Position(350,350) 
    ];
    
    function init() {
        $('.friend-selection li > div').each(function(){
    
            var id = this.id;
            var img = $('#img_' + id);
            var imageIndex = parseInt(id.substring(id.length - 1)); // This is a hack because you're using "picture*" as the id
    
            $("#parent_" + id).css({ //apply the position to parent divs
                top     : positionArray[imageIndex].top,
                left    : positionArray[imageIndex].left
            });
        });
    };
    
    
    // Randomize array - http://stackoverflow.com/questions/7802661
    positionArray.sort(rand);
    
    init(); 
    
    罗罗贝儿 2024-12-17 16:52:47

    将项目按顺序放入数组中,这样就不会覆盖已填充的位置,并使用 Shuffle 以随机顺序对数组进行打乱。

    但由于 Javascript 中没有这样的函数,你必须自己编写一个。像这样的东西会起作用。

    shuffle = function(o){
        for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
        return o;
    };
    
    
    alert(shuffle([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));
    

    http://jsfiddle.net/uxnn7/

    Put the items in sequence in the array so that you cannot overwrite the already filled positions and use Shuffle to shuffle the array in a random order.

    But as there is no such function in Javascript you will have write one on your own. Something like this would work.

    shuffle = function(o){
        for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
        return o;
    };
    
    
    alert(shuffle([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));
    

    http://jsfiddle.net/uxnn7/

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文