栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

写一个让一句话随着鼠标移动的小插件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

写一个让一句话随着鼠标移动的小插件

function mousemoveWithText(text, options) {  options = options || {};  const $el = text instanceof HTMLElement ? text : createElement();  const pos = { x: -9999, y: - 9999 };  // TODO: 移动后才第一次能显示,有点糟糕  window.onmousemove = handleMove;  setTextPosition();  (function loop() {    setTextPosition();    // TODO: 如果需要销毁,需要存变量    requestAnimationframe(loop);  })();  function createElement() {    const $el = document.createElement('div');    $el.innerText = text;    $el.style.position = 'fixed';    $el.style.pointerEvents = 'none';    document.body.appendChild($el);    return $el;  }  function handleMove(e) {    pos.x = e.clientX;    pos.y = e.clientY;    // TODO: 超出屏幕应隐藏,不然会造成比如 scroller-x 超出  }  function setTextPosition() {    const { x, y } = pos;    $el.style.left = x + 10 + 'px';    $el.style.top = y - 10 + 'px';  }}mousemoveWithText('xxxx');
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/385619.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号