国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

js實現鼠標跟隨運動效果

2024-05-06 15:11:47
字體:
來源:轉載
供稿:網友

鼠標跟隨運動效果展示

1、使用命令創建基本結構ul.cursorPlay#cursorPlay>li*12>a>img+div>span

2、給span標簽添加字段

3、設置基本的樣式

1、cursorPlay的寬度 992px,高度600px
2、cursorPlay li背景為白色,內邊距為8px,外邊距5px,顯示浮動為左浮動
3、cursorPlay li a,cursorPlay li a img顯示為塊狀并且為相對布局
4、cursorPlay li a添加overflow:hidden
5、cursorPlay li a div為絕對布局,寬度和高度均為100%,設置背景顏色為rgba

4、js添加動態效果(方向0,1,2,3分別為上,右,下,左)

1)、給綁定鼠標進入或者出去的事件

$("#cursorPlay li").on("mouseenter mouseleave",function(event){var evType = event.type;var direction = getDir($(this), {x: event.pageX,y: event.pageY});//  console.log("evtype:"+evType+",dir:"+direction);moveTo($(this),direction, evType);});2、

2)、使用getDir獲取鼠標移動的方向,coordinates坐標

計算鼠標劃入畫出方向函數(搜索關鍵詞“jquery計算鼠標劃入劃出方向”)

direction = Math.round((((Math.atan2(y, x) * (180 / Math.PI)) + 180) / 90) + 3) % 4;function getDir($el, coordinates){var w = $el.width(),h = $el.height(),x = (coordinates.x - $el.offset().left - (w / 2)) * (w > h ? (h / w) : 1),y = (coordinates.y - $el.offset().top - (h / 2)) * (h > w ? (w / h) : 1),direction = Math.round((((Math.atan2(y, x) * (180 / Math.PI)) + 180) / 90) + 3) % 4;return direction;}

3)、添加移動函數moveTo,三個參數分別為選擇器,方向,鼠標劃入畫出類型,通過判斷鼠標劃入類型,來自定義選擇器初始位置,然后重定義css樣式,當鼠標劃出時再重定義每個方向上的位置

function moveTo($el, direction, type){  var $layer = $el.find("div");  var coord = {};  if(type === "mouseenter"){    switch(direction){     case 0 :   $layer.css("top","-100%").css("left","0px");break;    case 1 : $layer.css("left","100%").css("top","0px");break;    case 2 : $layer.css("top","100%").css("left","0px");break;    case 3 : $layer.css("left","-100%").css("top","0px");break;  }    coord = {left:0,top:0}  }else{  switch(direction){    case 0 : coord = {left:0,top:'-100%'};break;    case 1 : coord = {left:'100%',top:0};break;    case 2 : coord = {left:0,top:'100%'};break;    case 3 : coord = {left:'-100%',top:0};break;  }}$layer.animate(coord,300);} 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 莱阳市| 惠来县| 股票| 榆林市| 孟州市| 莆田市| 博湖县| 长宁县| 从化市| 宁安市| 昌江| 彰武县| 通江县| 绥中县| 和平区| 桐城市| 南京市| 广安市| 乐都县| 祥云县| 洱源县| 德庆县| 宁乡县| 内丘县| 石棉县| 洞口县| 临澧县| 将乐县| 大悟县| 津市市| 巍山| 宜城市| 江门市| 长海县| 盈江县| 稻城县| 积石山| 崇礼县| 承德市| 沙田区| 托克逊县|