What i've tried is using the Pep.js plugin and the first else if inside the drag option. That and the code is in more detail here: jQuery code in if statement not running even though alert in if statement happens推荐答案好的,来了.脚本:window.onload = function () { var AniMove = function (doc, element) { var ox, oy, mx, my, w = element.offsetWidth, mouseMove = function (e) { e.preventDefault(); if (element.offsetLeft + 0.4 * w > window.innerWidth) { // 60% of the element's width is outside of the window now... do something return; } element.style.left = element.offsetLeft + e.clientX - mx + 'px'; element.style.top = element.offsetTop + e.clientY - my + 'px'; mx = e.clientX; my = e.clientY; return false; }, mouseUp = function () { doc.removeEventListener('mousemove', mouseMove); doc.removeEventListener('mouseup', mouseUp); return; }, mouseDown = function (e) { ox = mx = e.clientX; oy = my = e.clientY; doc.addEventListener('mousemove', mouseMove, false); doc.addEventListener('mouseup', mouseUp, false); return; }; element.addEventListener('mousedown', mouseDown, false); }, drag = new AniMove(document, document.getElementById('square')); }和HTML:<div id="square"></div>最后是CSS:DIV { position: fixed; width: 100px; height: 100px; background: #f00;}完成所有操作后,我们在jsFiddle上有了一个演示.我希望该代码段包含您需要的所有信息.After all that done we have a demo at jsFiddle. I hope this snippet contains all the information you need. 这篇关于在javascript中检测元素是否偏离屏幕右边缘60%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 19:18