>  기사  >  웹 프론트엔드  >  JS로 구현된 마우스 따라가기 코드(카툰 손 클릭 효과)

JS로 구현된 마우스 따라가기 코드(카툰 손 클릭 효과)

PHPz
PHPz앞으로
2016-05-16 15:35:062159검색

이 글에서는 주로 JS에서 구현된 마우스 추종 코드와 카툰 손 클릭 효과를 소개합니다. JavaScript 마우스 이벤트에 대한 반응과 페이지 요소의 동적 호출 기술을 포함하여 필요한 친구는 다음과 같이 참조할 수 있습니다.

마우스가 움직이는 곳마다 작은 손 효과가 나타납니다. 손이 따라옵니다.손 효과가 나타납니다.손이 바뀌게 됩니다.JS마우스를 따르는 코드가 여러분과 공유됩니다.

실행 중인 효과의 스크린샷은 다음과 같습니다.

온라인 데모 주소는 다음과 같습니다.

http: //demo.jb51.net/ js/2015/js-handle-style-focus-codes/

구체 코드는 다음과 같습니다.

<!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=utf-8" />
<title>可爱的鼠标跟随</title>
<style>
html{ background:#000;}
body,html,input{ cursor:none;}
body,html{ height:100%;}
#cursor{ position:absolute; left:100px; top:100px; display:block;}
</style>
<script>
 window.onload = function(){
  var oCursor = document.getElementById("cursor");
  document.onmousemove=function (ev){
   var oEvent=ev||event,
    oWidth = document.documentElement.clientWidth,
    oHeight = document.documentElement.clientHeight,
    scrollTop=document.documentElement.scrollTop + oEvent.clientY,
    scrollLeft=document.documentElement.scrollLeft + oEvent.clientX;
   if(scrollTop > oHeight-oCursor.offsetHeight){
    oCursor.style.top = oHeight-oCursor.offsetHeight+&#39;px&#39;;
   }else if(scrollTop < 0){
    oCursor.style.top = 0;
   }else{
    oCursor.style.top = scrollTop+&#39;px&#39;;
   }
   if(scrollLeft > oWidth-oCursor.offsetWidth){
    oCursor.style.left = oWidth-oCursor.offsetWidth+&#39;px&#39;;
   }else{
    oCursor.style.left = scrollLeft+&#39;px&#39;;
   }
   document.onmousedown = function(){
    oCursor.innerHTML = "<img src=&#39;images/cursor_hover.png&#39; />"; 
    return false;
   }
   document.onmouseup = function(){
    oCursor.innerHTML = "<img src=&#39;images/cursor.png&#39; />"; 
   }
  };
 }
</script>
</head>
<body>
<p id="cursor"><img src="images/cursor.png" /></p>
<input type="button" style="font-size:36px; margin:100px;" value="点击" onclick="window.open(&#39;http://www.baidu.com&#39;)" />
</body>
</html>

위 내용은 이 장의 전체 내용이 JavaScript 프로그래밍에 종사하는 모든 사람에게 도움이 되기를 바랍니다. 더 많은 관련 튜토리얼을 보려면 JavaScript 비디오 튜토리얼을 방문하세요.

성명:
이 기사는 jb51.net에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제