html中如何获取鼠标的坐标,CSS3 在CSS中通过获取鼠标座标变量

这篇博客介绍了一种利用CSS变量`var()`和`:root`选择器来动态设置元素位置的方法。通过将鼠标坐标定义为CSS变量,并在`div::after`伪元素中使用`counter-reset`和`content`属性,可以实时显示鼠标的位置。同时,博客还展示了如何通过`calc()`函数和`transform`属性来根据鼠标坐标调整元素的定位。
摘要由CSDN通过智能技术生成

CSS

语言:

CSSSCSS

确定

:root {

--mouse-x: 200;

--mouse-y: 200;

}

/* See: http://stackoverflow.com/q/40164169/4172644*/

div::after {

counter-reset: mouse-x var(--mouse-x) mouse-y var(--mouse-y);

content: counter(mouse-x) "px, " counter(mouse-y) "px";

}

/* setup and presentation styles */

div::before {

content: 'mouse position';

font-weight: bold;

}

div {

position: fixed;

top: 0;

left: 0;

-webkit-transform: translate(calc(var(--mouse-x) * 1px), calc(var(--mouse-y) * 1px));

transform: translate(calc(var(--mouse-x) * 1px), calc(var(--mouse-y) * 1px));

width: 10em;

height: 10em;

background: #9ccf5e;

color: #fff;

display: -webkit-box;

display: -webkit-flex;

display: -ms-flexbox;

display: flex;

-webkit-flex-flow: column;

-ms-flex-flow: column;

flex-flow: column;

-webkit-box-align: center;

-webkit-align-items: center;

-ms-flex-align: center;

align-items: center;

-webkit-box-pack: center;

-webkit-justify-content: center;

-ms-flex-pack: center;

justify-content: center;

line-height: 2;

border-radius: 100%;

will-change: transform;

}

body {

margin: 2em;

font-family: sans-serif;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值