0

我一直在尝试从数组中预定义的位置列表中随机化 div 的位置,但没有成功,我想我可能很接近,但我看不出我哪里出错了。

变量位置数组 = [];

    function Position(left, top) {
        this.left=left;
        this.top=top;
    }

    function rand(ar){
        return 0.5-Math.random();
    }

    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() {                
            var min = 0;
            var max = 7;
            var posIndex = Math.floor(Math.random() * (max - min + 1)) + min;
            alert(posIndex)                               
            $(".start-journey-marker").css({
                top     : positionArray[posIndex].top,
                left    : positionArray[posIndex].left
            });
    };

    positionArray.sort(rand);

    init(); 

看这里的例子:http: //jsfiddle.net/qQWYW/

你能帮忙吗?

4

3 回答 3

1

要使 CSS生效,您需要向top元素添加或。(编辑:或添加。)leftposition : relativeposition : absoluteposition : fixed

此外,您的演示也不起作用,因为您没有包含 jQuery(左侧的框架面板)。

更新演示:http: //jsfiddle.net/qQWYW/1/

于 2013-03-07T10:49:52.747 回答
0

这是一个造型问题。只需添加

position: absolute;

到您的.start-journey-marker选择器。

PS 它不会在小提琴上工作,因为你忘记包含 jQuery。

于 2013-03-07T10:49:50.593 回答
0

.start-journey-marker在您的 CSS 中添加位置属性

于 2013-03-07T10:51:06.903 回答