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