javascript - 使用 css 和 jQuery 使图像旋转
问题描述
单击按钮时,我试图使轮盘赌旋转,但我似乎无法将从我的 js 文件生成的随机数传递给 css
JS
function roulette_spin(btn){
var rand = Math.floor(Math.random() * 720) + 540;
$('.roulette_wheel').css('transform','rotate('+rand+'deg)');
}
HTML
<body>
<div class="roulette_wheel" style="z-index: 0;">
<img src="ayy.jpg" width="500px" height="500px" />
<button value="spin" onclick="roulette_spin(this)">SPIN</button>
</div>
解决方案
这是一个工作示例,其中包含对您的代码的一些更新。
就像@H.Figueiredo 建议的那样,我已将旋转选择器更改为img
以避免按钮旋转。
HTML
<div class="roulette_wheel" style="z-index: 0;">
<img src="https://picsum.photos/200/300" width="500px" height="500px" />
<button class="spin" value="spin">SPIN</button>
</div>
CSS
function roulette_spin(btn){
var rand = Math.floor(Math.random() * 720) + 540;
$('.roulette_wheel img').css('transform','rotate('+rand+'deg)');
}
$('.spin').click(function(){
roulette_spin($(this).siblings('img'));
});
推荐阅读
- android - 如何导航到位于不同模块中的活动
- capl - 使用 CAPL 禁用 CANoe 中的特定节点
- java - 由于缺少 bean,Spring Boot 应用程序无法启动
- data-structures - 哪种方式学习数据结构更有效?
- desktop-application - WPF应用程序中的乌尔都语
- javascript - If 子句括号和相等的优先级
- android - 除非图像 URL 是 HTTPS,否则 Fresco 无法在 Android 9(Pie) 中加载图像
- azure - 我可以更改应用服务计划/应用服务以使用应用服务环境吗
- r - 错误:`data` 和 `reference` 应该是具有相同级别的因子'不返回混淆矩阵
- python - Django:我们可以更改文件字段,以便在选择文件时立即上传