英文:
Windows Trail on moving HTML/CSS
问题
I want to use an image as my cursor and have it leave a trail as I move my mouse on the page. Can you provide examples using HTML, CSS, and maybe JavaScript? The image I'd like to use is an old Windows error message, and I want it to fill up the entire page. Can anyone assist with this effect?
英文:
I'm trying to create a cool effect on my website using HTML, CSS, and maybe some JavaScript.
I want to use an image and as my cursor,
and I want it to leave a trail behind as I move my mouse around the page.
I'm not very experienced with JavaScript, so I'd appreciate it if you could show me some examples.
The image I want to use is an old Windows error message, and I want to fill up the page with it.
Can anyone help me achieve this effect?
Thank you!
答案1
得分: 2
以下是翻译好的内容:
"unfortunately for now native CSS cursor
can't be used for this type of things natively (for example: image need to be very small like a icon)"
"但如果我们使用 <img>
方法,希望我们可以找到解决方案:"
"## 简短解决方案:"
"无需复杂的数学或JavaScript,我们可以使用本机CSS属性transition-duration
,并获得非常平滑的结果。"
transition-duration: calc(
var(--i) * 0.02s /* 增加0.02秒,如果你想要更多效果,例如0.05秒 */
);
"这是我得到的结果:"
"下面是演示代码:"
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const popups = document.querySelectorAll(".popup");
// just a simple move event, the magic is in the css
document.addEventListener("mousemove", (e) => {
popups.forEach((popup) => {
popup.style.left = `${e.clientX - popup.clientWidth / 2}px`;
popup.style.top = `${e.clientY - popup.clientHeight / 2}px`;
});
});
<!-- language: lang-css -->
.popup-container {
position: relative;
}
.popup {
position: absolute;
top: 0;
left: 0;
width: 50vh;
transition-duration: calc(var(--i) * 0.02s); /* 这里魔法发生 */
}
<!-- language: lang-html -->
<div class="popup-container">
<img style="--i: 5;" class="popup" src="https://i.stack.imgur.com/YNu91.png" alt="" />
<img style="--i: 4;" class="popup" src="https://i.stack.imgur.com/YNu91.png" alt="" />
<img style="--i: 3;" class="popup" src="https://i.stack.imgur.com/YNu91.png" alt="" />
<img style="--i: 2;" class="popup" src="https://i.stack.imgur.com/YNu91.png" alt="" />
<img style="--i: 1;" class="popup" src="https://i.stack.imgur.com/YNu91.png" alt="" />
<img style="--i: 0;" class="popup" src="https://i.stack.imgur.com/YNu91.png" alt="" />
</div>
<!-- end snippet -->
英文:
unfortunately for now native CSS cursor
can't be used for this type of things natively (for example: image need to be very small like a icon)
but if we use <img>
approach then hopefully we can find a solution:
TLDR solution:
Without any complex math or javascript,
<br>we can use the native CSS property transition-duration
,
<br>and get some results that are very smooth.
transition-duration: calc(
var(--i) * 0.02s /* increase the 0.02s if you want more effect, for example to 0.05s */
);
here the result I got:
demo code below:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const popups = document.querySelectorAll(".popup");
// just a simple move event, the magic is in the css
document.addEventListener("mousemove", (e) => {
popups.forEach((popup) => {
popup.style.left = `${e.clientX - popup.clientWidth / 2}px`;
popup.style.top = `${e.clientY - popup.clientHeight / 2}px`;
});
});
<!-- language: lang-css -->
.popup-container {
position: relative;
}
.popup {
position: absolute;
top: 0;
left: 0;
width: 50vh;
transition-duration: calc(var(--i) * 0.02s); /* here magic happens */
}
<!-- language: lang-html -->
<div class="popup-container">
<img style="--i: 5;" class="popup" src="https://i.stack.imgur.com/YNu91.png" alt="" />
<img style="--i: 4;" class="popup" src="https://i.stack.imgur.com/YNu91.png" alt="" />
<img style="--i: 3;" class="popup" src="https://i.stack.imgur.com/YNu91.png" alt="" />
<img style="--i: 2;" class="popup" src="https://i.stack.imgur.com/YNu91.png" alt="" />
<img style="--i: 1;" class="popup" src="https://i.stack.imgur.com/YNu91.png" alt="" />
<img style="--i: 0;" class="popup" src="https://i.stack.imgur.com/YNu91.png" alt="" />
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论