jQuery实现字体颜色渐变效果


jQuery不允许css属性值为非数字的属性进行动画处理,

比如.animate(color:'red',500)或是.animate(fontWeight:'bold',500)都无法运行,

因此如果想实现颜色渐变的效果需要animate()外的其他方法,示例如下

方法1:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6     <script type="text/javascript" src="http://libs.baidu.com/jquery/1.7.2/jquery.js"></script>
 7 </head>
 8 <body>
 9     <div class="b1" style="font-size: 50px;font-weight: bold;position: absolute;">BBB</div>
10     <div class="b2" style="font-size: 50px;font-weight: bold;position: absolute;color: #f60;display: none;">BBB</div>
11 </body>
12 <script type="text/javascript">
13 $(function(){
14     $('.b1').hover(function(){
15         $('.b2').fadeIn(500);
16     })
17 })
18 </script>
19 </html>

设置一个和b1完全相同位置的b2并隐藏,添加hover事件使b2渐显,用这个笨方法可模拟color渐变效果

 

方法2:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
    div{
        color: red;
        transition: color 1s;
    }
    div:hover{
        color: yellow;
    }
    </style>
    <script type="text/javascript" src="d:/jquery-3.1.0.js"></script>
</head>
<body>
<div class="wrapper">1111</div>
<script type="text/javascript">
    
</script>
</body>
</html>

利用css3的transition方法,实现鼠标悬浮后,颜色渐变动画效果


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM