本文為大家分享了一個簡單的滑鼠模擬案例,供大家參考,具體實作內容如下
如何實現捕抓滑鼠事件,當滑鼠滑動時,取得目前滑鼠的座標,接著在一個透明區域裡綁定捕抓的位移,這樣就能在模擬的透明區域裡實現滑鼠滑動的模型。
效果圖:
HTML程式碼:
<!DOCTYPE html> <html > <head> <meta charset="UTF-8"> <title> the mouse </title> <link rel="stylesheet" href="css/new.css"> </head> <body> <div class="main"> <div class="content"> <div class="content-nav-top"> <span onclick = 'koringz.createclick1(0)'>默认</span> <span onclick = 'koringz.createclick1(50)'>圆</span> </div> <div class="content-nav-left"> <span onclick = 'koringz.createclick2 (0.25) '>0.25</span> <span onclick = 'koringz.createclick2 (0.5) '>0.5</span> <span onclick = 'koringz.createclick2 (0.75)'>0.75</span> <span onclick = 'koringz.createclick2 (1) '>1</span> </div> <div class="box">鼠标感应器(the mouse sensor)</div> <div class="block"> <div class='block_case'></div> </div> </div> </div> <script type="text/javascript" src="js/demo.min/demo.min.js"></script> </body> </html>
CSS代碼:
* { margin: 0; padding: 0; box-sizing: border-box; } body { position: absolute; text-align: center; height: 100%; width: 100%; } .main{ position: relative; margin: 0 auto; height: 100%; background-color: rgb(48, 70, 82) } .main .content{ position:absolute; display: inline-block; top:50%; left:50%; margin-left: -300px; margin-top: -150px; width: 600px; height: 300px; line-height: 300px; /*overflow: hidden;*/ background: radial-gradient(ellipse farthest-corner, rgb(115, 176, 198) 0%, #888 100%); background: -webkit-radial-gradient(ellipse farthest-corner, rgb(115, 176, 198) 0%, #888 100%); box-shadow: 2px 3px 8px rgba(67, 50, 124 ,.6),0px 0px 8px rgba(67, 50, 124 ,.6); } .main .content .content-nav-top{ display: none; position: absolute; margin-top: -50px; height: 50px; width: 300px; } .main .content .content-nav-top >span{ display: block; float: left; font-size: 16px; font-weight: normal; margin-right:1px; width: 50px; height: 50px; line-height: 50px; background-color: rgba(251, 214, 146,.3); box-shadow: 0px 4px 13px rgb(222,222,222,1); cursor: pointer; } .main .content .content-nav-top >span:nth-child(1){ border-radius:0 ; } .main .content .content-nav-top >span:nth-child(2){ border-radius:50% ; } .main .content .content-nav-top >span:nth-child(3){ border-radius:0; } .main .content .content-nav-top >span:nth-child(4){ border-radius: 50% ; } .main .content .content-nav-left{ display: none; position: absolute; margin-left: -50px; width: 50px; height: 300px; } .main .content .content-nav-left >span{ display: block; font-size: 16px; font-weight: normal; margin-bottom:1px; width: 50px; height: 50px; line-height: 50px; background-color: rgb(85, 145, 140); box-shadow: 0px 4px 13px rgb(222,222,222,1); border-radius:50% 0 0 50% ; cursor: pointer; } .box{ position: relative; float: left; width: 49.9%; height: 100%; border-right-style: solid; border-right-width: 1px; border-right-color: rgba(211,211,211,.5); color:rgb(99, 84, 168); text-shadow: 0px 1px 0px #888,1px 0px 0px #888,0px 0px 1px #888; } .block{ float: right; width: 50%; height: 100%; }
JS代碼:
var koringz = (function(){ var x, y, getmain, getcontent, getbox, getblock, getblock_case, getnav_top, block_case_margin_top, block_case_margin_left, block_casetostring1, block_casetostring2, block_casesubstring1, block_casesubstring2, istouch; getmain = document.querySelector('.main'); getcontent = getmain.querySelector('.content'); getbox = getcontent.querySelector('.box'); getblock = getcontent.querySelector('.block'); getblock_case = getblock.querySelector('.block_case'); getnav_top = getcontent.querySelector('.content-nav-top'); getnav_left = getcontent.querySelector('.content-nav-left'); function get_box() { w_getbox_distance = getbox.offsetWidth; h_getbox_distance = getbox.offsetHeight; istouch = 'ontouchstart' in window; getbox.addEventListener(istouch?'touchmove':'mousemove',mouseevent,false); getbox.addEventListener(istouch?'touchmove':'mousemove',nav,false) } function nav () { return new_nav(); } var new_nav = function () { getnav_top.style.display = 'block'; getnav_left.style.display = 'block'; } function move_box() { getblock_case.style.width = '0px'; getblock_case.style.height = '0px'; block_case_margin_left = getblock_case.style.marginLeft = getblock.offsetWidth/2 + 'px';//子节点 block_case_margin_top = getblock_case.style.marginTop = getblock.offsetHeight/2 + 'px'; block_casetostring1 = block_case_margin_left.toString();//值转化为字符串 block_casetostring2 = block_case_margin_top.toString(); block_casesubstring1 = block_casetostring1.substring(0,3); block_casesubstring2 = block_casetostring2.substring(0,3); } var mouseevent = function () { mouseEvent(event); } function mouseEvent(e){ var zore = 0, val = 1; if(istouch){ x = e.touches[zore].pageX; y = e.touches[zore].pageY; e.preventDefault(); } else if(!istouch){ x = w_getbox_distance/2 != undefined ? e.offsetX:e.layerX; y = h_getbox_distance/2 != undefined ? e.offsetY:e.layerY; } if(val = true){ getblock_case.style.width = x + 'px';//获得了mouse划过的位置 getblock_case.style.height = y + 'px'; getblock_case.style.marginLeft = (block_casesubstring1-x/2) +'px'; getblock_case.style.marginTop = (block_casesubstring2-y/2) +'px'; getblock_case.style.backgroundColor = "rgba(147, 106, 77,1)"; } } (function (){ window.onload = function(){ move_box(); get_box() } })() var click =function () { this.borderradius = function(num) { if(typeof num == 'number'){ if(num == 0){ getblock_case.style.borderRadius = num; } else if(num > 0){ getblock_case.style.borderRadius = num +'%'; } else{ return false; } } } this.opacitas = function (num) { if(typeof num == 'number'){ getblock_case.style.opacity = num; } else{ return false; } } } var Click = new click(); return { createclick1 :Click.borderradius, createclick2 :Click.opacitas } })()
這裡的滑鼠箭頭也可換成自己喜歡的圖標,模擬滑鼠區域的顏色也可自由變換,模擬區域的效果也可是點狀的,也可以是線狀的,動畫效果等等,這個自由發揮吧。
以上就是針對JavaScript滑鼠回應事件的詳細介紹,希望對大家的學習有所幫助。

javaandjavascriptaredistinctlanguages:javaisusedforenterpriseandmobileapps,while javascriptifforInteractiveWebpages.1)JavaisComcompoppored,statieldinglationallyTypted,statilly tater astrunsonjvm.2)

JavaScript核心數據類型在瀏覽器和Node.js中一致,但處理方式和額外類型有所不同。 1)全局對像在瀏覽器中為window,在Node.js中為global。 2)Node.js獨有Buffer對象,用於處理二進制數據。 3)性能和時間處理在兩者間也有差異,需根據環境調整代碼。

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python和JavaScript的主要區別在於類型系統和應用場景。 1.Python使用動態類型,適合科學計算和數據分析。 2.JavaScript採用弱類型,廣泛用於前端和全棧開發。兩者在異步編程和性能優化上各有優勢,選擇時應根據項目需求決定。

選擇Python還是JavaScript取決於項目類型:1)數據科學和自動化任務選擇Python;2)前端和全棧開發選擇JavaScript。 Python因其在數據處理和自動化方面的強大庫而備受青睞,而JavaScript則因其在網頁交互和全棧開發中的優勢而不可或缺。

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3漢化版
中文版,非常好用

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)