能夠感知滑鼠移動方位的遮罩層效果程式碼例項
分享一段程式碼例項,它實現了感知滑鼠移動方位的遮罩層效果。
也就是是遮罩層的出現和滑鼠進入的方位是一致的。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> * { margin: 0; padding: 0; } ul { width: 630px; overflow: hidden; margin: 100px auto; } li { float: left; width: 200px; height: 200px; background: #ccc; margin: 5px; position: relative; overflow: hidden; } li span { position: absolute; top: 0; left: 200px; width: 100%; height: 100%; background: rgba(255,0,0,0.3); } li span p { font-size: 14px; color: #fff; padding: 10px; white-space: pre-wrap; } </style> <script> function getStyle(obj, name) { if (obj.currentStyle) { return obj.currentStyle[name]; } else { return getComputedStyle(obj, false)[name]; } } function move(obj, json, options) { options = options || {}; options.duration = options.duration || 700; options.easing = options.easing || 'ease-in'; var count = Math.floor(options.duration / 30); var start = {}; var dis = {}; for (var name in json) { start[name] = parseFloat(getStyle(obj, name)); dis[name] = json[name] - start[name]; } var n = 0; clearInterval(obj.timer); obj.timer = setInterval(function() { n++; for (var name in json) { switch (options.easing) { case 'linear': var cur = start[name] + dis[name] * n / count; break; case 'ease-in': var a = Math.pow(n / count, 3); var cur = start[name] + dis[name] * a; break; case 'ease-out': var a = Math.pow(1 - n / count, 3); var cur = start[name] + dis[name] * (1 - a); break; } if (name == 'opacity') { obj.style[name] = cur; obj.style.filter = 'alpha(opacity:' + (cur * 100) + ')'; } else { obj.style[name] = cur + 'px'; } } if (n == count) { clearInterval(obj.timer); options.complete && options.complete(); } }, 30); } function a2d(n) { return n * 180 / Math.PI; } function hoverDir(obj, ev) { var a = ev.clientX - (obj.offsetLeft + obj.offsetWidth / 2); var b = obj.offsetTop + obj.offsetHeight / 2 - ev.clientY; return Math.round((a2d(Math.atan2(b, a)) + 180) / 90) % 4; } function through(obj) { obj.onmouseenter = function(ev) { var oEvent = ev || event; var oS = this.children[0]; var dir = hoverDir(obj, oEvent); switch (dir) { case 0: oS.style.left = '-200px'; oS.style.top = 0; break; case 1: oS.style.left = 0; oS.style.top = '200px'; break; case 2: oS.style.left = '200px'; oS.style.top = 0; break; case 3: oS.style.left = 0; oS.style.top = '-200px'; break; } move(oS, { left: 0, top: 0 }, { duration: 300, easeing: 'linear' }); } obj.onmouseleave = function(ev) { var oEvent = ev || event; var oS = this.children[0]; var dir = hoverDir(obj, oEvent); switch (dir) { case 0: move(oS, { left: -200, top: 0 }, { duration: 300, easing: 'linear' }); break; case 1: move(oS, { left: 0, top: 200 }, { duration: 300, easing: 'linear' }); break; case 2: move(oS, { left: 200, top: 0 }, { duration: 300, easing: 'linear' }); break; case 3: move(oS, { left: 0, top: -200 }, { duration: 300, easing: 'linear' }); break; } }; } window.onload = function() { var oLi = document.getElementsByTagName("li"); for (var i = 0; i < oLi.length; i++) { through(oLi[i]) } } </script> </head> <body> <ul> <li><span><p>螞蟻部落歡迎您</p></span></li> <li><span><p>螞蟻部落歡迎您</p></span></li> <li><span><p>螞蟻部落歡迎您</p></span></li> <li><span><p>螞蟻部落歡迎您</p></span></li> <li><span><p>螞蟻部落歡迎您</p></span></li> <li><span><p>螞蟻部落歡迎您</p></span></li> </ul> </body> </html>
相關文章
- CSS滑鼠懸浮出現遮罩層程式碼演示CSS遮罩
- jQuery點選滑出層效果程式碼例項jQuery
- 美化滾動條效果程式碼例項
- JavaScript隨滑鼠晃動的div塊程式碼例項JavaScript
- css3晃動效果程式碼例項CSSS3
- CSS3星系運動效果程式碼例項CSSS3
- jQuery tab選項卡效果程式碼例項jQuery
- canvas載入效果程式碼例項Canvas
- CSS橢圓效果程式碼例項CSS
- canvas原型鐘錶效果程式碼例項Canvas原型
- css3水滴效果程式碼例項CSSS3
- css背景虛化效果程式碼例項CSS
- canvas氣泡上浮效果程式碼例項Canvas
- CSS3心形效果程式碼例項CSSS3
- div前後翻轉效果程式碼例項
- 當前文字框高亮效果程式碼例項
- CSS3旋轉效果程式碼例項CSSS3
- 淡入淡出效果簡單程式碼例項
- canvas繪製箭頭效果程式碼例項Canvas
- CSS3滑鼠懸浮環裝遮罩效果CSSS3遮罩
- html實現簡單ListViews效果的例項程式碼HTMLView
- JavaScript跟隨滑鼠移動的圖片效果JavaScript
- 滑鼠懸浮出現圓形資訊遮罩層遮罩
- CSS3滑過光束效果程式碼例項CSSS3
- CSS3小黃人效果程式碼例項CSSS3
- css3折角效果程式碼例項CSSS3
- CSS3梯形效果程式碼例項CSSS3
- CSS3背景漸變效果程式碼例項CSSS3
- CSS3圖層陰影程式碼例項CSSS3
- 商品搶購倒數計時效果程式碼例項
- CSS3文字凹凸效果程式碼例項CSSS3
- JavaScript運動框架程式碼例項JavaScript框架
- 遮罩層遮罩
- 解決小程式遮罩層滾動穿透問題遮罩穿透
- 滑鼠懸浮從中心放大和縮小的半透明遮罩層遮罩
- 用flutter給圖片加個好看的遮罩層【flutter20個例項之六】Flutter遮罩
- CSS3文字陰影效果程式碼例項CSSS3
- 遮罩層禁止頁面滾動遮罩
- app直播原始碼,彈出層 加遮罩層 頁面禁止滑動APP原始碼遮罩