简体   繁体   English

如何在jquery中随机化和模拟翻转?

[英]How to randomise and mimic a rollover in jquery?

I have a JQuery script which acts as a simple image rollover with a nice fade effect. 我有一个JQuery脚本,它充当简单的图像翻转并具有良好的淡入淡出效果。

Here is a test version of the current script in action - http://fi-testing.co.uk/SO/rubix-cube.html 这是当前正在运行的脚本的测试版本-http://fi-testing.co.uk/SO/rubix-cube.html

As you can see, there are 9 blocks, the client wishes for the rollovers to occur randomly (without a hover) to kind of create a ripple/pulsating effect. 如您所见,有9个区块,客户希望轮流随机发生(无悬停)以产生波动/脉动效果。

How would this be achieved with either JQuery or php?, and would It be possible that hovering over breaks the randomisation and acts as normal? 如何使用JQuery或php实现此目标?将鼠标悬停会破坏随机性并像正常情况那样吗?

Sorry if any of this is unclear. 抱歉,如果不清楚。

Thanks for any help. 谢谢你的帮助。

Dan

Without going too much into your code, you can create randomness by Math.random() 无需过多编写代码,就可以通过Math.random()创建随机性

so if you have an array of your cube entires, indexed 0 - 8, you may use 因此,如果您有一个多维数据集整体的数组(索引为0-8),则可以使用

var randomNumber = parseInt( Math.random() * 9 );
var randomCube = cubes[randomNumber];

you can use setInterval to get this to repeat once in every x ms 您可以使用setInterval使其每隔x ms重复一次

function randomlyChangeCubes() { ... }

...

setInterval( randomlyChangeCubes, 2000 );

you can use jQuery trigger to invoke the hover effect manually, but I'd say it'd be more readable to extract the code you have in hover to a function which you call both from hover and from randomlyChangeCubes . 你可以使用jQuery trigger调用hover手工效果,但我会说这将会是更具可读性提取你有代码hover给您无论从调用函数hoverrandomlyChangeCubes

All that being said... doing this entirely randomly probably won't make it look rippling/pulsating... 话虽如此...完全随机地执行此操作可能不会使其看起来像波纹/脉动...

Things you would need: 您需要做的事情:

setTimeout/setInterval = call you 'effect' function at a set time random number = use this to call the required element randomly eg #box1, #box2 etc. setTimeout / setInterval =在设置的时间调用“效果”函数随机数=使用此函数随机调用所需的元素,例如#box1,#box2等。

I think thats it - should be fairly straightforward. 我认为就是这样-应该相当简单。

It can be tricky to accomplish a cool looking randomness. 实现很酷的随机性可能很棘手。 The Math.random() method produces pseudo-random values from the uniform distribution (ie, all values in the range have the same probability) and that will not look nice. Math.random()方法根据均匀分布生成伪随机值(即,范围内的所有值具有相同的概率),并且看起来不太好。

I'm not an expect and I cannot tell you what distribution will yield better results but you can try the normal and poisson distributions: 我并不期望,我无法告诉您哪种分布会产生更好的结果,但是您可以尝试使用正态分布和泊松分布:

http://www.ciphersbyritter.com/JAVASCRP/BINOMPOI.HTM (see page source for code) http://www.ciphersbyritter.com/JAVASCRP/BINOMPOI.HTM (有关代码,请参见页面源代码)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM