首页 > 解决方案 > 使用jquery为div分配随机滚动速度?

问题描述

因此,我使用 javascript 随机更改了我正在尝试制作的网站上 div 的位置(wordpress 主题上的卡片)。

我希望为每张卡片添加随机滚动速度,因此它们的移动速度略有不同(使用与我用于随机位置的代码类似的代码。

我一直在努力寻找可以成功放入代码中的 jquery 示例。

到目前为止,这是我的代码大纲:

HTML:

<div class="card">
<img src="https://www.naturschaetze-suedwestfalens.de/var/sauerland/storage/images/media/bilder/naturschaetze/buchfotos/05_p3_laubfrosch-mbd/507661-1-ger-DE/05_P3_Laubfrosch-MBD_front_large.jpg">
</div>

<div class="card">
<img src="https://www.naturschaetze-suedwestfalens.de/var/sauerland/storage/images/media/bilder/naturschaetze/buchfotos/05_p3_laubfrosch-mbd/507661-1-ger-DE/05_P3_Laubfrosch-MBD_front_large.jpg">
</div>

<div class="card">
<img src="https://www.naturschaetze-suedwestfalens.de/var/sauerland/storage/images/media/bilder/naturschaetze/buchfotos/05_p3_laubfrosch-mbd/507661-1-ger-DE/05_P3_Laubfrosch-MBD_front_large.jpg">
</div>

CSS:

.card {
position: relative;
}

查询:

//random position of each Card

$(document).ready(function(){
$('.card').each(function() {
$(this).css({'left' : (Math.random() * 120) - 20 + '%'})
$(this).css({'top' : (Math.random() * 200) - 50})

});
});

如果有人能指出我正确的方向,那就太棒了!

谢谢

标签: javascripthtmljqueryrandomscroll

解决方案


推荐阅读