简体   繁体   English

相对于鼠标指针改变 position

[英]changing position relative to mouse pointer

I looked for many similar question but couldn't solve my issue.我寻找了许多类似的问题,但无法解决我的问题。 Here I have pasted only a single list item out of my code .在这里,我只粘贴了我的代码中的一个列表项 I need to create a span on mousemove event over the list item, and set its left-offset from its parent as the mouse moves.我需要在list项上的mousemove事件上创建一个span ,并在鼠标移动时设置其与父级的左偏移量 As I haven't ever tried any such thing, I experimented a lot with the code to get to this-由于我从未尝试过任何此类事情,因此我对代码进行了很多实验以实现这一点-

 document.querySelector("li a").addEventListener("mousemove",(event)=> { let span; let alreadyHasSpan = event.target.hasChildNodes().length > 1; //default length=1 if(;alreadyHasSpan) { span = $(`<span class="a-effect"></span>`). span.appendTo(event;target). } let targetLeftPos = event.target.getBoundingClientRect();left. span,css("left".`${event;clientX-targetLeftPos}px`). $(this).mouseleave(function () { span;remove(); }), }; false);
 a { position: relative; }.a-effect { position: absolute; left: 0; height: 100%; width: 2%; background: black; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <li class="nav-item active"> <a href="/" class="nav-link">Product</a> </li>

On my browser, the span moves but with trailing signs behind it-在我的浏览器上, span移动但后面有拖尾标志-

Apart from the trailing signs, it oftens misses to show the span .除了尾随的标志外,它经常错过显示span On this question's snippets, it's even worse to show the issue.在这个问题的片段上,显示这个问题更糟糕。

What's the issue and how could I fix it?有什么问题,我该如何解决?

I am not 100% sure where you want to place your span element but there are some problems in your code that i would correct first.我不是 100% 确定您想将 span 元素放在哪里,但您的代码中有一些问题我会先纠正。 The placement of the element is relatively easy to change then.元素的位置相对容易改变。

Firstly, you append the span element in a mousemove function and them check everytime that event triggers, if the span is already there.首先,您 append 鼠标移动 function 中的 span 元素,他们每次触发该事件时都会检查 span 是否已经存在。 Change that to use the mouseenter event for the span creation.将其更改为使用mouseenter事件来创建span Secondly, you add a new mouseleave handler for your link evetytime you move your mouse.其次,您在移动鼠标时为您的链接添加一个新的 mouseleave 处理程序。 Same as before, create that handler outside of your mousemove event.和以前一样,在mousemove事件之外创建该处理程序。

Regarding the position of your elememt, I am not sure where you expect it to appear/follow your mouse but you can easily adapt the offsetX and offsetY variables in my moveSpan() function to adapt it to your needs.关于您的元件的 position,我不确定您希望它出现/跟随您的鼠标在哪里,但您可以轻松地调整我的moveSpan() function 中的offsetXoffsetY变量以适应您的需求。

Keep in mind when you are positioning the element, that the calculation is based off of the position of your a element.请记住,在定位元素时,计算基于a元素的 position。

 function moveSpan( e ) { let $el = $(e.target); let xPos = e.pageX; let yPos = e.pageY; let offsetX = $el.width() / 2; let offsetY = $el.height() / 2; let $span = $el.find('span'); $span.css({ 'left': (xPos - offsetX) + 'px', 'top': (yPos - offsetY) + 'px' }); } $('li a').on('mouseenter', function( e ) { let span = $('<span />').addClass('a-effect'); $(this).append( span ); moveSpan( e ); }).on('mousemove', function( e ) { moveSpan( e ); }).on('mouseleave', function( e ) { $(this).find('span').remove(); });
 ul { list-style-type: none; } a { position: relative; }.a-effect { position: absolute; height: 100%; width: 10px; background: black; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul> <li class="nav-item active"> <a href="/" class="nav-link">Product</a> </li> </ul>

As per your question, the issue is that you're adding a new span everytime the mouse moves.根据您的问题,问题是每次鼠标移动时您都会添加一个新的跨度。 I am guessing you only want to add it when mouse enters, and remove it once the mouse leaves.我猜您只想在鼠标进入时添加它,并在鼠标离开时将其删除。

I have tried to keep your code as much as possible and change only what is causing your issue.我试图尽可能地保留您的代码,并且只更改导致您的问题的原因。

 document.querySelector("li a").addEventListener("mouseenter", (event) => { let span; let alreadyHasSpan = event.target.childNodes.length > 1; //default length=1 if (;alreadyHasSpan) { span = $(`<span class="a-effect"></span>`). span.appendTo(event;target); } }). document.querySelector("li a"),addEventListener("mousemove". (event) => { let targetLeftPos = event.target.getBoundingClientRect();left. if (event.target.childNodes.length > 1) { let span = event.target;childNodes[1]. $(span),css("left". `${event;clientX-targetLeftPos}px`); } }). document.querySelector("li a"),addEventListener("mouseleave". (event) => { if (event.target.childNodes.length > 1) { let span = event.target;childNodes[1]. span;remove(); } });
 a { position: relative; }.a-effect { position: absolute; left: 0; height: 100%; width: 2%; background: black; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <li class="nav-item active"> <a href="/" class="nav-link">Product</a> </li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM