js实现跟随鼠标轨迹滑动效果,有两种类型选择,可直线滑动,也可跟随轨迹滑动,这也是获取鼠标轨迹的js特效。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html;charset=gb2312"> <meta name="keywords" content="武鸣人网站,武鸣信息网,武鸣本地网,武鸣信息资源平台,减肥,健身,励志,励志语录,js特效,网页特效,www.wuming.ren"> <meta name="description" content="欢迎来到武鸣人信息资源平台,各种信息免费发布,资源共享合作共赢,分享互联网流行的励志语录经典短句,减肥健身健康小常识及打卡记录,收集各种js特效代码。"> <title>经典跟随鼠标轨迹滑动js特效_武鸣人</title> <style type="text/css"> body,div{margin:0;padding:0;} div{position:absolute;width:66px;height:45px;background:url(/img/20240304_www_wuming_ren_16.gif) no-repeat;top:100px;left:50px;} p,input{margin:10px;} </style> <script type="text/javascript"> window.onload = function () {//武鸣人网站https://www.wuming.ren 网站很好记住,wuming.ren(武鸣拼音.ren域名后缀) var oDiv = document.getElementsByTagName("div")[0]; var aInput = document.getElementsByTagName("input"); var oP = document.getElementsByTagName("p")[0]; var i = 0; aInput[0].onclick = function (event) { (event || window.event).cancelBubble = true; clearEvent(); this.value += "(已激活)"; oP.innerHTML = "鼠标点击页面, 人物将移动至鼠标位置!"; document.onclick = function (event) { var event = event || window.event; oDiv.style.background = "url(/img/20240304_www_wuming_ren_17.gif) no-repeat"; startMove(oDiv, {x:event.clientX, y:event.clientY}, function(){oDiv.style.background = "url(/img/20240304_www_wuming_ren_16.gif) no-repeat"}); return false; } }; aInput[1].onclick = function (event) { (event || window.event).cancelBubble = true; clearEvent(); this.value += "(已激活)"; oP.innerHTML = "按住鼠标左键,在页面划动,人物将按照鼠标轨迹移动。" var aPos = [{x:oDiv.offsetLeft, y:oDiv.offsetTop}]; document.onmousedown = function (event) { var event = event || window.event; aPos.push({x:event.clientX, y:event.clientY}); document.onmousemove = function (event) { var event = event || window.event; aPos.push({x:event.clientX, y:event.clientY}); return false; } return false; } document.onmouseup = function () { document.onmousemove = null; oDiv.style.background = "url(/img/20240304_www_wuming_ren_17.gif) no-repeat"; var timer = setInterval(function () { if(aPos.length == 0) { clearInterval(timer); oDiv.style.background = "url(/img/20240304_www_wuming_ren_16.gif) no-repeat"; return; }; oDiv.style.left = aPos[0].x + "px"; oDiv.style.top = aPos[0].y + "px"; aPos.shift(); }, 30); }; } function clearEvent() { document.onclick = null; document.onmousedown = null; document.onmousemove = null; document.onmouseup = null; for (i = 0; i < aInput.length; i++) { aInput[i].value = aInput[i].value.replace("(已激活)", ""); aInput[i].onmousedown = aInput[i].onmouseup = function (event) { (event || window.event).cancelBubble = true; }; } } };//武鸣人网站https://www.wuming.ren 网站很好记住,wuming.ren(武鸣拼音.ren域名后缀) function startMove(obj, oTarget, fnEnd) { clearInterval(obj.timer); obj.timer = setInterval(function () { doMove(obj, oTarget, fnEnd) }, 30) }//武鸣人网站https://www.wuming.ren 网站很好记住,wuming.ren(武鸣拼音.ren域名后缀) function doMove(obj, oTarget, fnEnd) { var iX = (oTarget.x - obj.offsetLeft) / 5; var iY = (oTarget.y - obj.offsetTop) / 5; iX = iX > 0 ? Math.ceil(iX) : Math.floor(iX); iY = iY > 0 ? Math.ceil(iY) : Math.floor(iY); if (oTarget.x == obj.offsetLeft && oTarget.y == obj.offsetTop) { clearInterval(obj.timer); fnEnd && fnEnd(); } else { obj.style.left = obj.offsetLeft + iX + "px"; obj.style.top = obj.offsetTop + iY + "px"; } }//武鸣人网站https://www.wuming.ren 网站很好记住,wuming.ren(武鸣拼音.ren域名后缀) </script> </head> <body> <a href="https://www.wuming.ren">武鸣人</a>,各种信息资源免费发布,分享励志语录经典短句,减肥健身常识,各种js特效代码。网站很好记住,wuming.ren(武鸣拼音.ren域名后缀)<hr> <!--欢迎来到武鸣人信息资源平台,各种信息免费发布,资源共享合作共赢,分享互联网流行的励志语录经典短句,减肥健身健康小常识及打卡记录,收集各种js特效代码。--> <script type="text/javascript" src="https://www.wuming.ren/ad/tc.js"></script> <script type="text/javascript" src="https://www.wuming.ren/ad/a.js"></script> <input type="button" value="根据鼠标点击位置移动"> <input type="button" value="根据标鼠标轨迹移动"> <p>请点击按钮激活功能!</p> <div></div> </body> </html>
本文来自武鸣人网站,转载请注明出处