烙印99
2018-09-11 13:13:12
/------------------js部分------------------------- /問題是為什么鼠標(biāo)拖拽滑塊的時候,滑塊會閃動,求大神指點(diǎn)案例代碼:https://jsfiddle.net/bbux0h7v/2/
1 回答

一只甜甜圈
TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個贊
把offsetY
改成pageY
就行了。
關(guān)于offsetY
的意思,規(guī)范是這么寫的:
The MouseEvent.offsetY read-only property provides the offset in the Y coordinate of the mouse pointer between that event and the padding edge of the target node.
關(guān)于pageY
的意思:
The MouseEvent.pageY read-only property returns the vertical coordinate of the event relative to the whole document.
你體會一下這兩者的區(qū)別,然后再想想為什么offsetY
不行。
最后,個人覺得你的js代碼寫的有點(diǎn)亂……我稍微改了改,你可以參考下:
$(document).ready(function(){ let axiL = 0, doc = $(document), b = $("#b"); //定義鼠標(biāo)拖動時執(zhí)行的函數(shù) function move(event){ const top = parseInt(b.css("top")); let axiN = top + event.pageY - axiL; axiN = (axiN < 0) ? 0: axiN; axiN = (axiN > 170) ? 170: axiN; b.css("top", axiN); axiL = event.pageY; } b.on("mousedown", function() { doc.on("mousemove", move); axiL = event.pageY; }); doc.on("mouseup", function() { doc.off("mousemove", move); }); });
添加回答
舉報
0/150
提交
取消