【问题标题】:Randomize Image On Refresh刷新时随机化图像
【发布时间】:2015-12-14 04:30:42
【问题描述】:

我需要一些关于我的应用程序的帮助

https://jsfiddle.net/fdatm462/1/

这是我目前正在做的事情

我也将 Jquery Mobile 用于我的框架,并且当我单击导航栏中的按钮时,我有一个刷新页面的功能

<li><button onclick="location.reload(true)" class ="ui-btn-b">New Cakes</button></li>

我也有不同的蛋糕图片,比如你在小提琴中看到的那个 我如何让它随机显示不同的图像并在页面刷新时采取不同的答案,我更担心如何让它首先随机显示

【问题讨论】:

    标签: javascript html jquery-ui jquery-mobile drag-and-drop


    【解决方案1】:

    首先,您需要一个可以从中选择的图像列表:

    imgArray = [ "img1.png", "img2.png", ... ];
    

    要从数组中选择一个随机元素,您可以使用Math.random() 选择一个从 0 到 1(不包括 1)的值,然后将其乘以数组的长度,得到从 0 到 n-1,最后发言将使它成为一个整数。

    var randomIndex = Math.floor(Math.random()*imgArray.length);
    randomImg = imgArray[randomIndex];
    

    然后您可以通过以下操作将src 更改为您的随机图像:

    $("#source li img").attr("src", randomImg );
    

    【讨论】:

    • 你能提供一个小提琴吗?
    • @artbarzz 当然,here is a general example。和here is an example with your code。您只有一个提供的图像,所以我使用了一些模板。
    • 哇斯宾塞非常感谢您的帮助,它的效果非常好,现在我只是想弄清楚如何制作它,所以可以说它显示 0 它进入了可放置选项的正确位置,我可以使用将随机图像变量存储到此处的索引吗? $('#answerfour').droppable({ accept: 'li[data-value="four"]', drop: function (event, ui) { $('#four').append(ui.draggable); } });
    猜你喜欢
    • 2023-04-02
    • 2018-10-05
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 2013-08-19
    相关资源
    最近更新 更多