鼠标跟随 html,鼠标跟随效果.html

鼠标跟随效果

.mouse-container{

background: #000;

width: 800px;

margin:20px auto;

}

table > tr > td {

color:transparent;

width: 40px;

height: 40px;

border: 1px solid #000;

}

.border{

border: 1px solid #fff;

background: #333;

}

.border-gray-top{

border: 1px solid #aaa;

border-top: 1px solid #000;

background: #222;

}

.border-gray-bottom{

border: 1px solid #aaa;

border-bottom: 1px solid #000;

background: #222;

}

.border-gray-left{

border: 1px solid #aaa;

border-left: 1px solid #000;

background: #222;

}

.border-gray-right{

border: 1px solid #aaa;

border-right: 1px solid #000;

background: #222;

}

.border-top-left{

border-top: 1px solid #666;

border-left: 1px solid #666;

}

.border-top-right{

border-top: 1px solid #666;

border-right: 1px solid #666;

}

.border-bottom-left{

border-bottom: 1px solid #666;

border-left: 1px solid #666;

}

.border-bottom-right{

border-bottom: 1px solid #666;

border-right: 1px solid #666;

}

const container = document.getElementsByClassName("mouse-container")[0];

const table = document.createElement("table");

for (let i = 0; i < 20; i++) {

const tr = document.createElement("tr");

for (let j = 0; j < 20; j++) {

const td = document.createElement("td");

td.classList.add(`${i}-${j}`);

td.innerHTML = `${i}-${j}`;

tr.appendChild(td);

td.addEventListener("mouseleave",(e)=>{

if(e.target.nodeName.toLowerCase()==="td"){

e.target.classList.remove("border");

const [xAxis,yAxis] = e.target.innerHTML.split("-");

if(xAxis-1>=0){

const topTarget = document.getElementsByClassName(`${xAxis-1}-${yAxis}`)[0];

topTarget.classList.remove("border-gray-top");

}

if(xAxis*1<19){

const bottomTarget = document.getElementsByClassName(`${xAxis*1+1}-${yAxis}`)[0];

bottomTarget.classList.remove("border-gray-bottom");

}

if(yAxis-1>=0){

const leftTarget = document.getElementsByClassName(`${xAxis}-${yAxis-1}`)[0];

leftTarget.classList.remove("border-gray-left");

}

if(yAxis*1<19){

const rightTarget = document.getElementsByClassName(`${xAxis}-${yAxis*1+1}`)[0];

rightTarget.classList.remove("border-gray-right");

}

if(xAxis-1>=0 && yAxis-1>=0){

const topLeftTarget = document.getElementsByClassName(`${xAxis-1}-${yAxis-1}`)[0];

topLeftTarget.classList.remove("border-bottom-right");

}

if(xAxis*1<19 && yAxis-1>=0){

const topLeftTarget = document.getElementsByClassName(`${xAxis*1+1}-${yAxis-1}`)[0];

topLeftTarget.classList.remove("border-top-right");

}

if(xAxis-1>=0 && yAxis*1<19){

const topLeftTarget = document.getElementsByClassName(`${xAxis-1}-${yAxis*1+1}`)[0];

topLeftTarget.classList.remove("border-bottom-left");

}

if(xAxis*1<19 && yAxis*1<19){

const bottomRightTarget = document.getElementsByClassName(`${xAxis*1+1}-${yAxis*1+1}`)[0];

bottomRightTarget.classList.remove("border-top-left");

}

}

})

}

table.appendChild(tr);

}

container.appendChild(table);

table.addEventListener("mousemove",(e)=>{

if(e.target.nodeName.toLowerCase()==="td"){

e.target.classList.add("border");

const [xAxis,yAxis] = e.target.innerHTML.split("-");

if(xAxis-1>=0){

const topTarget = document.getElementsByClassName(`${xAxis-1}-${yAxis}`)[0];

topTarget.classList.add("border-gray-top");

}

if(xAxis*1<19){

const bottomTarget = document.getElementsByClassName(`${xAxis*1+1}-${yAxis}`)[0];

bottomTarget.classList.add("border-gray-bottom");

}

if(yAxis-1>=0){

const leftTarget = document.getElementsByClassName(`${xAxis}-${yAxis-1}`)[0];

leftTarget.classList.add("border-gray-left");

}

if(yAxis*1<19){

const rightTarget = document.getElementsByClassName(`${xAxis}-${yAxis*1+1}`)[0];

rightTarget.classList.add("border-gray-right");

}

if(xAxis-1>=0 && yAxis-1>=0){

const topLeftTarget = document.getElementsByClassName(`${xAxis-1}-${yAxis-1}`)[0];

topLeftTarget.classList.add("border-bottom-right");

}

if(xAxis*1<19 && yAxis-1>=0){

const topLeftTarget = document.getElementsByClassName(`${xAxis*1+1}-${yAxis-1}`)[0];

topLeftTarget.classList.add("border-top-right");

}

if(xAxis-1>=0 && yAxis*1<19){

const topLeftTarget = document.getElementsByClassName(`${xAxis-1}-${yAxis*1+1}`)[0];

topLeftTarget.classList.add("border-bottom-left");

}

if(xAxis*1<19 && yAxis*1<19){

const bottomRightTarget = document.getElementsByClassName(`${xAxis*1+1}-${yAxis*1+1}`)[0];

bottomRightTarget.classList.add("border-top-left");

}

}

});

// container.innerHTML = "看看我在哪儿"

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值