如何使用jQuery淡入文本内容?
重点是吸引用户的注意力到这条消息上。
如何使用jQuery淡入文本内容?
重点是吸引用户的注意力到这条消息上。
$('#myElement').animate({backgroundColor: '#FF0000'}, 'slow');
jQueryUI有一些内置效果,可能对您有用。
我知道问题是如何使用jQuery来实现,但你可以通过简单的CSS和一点点jQuery来达到相同的效果...
例如,你有一个带有“box”类的div,添加以下CSS:
.box {
background-color: black;
-webkit-transition: background 0.5s linear;
-moz-transition: background 0.5s linear;
-ms-transition: background 0.5s linear;
-o-transition: background 0.5s linear;
transition: background 0.5s linear;
}
然后使用AddClass
函数添加另一个类,其背景色不同,例如“box highlighted”等,CSS如下:
.box.highlighted {
background-color: white;
}
$('div').animate({backgroundColor: '#f00'})
来改变颜色。仅使用jQuery(没有UI库/插件)。即使可以轻松地消除jQuery的使用。
//Color row background in HSL space (easier to manipulate fading)
$('tr').eq(1).css('backgroundColor','hsl(0,100%,50%');
var d = 1000;
for(var i=50; i<=100; i=i+0.1){ //i represents the lightness
d += 10;
(function(ii,dd){
setTimeout(function(){
$('tr').eq(1).css('backgroundColor','hsl(0,100%,'+ii+'%)');
}, dd);
})(i,d);
}
演示: http://jsfiddle.net/5NB3s/2/
根据您的浏览器支持,您可以使用CSS动画。CSS动画的浏览器支持是IE10及以上版本。这很好,因此如果只是一个小彩蛋,您就不必添加jquery UI依赖项。如果它对您的网站至关重要(即需要IE9及以下版本支持),请选择jquery UI解决方案。
.your-animation {
background-color: #fff !important;
-webkit-animation: your-animation-name 1s ease 0s 1 alternate !important;
}
//You have to add the vendor prefix versions for it to work in Firefox, Safari, and Opera.
@-webkit-keyframes your-animation-name {
from { background-color: #5EB4FE;}
to {background-color: #fff;}
}
-moz-animation: your-animation-name 1s ease 0s 1 alternate !important;
}
@-moz-keyframes your-animation-name {
from { background-color: #5EB4FE;}
to {background-color: #fff;}
}
-ms-animation: your-animation-name 1s ease 0s 1 alternate !important;
}
@-ms-keyframes your-animation-name {
from { background-color: #5EB4FE;}
to {background-color: #fff;}
}
-o-animation: your-animation-name 1s ease 0s 1 alternate !important;
}
@-o-keyframes your-animation-name {
from { background-color: #5EB4FE;}
to {background-color: #fff;}
}
animation: your-animation-name 1s ease 0s 1 alternate !important;
}
@keyframes your-animation-name {
from { background-color: #5EB4FE;}
to {background-color: #fff;}
}
接下来创建一个jQuery点击事件,将your-animation
类添加到您想要动画的元素上,触发背景从一种颜色渐变到另一种颜色:
$(".some-button").click(function(e){
$(".place-to-add-class").addClass("your-animation");
});
setTimeout
以触发插件淡出到原始背景颜色来创建高亮效果。使用简单的 JavaScript 实现两种颜色之间的淡入淡出效果:
function Blend(a, b, alpha) {
var aa = [
parseInt('0x' + a.substring(1, 3)),
parseInt('0x' + a.substring(3, 5)),
parseInt('0x' + a.substring(5, 7))
];
var bb = [
parseInt('0x' + b.substring(1, 3)),
parseInt('0x' + b.substring(3, 5)),
parseInt('0x' + b.substring(5, 7))
];
r = '0' + Math.round(aa[0] + (bb[0] - aa[0])*alpha).toString(16);
g = '0' + Math.round(aa[1] + (bb[1] - aa[1])*alpha).toString(16);
b = '0' + Math.round(aa[2] + (bb[2] - aa[2])*alpha).toString(16);
return '#'
+ r.substring(r.length - 2)
+ g.substring(g.length - 2)
+ b.substring(b.length - 2);
}
function fadeText(cl1, cl2, elm){
var t = [];
var steps = 100;
var delay = 3000;
for (var i = 0; i < steps; i++) {
(function(j) {
t[j] = setTimeout(function() {
var a = j/steps;
var color = Blend(cl1,cl2,a);
elm.style.color = color;
}, j*delay/steps);
})(i);
}
return t;
}
var cl1 = "#ffffff";
var cl2 = "#c00000";
var elm = document.getElementById("note");
T = fadeText(cl1,cl2,elm);
来源: http://www.pagecolumn.com/javascript/fade_text.htm
这是一个用JavaScript实现的文本渐隐效果。它通过改变文本的透明度来实现动画效果。你可以在网页中使用它,为你的文字添加一些生动的效果。
使用JavaScript实现在不使用jQuery或其他库的情况下淡出到白色:
<div id="x" style="background-color:rgb(255,255,105)">hello world</div>
<script type="text/javascript">
var gEvent=setInterval("toWhite();", 100);
function toWhite(){
var obj=document.getElementById("x");
var unBlue=10+parseInt(obj.style.backgroundColor.split(",")[2].replace(/\D/g,""));
if(unBlue>245) unBlue=255;
if(unBlue<256) obj.style.backgroundColor="rgb(255,255,"+unBlue+")";
else clearInterval(gEvent)
}
</script>
10+
中,减去蓝色的值增加直到达到255。