Windows Trail on moving HTML/CSS

huangapple go评论78阅读模式
英文:

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?

Windows Trail on moving HTML/CSS

英文:

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?

Windows Trail on moving HTML/CSS

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秒 */
                         ); 

"这是我得到的结果:"

Windows Trail on moving HTML/CSS

"下面是演示代码:"

<!-- 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 &lt;img&gt; 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:

Windows Trail on moving HTML/CSS

demo code below:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const popups = document.querySelectorAll(&quot;.popup&quot;);

// just a simple move event, the magic is in the css
document.addEventListener(&quot;mousemove&quot;, (e) =&gt; {
  popups.forEach((popup) =&gt; {
    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 -->

&lt;div class=&quot;popup-container&quot;&gt;
  &lt;img style=&quot;--i: 5;&quot; class=&quot;popup&quot; src=&quot;https://i.stack.imgur.com/YNu91.png&quot; alt=&quot;&quot; /&gt;
  &lt;img style=&quot;--i: 4;&quot; class=&quot;popup&quot; src=&quot;https://i.stack.imgur.com/YNu91.png&quot; alt=&quot;&quot; /&gt;
  &lt;img style=&quot;--i: 3;&quot; class=&quot;popup&quot; src=&quot;https://i.stack.imgur.com/YNu91.png&quot; alt=&quot;&quot; /&gt;
  &lt;img style=&quot;--i: 2;&quot; class=&quot;popup&quot; src=&quot;https://i.stack.imgur.com/YNu91.png&quot; alt=&quot;&quot; /&gt;
  &lt;img style=&quot;--i: 1;&quot; class=&quot;popup&quot; src=&quot;https://i.stack.imgur.com/YNu91.png&quot; alt=&quot;&quot; /&gt;
  &lt;img style=&quot;--i: 0;&quot; class=&quot;popup&quot; src=&quot;https://i.stack.imgur.com/YNu91.png&quot; alt=&quot;&quot; /&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月21日 03:04:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定