javascript - 使用 JS JQUERY 随机切换 CSS 标签
问题描述
我正在制作一个随机图像,当鼠标进入带有testarea
类的 div 时会发生变化。
鼠标进入Div,随机切换图像的'off-cursor'标签。
我提到了几个网站。
但是,我是 JS 和 JQUERY 初学者。我不知道如何完成它。
如果您能帮助我,我将不胜感激。
const Cursor = document.querySelectorAll('.cursor');
function getRdCursor() {
const index = Math.floor(Math.random() * Cursor.length);
return Cursor[index];
};
$(function () {
$(".testarea").mousemove(function (e) {
$(".cursor").show().css({
"left": e.clientX,
"top": e.clientY
});
}).mouseout(function () {
$(".cursor").hide();
});
});
.testarea {
border: 1px dashed #ccc;
height: 100px;
margin: 100px 100px 100px 100px;
cursor: none;
}
.cursor {
position: absolute;
width: 5%;
height: auto;
left: -100px;
cursor: none;
pointer-events: none;
transform: translate(-50%,-50%);
}
.off-cursor {
visibility: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="https://media.giphy.com/media/tJ1jipvvMs4r7xuZnI/giphy.gif" alt="Cursor" class="cursor" />
<img src="https://media.giphy.com/media/XFpIo4jKuUrjQHYHeq/giphy.gif" alt="Cursor" class="cursor off-cursor" />
<img src="https://media.giphy.com/media/uxunn6z4qKrGsND3II/giphy.gif" alt="Cursor" class="cursor off-cursor" />
<img src="https://media.giphy.com/media/XxRl7rbKSFvXEFoNzv/giphy.gif" alt="Cursor" class="cursor off-cursor" />
<div class="testarea"></div>
<div class="testarea"></div>
<div class="testarea"></div>
<div class="testarea"></div>
解决方案
推荐阅读
- flutter - 如何通过火焰库在正方形上制作蓝色半透明覆盖
- .net - Serilog 中有没有办法过滤以某个关键字开头的消息?
- mpi - 如何使用 mpiexec 避免 libpciaccess 初始化错误
- javascript - 当我在下拉菜单上选择一个选项时更改 Slick Image (SHOPIFY)
- spring - 如何从依赖项 jar 中修改 bean 中私有字段的值?
- python - python将数组保存到不同长度的csv
- javascript - 无法在 Typescript 的 ES6 类的构造函数中返回自定义对象
- javascript - iframe 画布在 Safari 桌面上没有声音
- javascript - Javascript请求本地服务器不是ec2服务器
- image - 如何将图像保存为文本文件?