模仿阿里云的(鼠标经过事件)

7 篇文章 0 订阅

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>无标题文档</title>

<style>

div{

height:150px;

width:150px;

margin:100px auto;

border:1px solid #000;

float:left;

margin:0 20px;

}

div:nth-of-type(1){

background:url(images/1.jpg) no-repeat;

}

div:nth-of-type(2){

background:url(images/2.jpg) no-repeat;

}

div:nth-of-type(3){

background:url(images/3.jpg) no-repeat;

}

div:nth-of-type(4){

background:url(images/4.jpg) no-repeat;

}

div:nth-of-type(5){

background:url(images/5.jpg) no-repeat;

}

div:nth-of-type(6){

background:url(images/6.jpg) no-repeat;

}

</style>

<script>

/*原生js通过定时器实现图片位置切换得到“动图”*/

window.onload = function(){

var aDiv = document.getElementsByTagName('div');

function aaa(obj){

obj.onmouseover = function(){

clearInterval(this.timer)

this.num =0;

var This = this;

this.timer= setInterval(function(){

if(This.num==59){

This.num=59

clearInterval(This.timer)

}else{

This.num++;

}

This.style.backgroundPosition = '0 -'+This.num*150+'px'

},30)

}



obj.onmouseout = function(){

clearInterval(this.timer)

var This = this;

this.timer= setInterval(function(){

if(This.num==0){

This.num=0

clearInterval(This.timer)

}else{

This.num--;

}

This.style.backgroundPosition = '0 -'+This.num*150+'px'

},30)

}

}

for(var i = 0;i<aDiv.length;i++){

aaa(aDiv[i])

}



}

</script>

</head>



<body>

<div></div>

<div></div>

<div></div>

<div></div>

<div></div>

<div></div>

</body>

</html>


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值