欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js鼠標(biāo)跟隨運(yùn)動(dòng)效果

 更新時(shí)間:2017年03月11日 11:05:50   作者:sjpqy  
這篇文章主要為大家詳細(xì)介紹了js鼠標(biāo)跟隨運(yùn)動(dòng)效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js鼠標(biāo)跟隨效果展示的具體代碼,供大家參考,具體內(nèi)容如下

1、使用命令創(chuàng)建基本結(jié)構(gòu)ul.cursorPlay#cursorPlay>li*12>a>img+div>span
2、給span標(biāo)簽添加字段
3、設(shè)置基本的樣式

  • cursorPlay的寬度 992px,高度600px
  • cursorPlay li背景為白色,內(nèi)邊距為8px,外邊距5px,顯示浮動(dòng)為左浮動(dòng)
  • cursorPlay li a,cursorPlay li a img顯示為塊狀并且為相對布局
  • cursorPlay li a添加overflow:hidden
  • cursorPlay li a div為絕對布局,寬度和高度均為100%,設(shè)置背景顏色為rgba

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

1、給綁定鼠標(biāo)進(jìn)入或者出去的事件

$("#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、使用getDir獲取鼠標(biāo)移動(dòng)的方向,coordinates坐標(biāo)

計(jì)算鼠標(biāo)劃入畫出方向函數(shù)(搜索關(guān)鍵詞“jquery計(jì)算鼠標(biāo)劃入劃出方向”)

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、添加移動(dòng)函數(shù)moveTo,三個(gè)參數(shù)分別為選擇器,方向,鼠標(biāo)劃入畫出類型,通過判斷鼠標(biāo)劃入類型,來自定義選擇器初始位置,然后重定義css樣式,當(dāng)鼠標(biāo)劃出時(shí)再重定義每個(gè)方向上的位置

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);
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論