三种按钮hover的方法

按钮hover效果实现
本文介绍三种实现按钮hover效果的方法:直接更改背景图片、修改CSS样式和利用jQuery hover函数。通过示例代码展示了如何使用这些方法为按钮添加悬停效果。

给按钮加hover,发现有很多种方法,小试三种,测试后发现都OK,留在此地作一个备忘。

 

1.直接改变背景图片 
  obj.style.backgroundImage="url(btnSmall.gif)";
2.改变CSS
  obj.style.cssText= "background:url('btnSmallHover.gif')"; 
3.jquery的hover
  jquery.hover(function(){},function(){});

 

示例代码如下图所示:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script> <script>
//改变背景图片
function m(obj,type)
{
switch(type)
{
case 'out':
obj.style.backgroundImage
="url(btnSmall.gif)";
break;
case 'over':
obj.style.backgroundImage
="url(btnSmallHover.gif)";
break;
}
}
//改变cssText
function mCss(obj,type){
switch(type)
{
case 'out':
obj.style.cssText
= "background:url('btnSmall.gif')";
break;
case 'over':
obj.style.cssText
= "background:url('btnSmallHover.gif')";
break;
}
}
//使用jquery
$(function(){
$(
'#c').hover(
function(){ $(this).addClass("hover");},
function(){ $(this).removeClass("hover");}
);
})

</script>

<style>
button
{border:0;width:82;height:40;background: url("btnSmall.gif");}

.hover
{background:url( 'btnSmallHover.gif');}
</style>
</head>
<body>
<button onmouseover="m(this,'over');" onmouseout="m(this,'out');">确定1</button>
<button onmouseover="mCss(this,'over');" onmouseout="mCss(this,'out');">确定2</button>
<button id="c">确定3</button>
</body>
</html>



转载于:https://www.cnblogs.com/yingzi/archive/2012/04/05/2433413.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值